Exemple #1
0
        /// <summary>
        /// Serialises the currently loaded level into an .xml file in the levels folder.
        /// </summary>
        /// <param name="levelName">The name of the level to load.</param>
        public static void SaveLevel(string levelName, bool useFullpath = false)
        {
            //Construct a new document and create the root node
            var document = new XmlDocument();
            var root     = document.CreateElement("BoxLevel");

            document.AppendChild(root);

            //In the meta node, we store data relating to the level, such as the author's details and the version of BoxEd with which it was created
            var meta = document.CreateElement("Meta");

            root.AppendChild(meta);
            {
                var level = document.CreateElement("Level");
                meta.AppendChild(level);

                level.SetAttribute("Name", LevelManager.LevelName);

                var author = document.CreateElement("Author");
                meta.AppendChild(author);

                author.SetAttribute("Author", LevelManager.AuthorName);

                var version = document.CreateElement("Version");
                meta.AppendChild(version);

                version.SetAttribute("App", Editor.Version.ToString(2));
            }

            //Data houses all the entities from the game world
            var data = document.CreateElement("Data");

            root.AppendChild(data);

            //This loop is where we serialise all the entities to XML
            foreach (var entity in LevelManager.Find <Entity>())
            {
                var type    = entity.GetType();
                var element = document.CreateElement(type.Name);

                //This scary piece of Linq code just gets us all properties that use the BoxEdProperty attribute
                var properties = type.GetProperties().Where(prop => prop.GetCustomAttributes(typeof(EntityPropertyAttribute), true).Length > 0);

                //Unique names will be a future feature for referencing entities
                if (entity.UniqueName != null)
                {
                    element.SetAttribute("Name", entity.UniqueName);
                }

                element.SetAttribute("Position", ConvertVector(entity.transform.position));

                foreach (var property in properties)
                {
                    element.SetAttribute(property.PropertyType + "." + property.Name, property.GetValue(entity, null).ToString());
                }

                data.AppendChild(element);
            }

            if (!useFullpath)
            {
                if (!Directory.Exists(LevelManager.LevelFolder))
                {
                    Directory.CreateDirectory(LevelManager.LevelFolder);
                }

                Editor.Log("Saving to {0}. Content is: {1}", LevelManager.GetFilepath(levelName), document.InnerXml);
                document.Save(LevelManager.GetFilepath(levelName));
            }
#if UNITY_EDITOR
            else
            {
                Editor.Log("Updating the webplayer demo. Content is: {0}", document.InnerXml);
                document.Save(PathEx.Combine(Application.dataPath, "Resources", "Level 1.box.xml"));
            }
#endif
        }
Exemple #2
0
 /// <summary>
 /// Retrieves the full filepath to the given level.
 /// </summary>
 /// <param name="levelName"></param>
 /// <returns></returns>
 public static string GetFilepath(string levelName)
 {
     return(PathEx.Combine(LevelFolder, levelName + ".box"));
 }