GameObject to XML

I’ve seen some scripts that saves an GameObject to an XML with transform.position infos.

But is it possible to save a GameObject including if it has colliders, or if it has a Mesh Renderer it will save it including what material its using and what textures. and if there is a script attached to it, and the public variable values.

How would you know if a gameObject has this kind of script, and then name of the variables, etc. .
Do I have to literally include all the script class in my project folder to put in the code of this XML saver and which variables are public and constantly update it when I’ve added a new script class in the project folder?

Well, i’m sure there are already some xml serializer out there, but i wasn’t in need of one yet :D. Basically you can use GetComponents() to get a list of all attached scripts. You would need to use reflection to get the public variables (type, name, value) if it should be useable for arbitrary scripts.

The second (maybe easier) solution would be to create an Interface from which all your scripts inherit. This interface forces those classes to implement a serialize and deserialize function. So your scripts have to provide these functions.

// C#

public interface IXMLSerializable
{
    public string Serialize();
    public void Deserialize(string aXMLText);
}

public class SampleClass : MonoBehaviour, IXMLSerializable
{
    public int intValue = 5;
    public string Serialize()
    {
        return "<intValue>"+intValue+ "</intValue>";
    }
    public void Deserialize(string aXMLText);
    {
        // parse the xml and set all of your variables
    }
}

That’s just an example. If you are familiar with reflection it will be easier (or maybe less complicated) with reflection.

ps. To process all components (including collider, renderer…) use GetComponents();
you have to check with the “is” operator what it is and serialize it as you need it. For things like materials you need some kind of material/texture manager. Otherwise you can’t access them (or put them all in the Ressources folder, not recommended)