- Home /
How to use XML Serialization to load custom asset types
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 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?
Answer by Lucas Meijer 1 · Dec 20, 2009 at 10:33 PM
I would approach this by making a
using UnityEngine;
public class XMLContains : ScriptableObject
{
public string theXML;
}
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:
using UnityEngine; public class TieFighter : MonoBehaviour { public XMLContainer myXMLSettings;
void Awake()
{
//parse myXMLSettings.theXML into reasonable data
}
void Update()
{
//use the reasonable data
}
}
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.
Answer by $$anonymous$$ · Feb 28, 2011 at 07:17 PM
You can start by this method and make adaptations as needed. (Link)
Answer by Ashkan_gc · Dec 20, 2009 at 12:08 PM
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.
Your answer
Follow this Question
Related Questions
[SCREENSHOTS]Save scenes using XML/json descriptor 0 Answers
ScriptableObject+XmlSerializer=sad 2 Answers
Questions about XML serialization of Class Properties 1 Answer
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer