We have custom assets that contain data that serializes very well to XML (string-based) using the standard .NET XmlSerialization methods. I can make this work very well manually, within Unity Script code (put into plugin assemblies that I have dropped into "Assets/Plugins")... but this is not making use of "Resources.Load(path, Type)". It seems that your examples for using this method only reference built-in Unity types (Texture2D, TextAsset, etc).
How do I register my own type (e.g. "XML") to work with this? I want to make sure this all works well in the Web Player, which isn't going to fly if I'm doing direct http://System.IO to the file system. I need my XML files to be stored under Resources and to be packaged with the Web Player deployment, and allow me to deserialize my XML files from in this mode of deployment.
Got any good advice?
I would approach this by making a
By making your class enherit from ScriptableObject, it becomes an "Asset Type" to unity, in the sense that you can store them on disk, and use the Unity IDE interface to drag it from the assetsfolder to a behaviour or whatever. It also automatically plays nice with Unity's dependency tracking system that decides what does and what does not go into a webplayer. Going a bit further with this example, you could have let's say a TieFighter class, actually get some of its information (max health, max speed, acceleration), from your xml file, by making the TieFighter class look like this:
You could then attach the TieFighter script to a gameobject, and then drag your xml-asset from your assetsfolder onto the myXMLSettings public field.
Please note that this example is much better at showing how you could use a ScriptableObject than how to properly implement a TieFighter, as I would never really have that parse an actual xml file on runtime. Tiefighters are too cool for xml anyway.
answered Dec 20 '09 at 10:33 PM
Lucas Meijer 1 ♦♦
this problem is something that should be fixed in future versions of unity i think. you have two methods: create asset bundles with your XML files and load them with www class. www can get file system paths like this "file://path". the other way is to put the XML in a URL and load it with WWW class. also you can put the xml file as a text asset in your unity application and then easily work with it.
answered Dec 20 '09 at 12:08 PM