Unity Account You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Unity Load Xml
What to expect
Data can be saved in many different formats. Xml is one of the more standardized ones. There are several different ways to parse an xml file. The XmlSerializer is one of the easiest way included directly with every .NET/mono installation.
Saving and Loading Data: XmlSerializer. Unity strips all assets that aren't. The JSON Serialization feature converts objects to and from JSON format. This can be useful when interacting with web services, or just for packing and unpacking data to a text-based format easily. For information on the JsonUtility class, please see the Unity ScriptRef JsonUtility page. A Unity ID allows you to buy and/or subscribe to Unity products and services. Serialization and Game Data. Intermediate Scripting. Unity serialization is a powerful feature but a little bit hard to master. Working on my AudioLogic plugin I learned something that I want to share.
The goal of this tutorial will be to save and load data with the following xml structure.
Preparing the Monster class
The XmlSerializer automatically knows about each public variable or read/write property in any type you can throw at it. Primitive types like string, int, float and enums can be automatically serialized.
Through attributes you can further tell the XmlSerializer about how you want the xml to be parsed.By default every variable will be translated to one xml element (e.g. <Health>5</Health>).If you want it to be parsed as attribute ( e.g. <Monster name='a'> ) you have to use XmlAttribute(name) like in the sample.
The MonsterContainer
To store all of the monsters we need a list of all of them.
The root element of each xml file should be annotated with the XmlRoot attribute. This way the XmlSerializer will know which XmlElement is to be expected as the root element.A list is just like an array with the added bonus of being able to add new elements easily.
With XmlArray and XmlArrayItem you can declare how the list should be represented within the xml file.
Reading data
Writing data
Make sure you call the Close or Dispose method after writing else the file you just created will only be created in memory and will never be actually written to the file.
Convenience
I personally like putting read and write methods in the root class like this.
Usage![]() Reading
Writing
Notes
As you may have noticed I am using Application.dataPath and Application.persistentDataPath in my pathes. Equal interval system.
Unity Xml Serialization
Deployment
For this code to work after being deployed you need to take additional care.
Standalone Player
After you deployed your application your Application.dataPath will point to '(PathToExecutable)/executable_Data'. So you need to take care to store your monsters.xml there for the application to find it.
Unity XmlWebplayer
The webplayer can't access files on any computer directly so using the methods described before to read/write data won't work here.To load any data you need to use the WWW class.
Example that loads the monsters.xml file directly from where your webplayer html is located.
iOS Devices
A NullReferenceException will be raised if you're using List<T> in your MonsterContainer class, use an array instead. Similarly the same exception is raised if you're using get and set to implement properties in the class you're trying to serialize.
![]() Additional Notes![]()
Retrieved from 'http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer&oldid=17478'
I am building a level editor for my game.
This means that I have to be able to:
I have read thread after thread about serialization, but I can't seem to find the right one. It's always about just saving the game. Is there a good way to completely serialize a GameObject and all of it children, top to bottom?
If you want to know why I am doing this, it's because I want my game to eventually become a moddable community project, and I don't want to require every contributor to have a Unity license. Furthermore, it facilitates the whole project structure if I can use files that are external from the core build. I generally find the package approach extremely limiting. And last, but not least, I want to enable contributors to make game content in Linux, which the Unity Editor currently does not :)
Any help on this is highly appreciated. Thank you in advance.
Serialization
EDIT: I have been reading about ISerializationSurrogate, is it possible that this might be my salvation?
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |