Пример #1
0
        public static void DoOnTilePrefabCreation(TilemapChunk.OnTilePrefabCreationData data, SpriteRenderer spriteRenderer, bool changeSpriteOnly)
        {
            Sprite tileSprite = GetOrCreateSprite(data);

            if (tileSprite)
            {
                spriteRenderer.sprite = tileSprite;
                if (!changeSpriteOnly)
                {
                    spriteRenderer.sortingLayerID = data.ParentTilemap.SortingLayerID;
                    spriteRenderer.sortingOrder   = data.ParentTilemap.OrderInLayer;
                }
            }
        }
        void OnTilePrefabCreation(TilemapChunk.OnTilePrefabCreationData data)
        {
            Tile tile = data.ParentTilemap.GetTile(data.GridX, data.GridY);

            if (tile != null)
            {
                float          pixelsPerUnit  = data.ParentTilemap.Tileset.TilePxSize.x / data.ParentTilemap.CellSize.x;
                Vector2        atlasSize      = new Vector2(data.ParentTilemap.Tileset.AtlasTexture.width, data.ParentTilemap.Tileset.AtlasTexture.height);
                Rect           spriteUV       = new Rect(Vector2.Scale(tile.uv.position, atlasSize), Vector2.Scale(tile.uv.size, atlasSize));
                SpriteRenderer spriteRenderer = GetComponent <SpriteRenderer>();
                spriteRenderer.sprite         = Sprite.Create(data.ParentTilemap.Tileset.AtlasTexture, spriteUV, new Vector2(.5f, .5f), pixelsPerUnit);
                spriteRenderer.sortingLayerID = data.ParentTilemap.SortingLayerID;
                spriteRenderer.sortingOrder   = data.ParentTilemap.OrderInLayer;
            }
        }
Пример #3
0
        public static Sprite GetOrCreateSprite(TilemapChunk.OnTilePrefabCreationData data)
        {
            Sprite sprite = null;
            int    tileId = Tileset.GetTileIdFromTileData(data.ParentTilemap.GetTileData(data.GridX, data.GridY));
            Tile   tile   = data.ParentTilemap.Tileset.GetTile(tileId);

            if (tile != null)
            {
                float   pixelsPerUnit = data.ParentTilemap.Tileset.TilePxSize.x / data.ParentTilemap.CellSize.x;
                Vector2 atlasSize     = new Vector2(data.ParentTilemap.Tileset.AtlasTexture.width, data.ParentTilemap.Tileset.AtlasTexture.height);
                Rect    spriteUV      = new Rect(Vector2.Scale(tile.uv.position, atlasSize), Vector2.Scale(tile.uv.size, atlasSize));
                string  spriteName    = data.ParentTilemap.Tileset.name + "_" + tileId + "_" + pixelsPerUnit;
                if (!s_spriteCache.TryGetValue(spriteName, out sprite) || !sprite)
                {
                    sprite      = Sprite.Create(data.ParentTilemap.Tileset.AtlasTexture, spriteUV, new Vector2(.5f, .5f), pixelsPerUnit);
                    sprite.name = spriteName;
                    s_spriteCache[spriteName] = sprite;
                }
            }
            return(sprite);
        }
Пример #4
0
        protected virtual void OnTilePrefabCreation(TilemapChunk.OnTilePrefabCreationData data)
        {
            Tile tile = data.ParentTilemap.GetTile(data.GridX, data.GridY);

            if (tile != null)
            {
                m_meshRenderer.material       = data.ParentTilemap.Material;
                m_meshRenderer.sortingLayerID = data.ParentTilemap.SortingLayerID;
                m_meshRenderer.sortingOrder   = data.ParentTilemap.OrderInLayer;
                Vector2   cellSizeDiv2 = data.ParentTilemap.CellSize / 2f;
                Vector3[] vertices     = new Vector3[4]
                {
                    new Vector3(-cellSizeDiv2.x, -cellSizeDiv2.y, 0),
                    new Vector3(cellSizeDiv2.x, -cellSizeDiv2.y, 0),
                    new Vector3(-cellSizeDiv2.x, cellSizeDiv2.y, 0),
                    new Vector3(cellSizeDiv2.x, cellSizeDiv2.y, 0),
                };
                int[]     triangles = new int[] { 3, 0, 2, 0, 3, 1 };
                Vector2[] uvs       = new Vector2[]
                {
                    new Vector2(tile.uv.xMin, tile.uv.yMin),
                    new Vector2(tile.uv.xMax, tile.uv.yMin),
                    new Vector2(tile.uv.xMin, tile.uv.yMax),
                    new Vector2(tile.uv.xMax, tile.uv.yMax),
                };

                if (!m_meshFilter.sharedMesh)
                {
                    m_meshFilter.sharedMesh = new Mesh();
                }
                m_meshFilter.sharedMesh.name = "Quad";
                Mesh mesh = m_meshFilter.sharedMesh;
                mesh.Clear();
                mesh.vertices  = vertices;
                mesh.triangles = triangles;
                mesh.uv        = uvs;
                mesh.RecalculateNormals();
            }
        }
Пример #5
0
 protected virtual void OnTilePrefabCreation(TilemapChunk.OnTilePrefabCreationData data)
 {
     SetRenderTile(data.ParentTilemap, data.ParentTilemap.GetTileData(data.GridX, data.GridY));
 }
Пример #6
0
 protected virtual void OnTilePrefabCreation(TilemapChunk.OnTilePrefabCreationData data)
 {
     DoOnTilePrefabCreation(data, GetComponent <SpriteRenderer>(), ChangeSpriteOnly);
 }