Esempio n. 1
0
        public static Texture2D GetTile(this Texture2D texture, TiledTilesetData tilesetData, int lid)
        {
            int x = (lid % tilesetData.columns) * tilesetData.tilewidth;
            int y = (tilesetData.columns - Mathf.FloorToInt(lid / tilesetData.columns) - 1) * tilesetData.tileheight;

            Texture2D tileTexture = new Texture2D(tilesetData.tilewidth, tilesetData.tileheight);

            tileTexture.SetPixels(texture.GetPixels(x, y, tilesetData.tilewidth, tilesetData.tileheight));
            tileTexture.Apply();
            return(tileTexture);
        }
Esempio n. 2
0
        public static Texture2D GetTexture2D(this TiledTilesetData tilesetData, string mapPath)
        {
            Texture2D texture   = null;
            string    imagePath = tilesetData.AbsoluteImagePath(mapPath);

            if (File.Exists(imagePath))
            {
                texture = new Texture2D(2, 2);
                texture.LoadImage(File.ReadAllBytes(imagePath));
            }
            return(texture);
        }
        public int GetTileset(int gid)
        {
            for (int i = 0; i < tilesets.Length; i++)
            {
                TiledTilesetData tileset = tilesets[i];

                if (gid >= tileset.firstgid && gid < tileset.firstgid + tileset.tilecount)
                {
                    return(i);
                }
            }
            return(-1);
        }
        public void Init(TiledTilesetData tilesetData, string mapPath)
        {
            if (tiles == null || tiles.Length != tilesetData.tilecount)
            {
                tiles = new TiledTile[tilesetData.tilecount];
            }

            Texture2D tilesetTexture = tilesetData.GetTexture2D(mapPath);

            if (tilesetTexture != null)
            {
                for (int lid = 0; lid < tilesetData.tilecount; lid++)
                {
                    tiles[lid].texture = tilesetTexture.GetTile(tilesetData, lid);
                }
            }
        }
        /// <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);
            }
        }
        void DrawTilesetEditing()
        {
            if (target.tilesets == null || target.tilesets.Length != data.tilesets.Length)
            {
                target.tilesets = new TiledTileset[data.tilesets.Length];
            }
            if (showTilesets == null || showTilesets.Length != target.tilesets.Length)
            {
                showTilesets = new bool[target.tilesets.Length];
            }
            for (int i = 0; i < target.tilesets.Length; i++)
            {
                TiledTilesetData tilesetData = data.tilesets[i];

                if (target.tilesets[i] == null)
                {
                    target.tilesets[i] = CreateInstance <TiledTileset>();
                }
                target.tilesets[i].Init(tilesetData, fullPath);

                if (!(showTilesets[i] = EditorGUILayout.Foldout(showTilesets[i], tilesetData.name)))
                {
                    continue;
                }

                EditorGUI.indentLevel++;
                EditorGUILayout.BeginVertical();
                EditorGUI.indentLevel--;

                target.tilesets[i] = EditorGUILayout.ObjectField("Tileset Asset", target.tilesets[i], typeof(TiledTileset), true) as TiledTileset;

                for (int col = 0; col < tilesetData.columns; col++)
                {
                    EditorGUILayout.BeginHorizontal();
                    for (int row = 0; row < tilesetData.tilecount / tilesetData.columns; row++)
                    {
                        int lid = row + col * tilesetData.columns;
                        EditorGUILayout.BeginVertical();

                        if (GUILayout.Button(target.tilesets[i].tiles[lid].texture, GUILayout.ExpandWidth(true)))
                        {
                            TiledTileWindow.EditTile(target.tilesets[i], lid);
                        }

                        EditorGUILayout.EndVertical();
                    }
                    EditorGUILayout.EndHorizontal();
                }

                if (GUILayout.Button("Save Tileset"))
                {
                    string path = EditorUtility.SaveFilePanelInProject("Save Tileset", tilesetData.name, "asset", "Message");
                    if (path.Length > 0)
                    {
                        AssetDatabase.CreateAsset(target.tilesets[i], path);
                    }
                }

                EditorGUILayout.EndVertical();
            }
        }