Exemple #1
0
        private void MergeTileParameters(List <uint> listOfTileData)
        {
            List <Parameter> fullParameterList = new List <Parameter>();

            foreach (uint tileData in listOfTileData)
            {
                ParameterContainer @params = TilemapUtils.GetParamsFromTileData(Tileset, tileData);
                foreach (Parameter param in @params.ParameterList)
                {
                    if (!fullParameterList.Exists(o => o.name.Equals(param.name)))
                    {
                        fullParameterList.Add(param);
                    }
                }
            }
            foreach (uint tileData in listOfTileData)
            {
                ParameterContainer @params = TilemapUtils.GetParamsFromTileData(Tileset, tileData);
                foreach (Parameter param in fullParameterList)
                {
                    if ([email protected](o => o.name.Equals(param.name)))
                    {
                        @params.ParameterList.Add(param.Clone());
                    }
                }
            }
        }
Exemple #2
0
        public static void DrawDot(STETilemap tilemap, Vector2 locPos, uint[,] tileData, bool randomize = false)
        {
            int x0 = TilemapUtils.GetGridX(tilemap, locPos);
            int y0 = TilemapUtils.GetGridY(tilemap, locPos);

            DrawDot(tilemap, x0, y0, tileData, randomize);
        }
Exemple #3
0
        public static void DrawEllipse(STETilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData, bool isFilled, bool randomize = false)
        {
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);

            DrawEllipse(tilemap, x0, y0, x1, y1, tileData, isFilled, randomize);
        }
Exemple #4
0
        public static void DrawLineMirrored(STETilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData, bool randomize = false)
        {
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);

            DrawLineMirrored(tilemap, x0, y0, x1, y1, tileData, randomize);
        }
Exemple #5
0
 void Reset()
 {
     Start();
     m_meshRenderer.material = TilemapUtils.FindDefaultSpriteMaterial();
     if (!m_meshFilter.sharedMesh)
     {
         m_meshFilter.sharedMesh = new Mesh();
     }
     m_meshFilter.sharedMesh.name = "Quad";
 }
Exemple #6
0
 public void SetGridPos(int gridX, int gridY, Vector2 cellSize)
 {
     GridX    = gridX;
     GridY    = gridY;
     Position = TilemapUtils.GetGridWorldPos(gridX, gridY, cellSize);
     for (int y = -1, neighIdx = 0; y <= 1; ++y)
     {
         for (int x = -1; x <= 1; ++x)
         {
             if ((x | y) != 0) // skip this node
             {
                 m_neighborIdxList[neighIdx++] = UnsafeJointTwoInts(GridX + x, GridY + y);
             }
         }
     }
 }
        public static void DrawLine(Tilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData)
        {
            int w  = tileData.GetLength(0);
            int h  = tileData.GetLength(1);
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);

            TilemapDrawingUtils.Line(x0, y0, x1, y1,
                                     (x, y) =>
            {
                tilemap.SetTileData(x, y, tileData[(x % w + w) % w, (y % h + h) % h]);
                return(true);
            }
                                     );
        }
        void Awake()
        {
            BuildTilechunkDictionary();
            var valueIter = m_dicChunkCache.Values.GetEnumerator();

            while (valueIter.MoveNext())
            {
                TilemapChunk chunk = valueIter.Current;
                chunk.gameObject.hideFlags |= HideFlags.HideInHierarchy;
            }

            // NOTE: in game Reset is not called
            if (!m_material)
            {
                m_material  = TilemapUtils.FindDefaultSpriteMaterial();
                m_tintColor = Color.white;
            }
        }
        public static void DrawEllipse(Tilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData, bool isFilled)
        {
            int w  = tileData.GetLength(0);
            int h  = tileData.GetLength(1);
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);
            int xf = 0;
            int yf = 0;

            //fix for cases where x1 x2 y1 or y2 are negative or x1 > x2 or y1 > y2
            // NOTE: I tested this only for case x1 == y1 == 0
            if (x0 > x1)
            {
                Swap <int>(ref x0, ref x1);
            }
            if (y0 > y1)
            {
                Swap <int>(ref y0, ref y1);
            }
            if (x0 < 0)
            {
                xf = x0; x0 = 0; x1 -= xf;
            }
            if (y0 < 0)
            {
                yf = y0; y0 = 0; y1 -= yf;
            }
            //
            TilemapDrawingUtils.Ellipse(x0, y0, x1, y1, isFilled,
                                        (x, y) =>
            {
                tilemap.SetTileData(x + xf, y + yf, tileData[((x + xf) % w + w) % w, ((y + yf) % h + h) % h]);
                return(true);
            }
                                        );
        }
        /// <summary>
        /// Paint the tiles in a tilemap specifing a center and a radius. The color will be multiplied by the intensity curve along the radius.
        /// </summary>
        /// <param name="tilemap">The target tilemap</param>
        /// <param name="center">The center of the circle</param>
        /// <param name="radius">The radius of the circle</param>
        /// <param name="color">The color to be painted</param>
        /// <param name="blendingMode">The blending mode</param>
        /// <param name="vertexPaint">If the color is changed for the whole tile or by vertex</param>
        /// <param name="intensityCurve">The value multiplied to the color along the radius</param>
        public static void VertexPaintCircle(STETilemap tilemap, Vector2 center, float radius, Color color, eBlendMode blendingMode, bool vertexPaint = false, AnimationCurve intensityCurve = null)
        {
            Vector2 minPos     = new Vector2(center.x - radius, center.y - radius);
            Vector2 maxPos     = new Vector2(center.x + radius, center.y + radius);
            int     minGridX   = TilemapUtils.GetGridX(tilemap, minPos);
            int     minGridY   = TilemapUtils.GetGridY(tilemap, minPos);
            int     maxGridX   = TilemapUtils.GetGridX(tilemap, maxPos);
            int     maxGridY   = TilemapUtils.GetGridY(tilemap, maxPos);
            Vector2 tileCenter = TilemapUtils.GetTileCenterPosition(tilemap, minPos);
            float   minX       = tileCenter.x;
            float   sqrRadius  = radius * radius;
            Color32 color32    = color;

            for (int y = minGridY; y <= maxGridY; ++y, tileCenter.y += tilemap.CellSize.y)
            {
                tileCenter.x = minX;
                for (int x = minGridX; x <= maxGridX; ++x, tileCenter.x += tilemap.CellSize.x)
                {
                    if (vertexPaint)
                    {
                        _PaintTileVerticesByDist(tilemap, x, y, tileCenter, center, radius, color, blendingMode, intensityCurve);
                    }
                    else
                    {
                        float sqrDist = (center - tileCenter).sqrMagnitude;
                        if (sqrDist <= sqrRadius)
                        {
                            if (intensityCurve != null)
                            {
                                float dist = Mathf.Sqrt(sqrDist);
                                color32.a = (byte)(255 * color.a * intensityCurve.Evaluate(1f - dist / radius));
                            }
                            tilemap.SetTileColor(x, y, color32, blendingMode);
                        }
                    }
                }
            }
        }
        void OnValidate()
        {
            BuildTilechunkDictionary();
            m_parentTilemapGroup = GetComponentInParent <TilemapGroup>();
#if UNITY_EDITOR
            // fix: for tilemaps created with version 1.3.5 or below
            if (m_tintColor == default(Color))
            {
                Debug.Log("Fixing tilemap made with version below 1.3.5: " + name);
                if (m_material)
                {
                    m_tintColor = m_material.color; //take the color from the material
                    m_pixelSnap = Material.HasProperty("PixelSnap") && Material.IsKeywordEnabled("PIXELSNAP_ON");
                    bool fixMaterial = string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(m_material));
                    if (fixMaterial)
                    {
                        m_material = TilemapUtils.FindDefaultSpriteMaterial();
                    }
                }
            }
            //---
#endif
            PixelSnap = m_pixelSnap;
        }
        public static void DrawRect(Tilemap tilemap, Vector2 locPosA, Vector2 locPosB, uint[,] tileData, bool isFilled, bool is9Sliced = false)
        {
            int w  = tileData.GetLength(0);
            int h  = tileData.GetLength(1);
            int x0 = TilemapUtils.GetGridX(tilemap, locPosA);
            int y0 = TilemapUtils.GetGridY(tilemap, locPosA);
            int x1 = TilemapUtils.GetGridX(tilemap, locPosB);
            int y1 = TilemapUtils.GetGridY(tilemap, locPosB);

            if (x0 > x1)
            {
                Swap <int>(ref x0, ref x1);
            }
            if (y0 > y1)
            {
                Swap <int>(ref y0, ref y1);
            }
            TilemapDrawingUtils.Rect(x0, y0, x1, y1, isFilled,
                                     (x, y) =>
            {
                if (is9Sliced)
                {
                    if (x == x0 && y == y0)
                    {
                        tilemap.SetTileData(x, y, tileData[0, 0]);
                    }
                    else if (x == x0 && y == y1)
                    {
                        tilemap.SetTileData(x, y, tileData[0, h - 1]);
                    }
                    else if (x == x1 && y == y0)
                    {
                        tilemap.SetTileData(x, y, tileData[w - 1, 0]);
                    }
                    else if (x == x1 && y == y1)
                    {
                        tilemap.SetTileData(x, y, tileData[w - 1, h - 1]);
                    }
                    else
                    {
                        int cw = w - 2;
                        int ch = h - 2;
                        int cx = cw >= 1 ? 1 + (x % cw + cw) % cw : (x % w + w) % w;
                        int cy = ch >= 1 ? 1 + (y % ch + ch) % ch : (y % h + h) % h;
                        if (x == x0)
                        {
                            tilemap.SetTileData(x, y, tileData[0, cy]);
                        }
                        else if (x == x1)
                        {
                            tilemap.SetTileData(x, y, tileData[w - 1, cy]);
                        }
                        else if (y == y0)
                        {
                            tilemap.SetTileData(x, y, tileData[cx, 0]);
                        }
                        else if (y == y1)
                        {
                            tilemap.SetTileData(x, y, tileData[cx, h - 1]);
                        }
                        else
                        {
                            tilemap.SetTileData(x, y, tileData[cx, cy]);
                        }
                    }
                }
                else
                {
                    tilemap.SetTileData(x, y, tileData[(x % w + w) % w, (y % h + h) % h]);
                }
                return(true);
            }
                                     );
        }
        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();
            }
        }
 void Reset()
 {
     ClearMap();
     m_material  = TilemapUtils.FindDefaultSpriteMaterial();
     m_tintColor = Color.white;
 }
        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);
        }