Ejemplo n.º 1
0
        private void DoWindow(int id)
        {
            Event      e = Event.current;
            STETilemap defaultTilemap = m_target.GetDefaultTilemapForCurrentTileSelection();

            GUILayout.BeginArea(new Rect(0f, EditorGUIUtility.singleLineHeight, m_defaultTilemapsWindowRect.width, m_defaultTilemapsWindowRect.height));
            GUILayout.Label("Default Tilemap: " + (defaultTilemap? "<b>" + defaultTilemap.name + "</b>" : "<none>"), STEditorStyles.Instance.richLabel);
            if (GUILayout.Button("Set Default Tilemap <b>(For the Palette Selection)</b>", STEditorStyles.Instance.richButton))
            {
                if (m_target.SelectedTilemap && m_target.SelectedTilemap.Tileset && m_target.SelectedTilemap.Tileset.TileSelection != null)
                {
                    foreach (uint tileData in m_target.SelectedTilemap.Tileset.TileSelection.selectionData)
                    {
                        SetDefaultTilemapFromTileData(m_target.SelectedTilemap, 0, 0, tileData);
                    }
                }
                else
                {
                    m_target.SetDefaultTilemapForCurrentSelectedTileOrBrush();
                }
            }
            if (GUILayout.Button("Remove Default Tilemap <b>(For the Palette Selection)</b>", STEditorStyles.Instance.richButton))
            {
                m_target.ClearDefaultTilemapForCurrentSelectedTileOrBrush();
            }
            EditorGUILayout.Space();
            if (GUILayout.Button("Set Default Tilemap <b>(For the Tiles in the Tilemap)</b>", STEditorStyles.Instance.richButton))
            {
                TilemapUtils.IterateTilemapWithAction(m_target.SelectedTilemap, SetDefaultTilemapFromTileData);
            }
            if (GUILayout.Button("Set Default Tilemap <b>(For all Tilemaps)</b>", STEditorStyles.Instance.richButton))
            {
                m_target.IterateTilemapWithAction((STETilemap tilemap) => {
                    TilemapUtils.IterateTilemapWithAction(tilemap, SetDefaultTilemapFromTileData);
                });
            }
            EditorGUILayout.Space();

            /*
             * if (GUILayout.Button("Clear Default Tilemap <b>(All Tiles in the Tilemap)</b>", STEditorStyles.Instance.richButton))
             *  TilemapUtils.IterateTilemapWithAction(m_target.SelectedTilemap, (STETilemap tilemap, int gx, int gy, uint tileData) =>
             *  {
             *      int brushId = Tileset.GetBrushIdFromTileData(tileData);
             *      if (brushId != Tileset.k_BrushId_Default)
             *          m_target.ClearBrushDefaultTilemap(brushId);
             *      else
             *      {
             *          int tileId = Tileset.GetTileIdFromTileData(tileData);
             *          if (tileId != Tileset.k_TileId_Empty)
             *              m_target.ClearTileDefaultTilemap(tileId);
             *      }
             *  });
             */
            if (GUILayout.Button("Clear Default Tilemap Data <b>(For all Tiles)</b>", STEditorStyles.Instance.richButton))
            {
                m_target.ClearAllDefaultTilemapData();
            }
            GUILayout.EndArea();
            if (e.isMouse && m_defaultTilemapsWindowRect.Contains(e.mousePosition))
            {
                if (e.type == EventType.MouseDown && e.mousePosition.y <= EditorGUIUtility.singleLineHeight)
                {
                    m_defaultTilemapWindowMinimized = !m_defaultTilemapWindowMinimized;
                }
                e.Use();
            }
        }
        static public Texture2D CreateTexture2DFromTilemap(STETilemap tilemap)
        {
            MakeTextureReadable(tilemap.Tileset.AtlasTexture);
            int       tilePxSizeX = (int)tilemap.Tileset.TilePxSize.x;
            int       tilePxSizeY = (int)tilemap.Tileset.TilePxSize.y;
            Texture2D output      = new Texture2D(tilemap.GridWidth * tilePxSizeX, tilemap.GridHeight * tilePxSizeY, TextureFormat.ARGB32, false);

            output.filterMode = FilterMode.Point;
            output.SetPixels32(new Color32[output.width * output.height]);
            output.Apply();
            System.Action <STETilemap, int, int, uint> action = (STETilemap source, int gridX, int gridY, uint tileData) =>
            {
                gridX -= source.MinGridX;
                gridY -= source.MinGridY;
                Tile tile = tilemap.Tileset.GetTile(Tileset.GetTileIdFromTileData(tileData));
                if (tile != null)
                {
                    Texture2D atlasTexture = tilemap.Tileset.AtlasTexture;
                    int       tx           = Mathf.RoundToInt(tile.uv.x * atlasTexture.width);
                    int       ty           = Mathf.RoundToInt(tile.uv.y * atlasTexture.height);
                    int       tw           = tilePxSizeX;
                    int       th           = tilePxSizeY;
                    Sprite    prefabSprite = null;
                    if (tile.prefabData.prefab)
                    {
                        SpriteRenderer spriteRenderer = tile.prefabData.prefab.GetComponent <SpriteRenderer>();
                        if (spriteRenderer && spriteRenderer.sprite)
                        {
                            prefabSprite = spriteRenderer.sprite;
                            MakeTextureReadable(spriteRenderer.sprite.texture);
                            atlasTexture = spriteRenderer.sprite.texture;
                            tx           = Mathf.RoundToInt(spriteRenderer.sprite.textureRect.x);
                            ty           = Mathf.RoundToInt(spriteRenderer.sprite.textureRect.y);
                            tw           = Mathf.RoundToInt(spriteRenderer.sprite.textureRect.width);
                            th           = Mathf.RoundToInt(spriteRenderer.sprite.textureRect.height);
                        }
                    }
                    bool    flipH         = (tileData & Tileset.k_TileFlag_FlipH) != 0;
                    bool    flipV         = (tileData & Tileset.k_TileFlag_FlipV) != 0;
                    bool    rot90         = (tileData & Tileset.k_TileFlag_Rot90) != 0;
                    Color[] srcTileColors = atlasTexture.GetPixels(tx, ty, tw, th);
                    if (flipH)
                    {
                        Color[] tempArr = new Color[0];
                        for (int i = 0; i < th; ++i)
                        {
                            tempArr = tempArr.Concat(srcTileColors.Skip(tw * i).Take(tw).Reverse()).ToArray();
                        }
                        srcTileColors = tempArr;
                    }
                    if (flipV)
                    {
                        Color[] tempArr = new Color[0];
                        for (int i = th - 1; i >= 0; --i)
                        {
                            tempArr = tempArr.Concat(srcTileColors.Skip(tw * i).Take(tw)).ToArray();
                        }
                        srcTileColors = tempArr;
                    }
                    if (rot90)
                    {
                        Color[] tempArr = new Color[tw * th];
                        for (int x = tw - 1, i = 0; x >= 0; --x)
                        {
                            for (int y = 0; y < th; ++y, ++i)
                            {
                                tempArr[i] = srcTileColors[y * tw + x];
                            }
                        }
                        srcTileColors = tempArr;
                        int temp = tw;
                        tw = th;
                        th = temp;
                    }
                    if (prefabSprite)
                    {
                        Vector2 tileSize = prefabSprite.textureRect.size;
                        Vector2 pivot    = prefabSprite.pivot - prefabSprite.textureRectOffset;
                        if (flipV)
                        {
                            pivot.y = -pivot.y + prefabSprite.textureRect.height;
                        }
                        if (flipH)
                        {
                            pivot.x = -pivot.x + prefabSprite.textureRect.width;
                        }
                        if (rot90)
                        {
                            pivot      = new Vector2(pivot.y, tileSize.x - pivot.x);
                            tileSize.x = prefabSprite.textureRect.size.y;
                            tileSize.y = prefabSprite.textureRect.size.x;
                        }
                        Vector2 offset = pivot - tilemap.Tileset.TilePxSize / 2;// sprite.pivot + sprite.textureRect.position - sprite.textureRectOffset;
                        BlitPixels(output, gridX * tilePxSizeX - Mathf.RoundToInt(offset.x), gridY * tilePxSizeY - Mathf.RoundToInt(offset.y), Mathf.RoundToInt(tileSize.x), Mathf.RoundToInt(tileSize.y), srcTileColors);
                    }
                    else
                    {
                        output.SetPixels(gridX * tilePxSizeX, gridY * tilePxSizeY, tw, th, srcTileColors);
                    }
                }
            };
            TilemapUtils.IterateTilemapWithAction(tilemap, action);
            output.Apply();
            return(output);
        }