Example #1
0
        void DrawTileSets()
        {
            if (tmxFile.tileSets == null)
            {
                return;
            }

            if (tileMap.tileSetMaterials == null || tileMap.tileSetMaterials.Length != tmxFile.tileSets.Length)
            {
                tileMap.tileSetMaterials = TileMapEditor.GetMaterials(tmxFile, path);
                tileMap.tileSetSprites   = TileMapEditor.GetSprites(tmxFile, path);
            }

            EditorGUILayout.LabelField("Tile Sets", EditorStyles.boldLabel);
            string[] tilesSetNames = Array.ConvertAll(tmxFile.tileSets, (t) => t.name);
            selectedTileSetIndex = GUILayout.Toolbar(selectedTileSetIndex, tilesSetNames);

            switch (paintType)
            {
            case 0:
                TileSetField(tmxFile.tileSets[selectedTileSetIndex]);
                break;

            case 1:
                string[] terrainNames = Array.ConvertAll(terrains, (terrain) => terrain.name);
                selectedTerrainIndex = GUILayout.SelectionGrid(selectedTerrainIndex, terrainNames, 1);
                break;

            default:
                break;
            }

            paintType = GUILayout.Toolbar(paintType, new string[] { "Tiles", "Terrains" });
            EditorGUILayout.Separator();
        }
Example #2
0
        private static void DragAndDropTMXFile(bool isSceneView = false, Transform parent = null)
        {
            EventType eventType = Event.current.type;

            if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (eventType == EventType.DragPerform)
                {
                    bool foundTMX = false;
                    foreach (UnityEngine.Object o in DragAndDrop.objectReferences)
                    {
                        string tmxFilePath = AssetDatabase.GetAssetPath(o);
                        if (Path.GetExtension(tmxFilePath) == ".tmx")
                        {
                            float         pixelsPerUnit = -1;
                            AssetImporter importer      = AssetImporter.GetAtPath(tmxFilePath);
                            if (!string.IsNullOrEmpty(importer.userData))
                            {
                                pixelsPerUnit = float.Parse(importer.userData);
                            }

                            string     name    = Path.GetFileNameWithoutExtension(tmxFilePath);
                            GameObject map     = new GameObject(name);
                            TileMap    tileMap = map.AddComponent <TileMap>();
                            TMXFile    tmxFile = TMXFile.Load(tmxFilePath);
                            tileMap.tileSetMaterials = TileMapEditor.GetMaterials(tmxFile, tmxFilePath);
                            tileMap.Setup(tmxFile, tmxFilePath, pixelsPerUnit);

                            if (isSceneView)
                            {
                                Ray   ray   = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                                float dist  = 0;
                                Plane plane = new Plane(Vector3.forward, tileMap.transform.position);
                                if (plane.Raycast(ray, out dist))
                                {
                                    map.transform.position = ray.GetPoint(dist);
                                }
                            }

                            if (parent != null)
                            {
                                map.transform.SetParent(parent);
                            }

                            Event.current.Use();
                            Selection.activeGameObject = map;
                            Undo.RegisterCreatedObjectUndo(map, "Created '" + name + "' from TMX file.");
                            foundTMX = true;
                        }
                    }
                    if (foundTMX)
                    {
                        DragAndDrop.AcceptDrag();
                        Event.current.Use();
                    }
                }
            }
        }
Example #3
0
 void DrawFilePanel()
 {
     EditorGUILayout.BeginHorizontal();
     if (GUILayout.Button("Reload"))
     {
         tmxFile = TMXFile.Load(path);
         tileMap.tileSetMaterials = TileMapEditor.GetMaterials(tmxFile, path);
         tileMap.tileSetSprites   = TileMapEditor.GetSprites(tmxFile, path);
         tileMap.Setup();
         treeView.Reload();
     }
     if (GUILayout.Button("Save"))
     {
         tmxFile.Save(path);
         AssetDatabase.ImportAsset(path);
     }
     if (GUILayout.Button("Save As"))
     {
         tmxFile.Save(
             EditorUtility.SaveFilePanel(
                 "Save as TMX",
                 Path.GetDirectoryName(path),
                 Path.GetFileNameWithoutExtension(path),
                 Path.GetExtension(path).TrimStart(new char[] { '.' })
                 )
             );
         AssetDatabase.Refresh();
         AssetDatabase.ImportAsset(path);
     }
     EditorGUILayout.EndHorizontal();
 }
Example #4
0
        private void UpdateTexturePadding()
        {
            tileSets[path] = TileSet.Load(path); // DELETE ME
            TileSet   ts          = tileSet;
            Texture2D texture     = TileMapEditor.GetTileSetTexture(ts, path);
            string    texturePath = AssetDatabase.GetAssetPath(texture);

            TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(texturePath);

            importer.isReadable = true;
            AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);

            int inWidth   = ts.tileWidth * ts.columns + ts.margin * 2 + ts.spacing * (ts.columns - 1);
            int outWidth  = ts.tileWidth * ts.columns + margin * 2 + spacing * (ts.columns - 1);
            int outHeight = ts.tileHeight * ts.rows + margin * 2 + spacing * (ts.rows - 1);

            Color[] inColors  = texture.GetPixels();
            Color[] outColors = new Color[outWidth * outHeight];

            for (int column = 0; column < ts.columns; column++)
            {
                for (int row = 0; row < ts.rows; row++)
                {
                    for (int i = 0; i < ts.tileWidth; i++)
                    {
                        for (int j = 0; j < ts.tileHeight; j++)
                        {
                            int   inX = i + (ts.tileWidth + ts.spacing) * column + ts.margin;
                            int   inY = j + (ts.tileHeight + ts.spacing) * row + ts.margin;
                            Color c   = inColors[inY * inWidth + inX];

                            int left  = (i == 0) ? ((column == 0) ? -margin : -spacing / 2) : 0;
                            int right = (i == ts.tileWidth - 1) ? ((column == ts.columns - 1) ? margin : spacing / 2) : 0;
                            int down  = (j == 0) ? ((row == 0) ? -margin : -spacing / 2) : 0;
                            int up    = (j == ts.tileHeight - 1) ? ((row == ts.rows - 1) ? margin : spacing / 2) : 0;
                            for (int x = i + left; x <= i + right; x++)
                            {
                                for (int y = j + down; y <= j + up; y++)
                                {
                                    int outX = x + (ts.tileWidth + spacing) * column + margin;
                                    int outY = y + (ts.tileHeight + spacing) * row + margin;
                                    outColors[outY * outWidth + outX] = c;
                                }
                            }
                        }
                    }
                }
            }

            Texture2D t = new Texture2D(outWidth, outHeight);

            t.SetPixels(outColors);
            byte[] bytes = t.EncodeToPNG();
            File.WriteAllBytes(texturePath, bytes);

            AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);

            tileSet.margin       = margin;
            tileSet.spacing      = spacing;
            tileSet.image.width  = outWidth;
            tileSet.image.height = outHeight;
            tileSet.Save(path);
        }