Example #1
0
        private Sprite GetCorrectSprite(int x, int y, int width, int height, TileSetSpriteInfo info)
        {
            Sprite s = info.CenterTileSprite;

            width  -= 1;
            height -= 1;

            if (x != 0 && x != width)
            {
                if (y == height)
                {
                    s = info.TopTileSprite;
                }
                else if (y == 0)
                {
                    s = info.BottomTileSprite;
                }
                else
                {
                    s = info.CenterTileSprite;
                }
            }
            else if (x == 0)
            {
                if (y == height)
                {
                    s = info.LeftTopTileSprite;
                }
                else if (y == 0)
                {
                    s = info.BottomLeftTileSprite;
                }
                else
                {
                    s = info.LeftTileSprite;
                }
            }
            else
            {
                if (y == height)
                {
                    s = info.RightTopTileSprite;
                }
                else if (y == 0)
                {
                    s = info.BottomRightTileSprite;
                }
                else
                {
                    s = info.RightTileSprite;
                }
            }
            return(s);
        }
Example #2
0
        public void GenerateTileSet(int width, int height, string setName, SpriteRenderer tilePrefab, TileSetSpriteInfo spritesInfo, int layer = -1)
        {
            SpriteRenderer tileObject = null;

            if (tilePrefab != null)
            {
                GameObject parent = new GameObject(setName + "(size[" + width + "x" + height + "])");
                for (int yRow = 0; yRow < height; yRow++)
                {
                    for (int xRow = 0; xRow < width; xRow++)
                    {
                        tileObject                    = GameObject.Instantiate <SpriteRenderer>(tilePrefab);
                        tileObject.sprite             = GetCorrectSprite(xRow, yRow, width, height, spritesInfo);
                        tileObject.transform.position = new Vector3(xRow * (tileObject.sprite.bounds.size.x * tilePrefab.transform.localScale.x), yRow * (tileObject.sprite.bounds.size.y * tilePrefab.transform.localScale.y));
                        tileObject.transform.SetParent(parent.transform);
                    }
                }
            }
            else
            {
                Debug.LogError("Please give a Tile Prefab to generate the tiles with");
            }
        }