/// <summary>
        /// Creates a new <see cref="TiledMapData"/> instance with data from a xml file.
        /// </summary>
        /// <param name="path">Absolute path to file</param>
        /// <returns>A populated <see cref="TiledMapData"/></returns>
        public static TiledMapData Load(string path)
        {
            // Declare a new serializer with our type
            XmlSerializer serializer = new XmlSerializer(typeof(TiledMapData));

            // Open file for reading
            using (FileStream stream = new FileStream(path, FileMode.Open)) {
                // Create reader, set settings and deserialize
                XmlReader reader = XmlReader.Create(stream);
                reader.Settings.IgnoreWhitespace = true;
                TiledMapData result = serializer.Deserialize(reader) as TiledMapData;

                // Check each tileset and read it from file if needed
                int count = 0;
                foreach (TiledTilesetData tileset in result.tilesets)
                {
                    if (Path.GetExtension(tileset.source) != ".tsx")
                    {
                        continue;
                    }

                    result.tilesets[count]          = TiledTilesetData.Load(Path.GetFullPath(path + "\\..\\" + tileset.source));
                    result.tilesets[count].firstgid = tileset.firstgid;
                    result.tilesets[count].source   = tileset.source;
                    count++;
                }

                // Convert csv data of each layer to an array
                foreach (TiledLayerData layer in result.layers)
                {
                    if (layer.data.encoding != "csv")
                    {
                        continue;
                    }

                    string[] tokens = layer.data.text.Split(',');
                    layer.data.gids = Array.ConvertAll(tokens, int.Parse);
                }

                return(result);
            }
        }
Exemplo n.º 2
0
        bool DrawMapSelection()
        {
            bool foundFile = false;

            // Try loading mapdata from file
            try {
                // If everything is successful
                target.data = TiledMapData.Load(fullPath);
                EditorGUILayout.HelpBox("Using file: " + fullPath, MessageType.Info);
                foundFile = true;
            } catch (System.Exception) {
                // If the file can't be found
                EditorGUILayout.HelpBox("Can't find file: " + fullPath, MessageType.Error);
            }
            if (GUILayout.Button("Select File"))
            {
                // Lambda expression to avoid (redundant?) functions
                TiledMapSelectorWindow.SelectMap(x => target.relativePath = x);
            }

            return(foundFile);
        }