I’ve written a custom editor to allow myself to key in different parameters for monster, level state information.
For example:
Monster Editor: I can set monster type, HP, damage, and etc.
Level Editor: I can set total monsters for a particular level, difficulty, possible drop items and etc.
My problem is, I don’t know how can I record all these data.
Currently my idea is to put them in XML files and load them during game launch. By doing this, I have no problem to load through editor but the problem arise when I’m trying to test the load on mobile devices because the device couldn’t find/load the XML files. I have put the XML files in Resources folder but somehow it just couldn’t work.
I’m looking for better/faster way to handle this save/load and make sure it works on mobile devices as well.
Anyone who done this before, could you please share your ideas with me on how should I handle this.
I think I’ll just write a template and just copy paste it from now on. I think - lately - it has been among the most often asked question. (Made a quick blog post: http://lightstrikersoftware.com/index.php/blog )
In the less painful, most straight forwards, no conversion at all;
The good thing? Unity handle all the serialization/deserialization on its own. It also uses the same rules as what the Inspector uses for what is serializable and what isn’t.
After tested, it seems like the following codes work fine on both PC and Mobile
var serializer = new XmlSerializer(typeof(MyClassContainer));
TextAsset textAsset = (TextAsset)Resources.Load("data", typeof(TextAsset)); // data is the data.xml
MemoryStream assetStream = new MemoryStream(textAsset.bytes);
MyClassContainer container = serializer.Deserialize(assetStream) as MyClassContainer;
assetStream.Close();
I use a similar approach all the time. One thing to be careful of is there are some cases where the AOT compiler misses some generic type. This will work on desktop/web but on iOS you will get an ExecutionEngineException which can be a little hard to trace if you don’t know what you are looking for.
The solution is to include a dummy reference to the missing type.
Another option is to use CreateAsset (like LightStriker writes) to create a ScriptableObject. Jacob Pennock wrote a good tutorial on this: http://www.jacobpennock.com/Blog/?p=670. This is a really easy way to manage and edit data.