Exemple #1
0
    /// <summary>
    /// Write a given scene to xml. The filename is the same as the scene.
    /// </summary>
    /// <param name="scene">the scene to write</param>
    /// <param name="outputPath">the folder to write the file</param>
    public static void WriteLevel(Scene scene, string outputPath)
    {
        //get scene objects
        List <GameObject> rootObjects = new List <GameObject>();

        scene.GetRootGameObjects(rootObjects);

        //create stream writer at directory
        StreamWriter     writer = new StreamWriter(outputPath + "/" + scene.name + ".xml");
        LevelDescription ld     = new LevelDescription();
        uint             count  = 0;

        EditorObjectBase.ResetGlobalID();

        //assign IDs
        foreach (GameObject ob in rootObjects)
        {
            var subs = ob.GetComponentsInChildren <EditorObjectBase>();
            foreach (var sub in subs)
            {
                sub.AssignId();
            }
        }

        // iterate root objects
        foreach (GameObject ob in rootObjects)
        {
            //only process EditorObjects
            EditorObjectBase editorObject = ob.GetComponent <EditorObjectBase>();
            if (editorObject != null)
            {
                ++count;
                ld.Add(editorObject.GetReadySerialize());
            }
        }
        XmlSerializer xml = new XmlSerializer(ld.GetType());

        xml.Serialize(writer.BaseStream, ld);
        writer.Close();
        Debug.Log("Wrote " + count + " objects to \"" + outputPath + "/" + scene.name + ".xml\"");
    }