void _OnSet(GridTriangle triangle, PositionRegionRenderer renderer, int x, int y, Tile tile)
        {
            TileInfo info = grid.atlas [tile.id];

            Clear(triangle, renderer, x, y);

            switch (info.shape)
            {
            case TileShape.UP_ONEWAY:
            case TileShape.RIGHT_ONEWAY:
            case TileShape.DOWN_ONEWAY:
            case TileShape.LEFT_ONEWAY:
            case TileShape.FULL: {
                renderer.mesh.SetTile(x - renderer.regionX * Grid.REGION_SIZE, y - renderer.regionY * Grid.REGION_SIZE, info.GetSprite(tile.subId));
                break;
            }

            case TileShape.DOWN_LEFT_TRIANGLE:
            case TileShape.DOWN_RIGHT_TRIANGLE:
            case TileShape.UP_LEFT_TRIANGLE:
            case TileShape.UP_RIGHT_TRIANGLE: {
                JoinTriangle(renderer, info, tile, x, y);
                break;
            }
            }
        }
        public override void OnSet(int x, int y, Tile tile)
        {
            PositionRegionRenderer renderer = GetContainingRegionRenderer(x, y);
            GridTriangle           triangle = triangles.Get(x, y) as GridTriangle;

            ClearRenderers();
            renderer.mesh.PrepareUV();

            _OnSet(triangle, renderer, x, y, tile);

            renderer.mesh.ApplyUV();
            ApplyRenderers();
        }
        public static GridTriangle CreateTriangle(GameObject parent, Grid grid, int id, int subId, bool vertical, int x, int y, int width, int height)
        {
            GameObject obj = new GameObject("sprite x=" + x + " y=" + y);

            obj.transform.SetParent(parent.transform);
            obj.transform.localPosition = grid.TileCenterInTransform(x, y);
            obj.transform.localScale    = new Vector3(1, 1, 1);

            GridTriangle triangle = obj.AddComponent <GridTriangle> ();

            triangle.id         = id;
            triangle.subId      = subId;
            triangle.isVertical = vertical;

            triangle.width       = width;
            triangle.height      = height;
            triangle.bottomLeftX = x;
            triangle.bottomLeftY = y;

            return(triangle);
        }
        void JoinTriangle(PositionRegionRenderer currentRenderer, TileInfo info, Tile tile, int x, int y)
        {
            bool isExpanded = false;

            int relX = x - currentRenderer.regionX * Grid.REGION_SIZE;
            int relY = y - currentRenderer.regionY * Grid.REGION_SIZE;

            if (info.isVertical)
            {
                GridTriangle down = triangles.Get(x, y - 1) as GridTriangle;
                GridTriangle up   = triangles.Get(x, y + 1) as GridTriangle;

                if (down != null && down.id == tile.id && down.subId == tile.subId)
                {
                    down.height++;

                    triangles.Set(x, y, down);
                    isExpanded = true;
                }

                if (up != null && up.id == tile.id && up.subId == tile.subId)
                {
                    if (isExpanded)
                    {
                        down.height += up.height;

                        for (int i = y + 1; i <= y + up.height; i++)
                        {
                            triangles.Set(x, i, down);
                        }
                    }
                    else
                    {
                        up.height++;
                        up.bottomLeftY--;

                        triangles.Set(x, y, up);
                        isExpanded = true;
                    }
                }

                if (isExpanded)
                {
                    if (down != null)
                    {
                        int    actualSize;
                        Sprite sprite     = info.GetSprite(out actualSize, down.subId, down.height);
                        int    relBottomY = down.bottomLeftY - currentRenderer.regionY * Grid.REGION_SIZE;

                        if (relBottomY > Grid.REGION_SIZE / 2)
                        {
                            RenderUpTriangle(currentRenderer, relX, relBottomY, down.height, actualSize, sprite);
                        }
                        else
                        {
                            RenderDownTriangle(currentRenderer, relX, relBottomY, down.height, actualSize, sprite);
                        }
                    }
                    else if (up != null)
                    {
                        int    actualSize;
                        Sprite sprite     = info.GetSprite(out actualSize, up.subId, up.height);
                        int    relBottomY = up.bottomLeftY - currentRenderer.regionY * Grid.REGION_SIZE;

                        if (relBottomY > Grid.REGION_SIZE / 2)
                        {
                            RenderUpTriangle(currentRenderer, relX, relBottomY, up.height, actualSize, sprite);
                        }
                        else
                        {
                            RenderDownTriangle(currentRenderer, relX, relBottomY, up.height, actualSize, sprite);
                        }
                    }
                }
            }
            else
            {
                GridTriangle left  = triangles.Get(x - 1, y) as GridTriangle;
                GridTriangle right = triangles.Get(x + 1, y) as GridTriangle;

                if (left != null && left.id == tile.id && left.subId == tile.subId)
                {
                    left.width++;

                    triangles.Set(x, y, left);
                    isExpanded = true;
                }

                if (right != null && right.id == tile.id && right.subId == tile.subId)
                {
                    if (isExpanded)
                    {
                        left.width += right.width;

                        for (int i = x + 1; i <= x + right.width; i++)
                        {
                            triangles.Set(i, y, left);
                        }
                    }
                    else
                    {
                        right.width++;
                        right.bottomLeftX--;

                        triangles.Set(x, y, right);
                        isExpanded = true;
                    }
                }

                if (isExpanded)
                {
                    if (left != null)
                    {
                        int    actualSize;
                        Sprite sprite     = info.GetSprite(out actualSize, left.subId, left.width);
                        int    relBottomX = left.bottomLeftX - currentRenderer.regionX * Grid.REGION_SIZE;

                        if (relBottomX > Grid.REGION_SIZE / 2)
                        {
                            RenderRightTriangle(currentRenderer, relBottomX, relY, left.width, actualSize, sprite);
                        }
                        else
                        {
                            RenderLeftTriangle(currentRenderer, relBottomX, relY, left.width, actualSize, sprite);
                        }
                    }
                    else if (right != null)
                    {
                        int    actualSize;
                        Sprite sprite     = info.GetSprite(out actualSize, right.subId, right.width);
                        int    relBottomX = right.bottomLeftX - currentRenderer.regionX * Grid.REGION_SIZE;

                        if (relBottomX > Grid.REGION_SIZE / 2)
                        {
                            RenderRightTriangle(currentRenderer, relBottomX, relY, right.width, actualSize, sprite);
                        }
                        else
                        {
                            RenderLeftTriangle(currentRenderer, relBottomX, relY, right.width, actualSize, sprite);
                        }
                    }
                }
            }

            if (!isExpanded)
            {
                triangles.Set(x, y, GridTriangle.CreateTriangle(containerGO, grid, tile.id, tile.subId, info.isVertical, x, y, 1, 1));

                currentRenderer.mesh.SetTile(relX, relY, info.GetSprite(tile.subId));
            }
        }
        void Clear(GridTriangle triangle, PositionRegionRenderer currentRenderer, int x, int y)
        {
            int relX = x - currentRenderer.regionX * Grid.REGION_SIZE;
            int relY = y - currentRenderer.regionY * Grid.REGION_SIZE;

            if (triangle != null)
            {
                TileInfo info = grid.atlas [triangle.id];
                int      actualSize;

                if (triangle.isVertical)
                {
                    int bottomRelY = triangle.bottomLeftY - currentRenderer.regionY * Grid.REGION_SIZE;

                    if (y == triangle.bottomLeftY)
                    {
                        if (triangle.height == 1)
                        {
                            DestroyImmediate(triangle.gameObject);
                        }
                        else
                        {
                            triangle.height      -= 1;
                            triangle.bottomLeftY += 1;

                            Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.height);
                            RenderUpTriangle(currentRenderer, relX, bottomRelY + 1, triangle.height, actualSize, sprite);
                        }
                    }
                    else if (y == triangle.bottomLeftY + triangle.height - 1)
                    {
                        triangle.height -= 1;

                        Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.height);
                        RenderDownTriangle(currentRenderer, relX, bottomRelY, triangle.height, actualSize, sprite);
                    }
                    else
                    {
                        GridTriangle other = GridTriangle.CreateTriangle(
                            containerGO,
                            grid,
                            triangle.id, triangle.subId, true,
                            triangle.bottomLeftX, y + 1,
                            1, triangle.height - (y + 1 - triangle.bottomLeftY)
                            );

                        triangle.height = y - triangle.bottomLeftY;

                        Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.height);
                        RenderDownTriangle(currentRenderer, relX, bottomRelY, triangle.height, actualSize, sprite);

                        sprite     = info.GetSprite(out actualSize, other.subId, other.height);
                        bottomRelY = other.bottomLeftY - currentRenderer.regionY * Grid.REGION_SIZE;
                        for (int i = 0; i < other.height; i++)
                        {
                            triangles.Set(other.bottomLeftX, other.bottomLeftY + i, other);
                        }
                        RenderUpTriangle(currentRenderer, relX, bottomRelY, other.height, actualSize, sprite);
                    }
                }
                else
                {
                    int bottomRelX = triangle.bottomLeftX - currentRenderer.regionX * Grid.REGION_SIZE;

                    if (x == triangle.bottomLeftX)
                    {
                        if (triangle.width == 1)
                        {
                            DestroyImmediate(triangle.gameObject);
                        }
                        else
                        {
                            triangle.width       -= 1;
                            triangle.bottomLeftX += 1;

                            Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.width);
                            RenderRightTriangle(currentRenderer, bottomRelX + 1, relY, triangle.width, actualSize, sprite);
                        }
                    }
                    else if (x == triangle.bottomLeftX + triangle.width - 1)
                    {
                        triangle.width -= 1;

                        Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.width);
                        RenderLeftTriangle(currentRenderer, bottomRelX, relY, triangle.width, actualSize, sprite);
                    }
                    else
                    {
                        GridTriangle other = GridTriangle.CreateTriangle(
                            containerGO,
                            grid,
                            triangle.id, triangle.subId, false,
                            x + 1, triangle.bottomLeftY,
                            triangle.width - (x + 1 - triangle.bottomLeftX), 1
                            );

                        triangle.width = x - triangle.bottomLeftX;

                        Sprite sprite = info.GetSprite(out actualSize, triangle.subId, triangle.width);
                        RenderLeftTriangle(currentRenderer, bottomRelX, relY, triangle.width, actualSize, sprite);

                        sprite     = info.GetSprite(out actualSize, other.subId, other.width);
                        bottomRelX = other.bottomLeftX - currentRenderer.regionX * Grid.REGION_SIZE;
                        for (int i = 0; i < other.width; i++)
                        {
                            triangles.Set(other.bottomLeftX + i, other.bottomLeftY, other);
                        }
                        RenderRightTriangle(currentRenderer, bottomRelX, relY, other.width, actualSize, sprite);
                    }
                }

                triangles.Set(x, y, null);
            }

            currentRenderer.mesh.Clear(relX, relY);
        }
 public bool IsCompatible(GridTriangle other)
 {
     return(id == other.id && subId == other.subId);
 }