Beispiel #1
0
        /// <summary>
        /// Update the Texture2D used as an array in the shader.
        /// </summary>
        private void UpdateLayerDataArray()
        {
            int width = tilemapLayerList.Count;

            Color32[]     layerData   = new Color32[width];
            TilePalette[] paletteList = new TilePalette[width];

            for (int i = 0; i < width; i++)
            {
                paletteList[i] = tilemapLayerList[i].tilePalette;
                if (paletteList[i])
                {
                    layerData[i].r = (byte)paletteList[i].tilesCount;
                }
                layerData[i].g = (byte)(tilemapLayerList[i].intensity * 255);
            }

            DestroyImmediate(layerArrayTexture, true);

            layerArrayTexture = new Texture2D(width, 1, TextureFormat.RGBA32, false)
            {
                name       = "Layer Array Texture",
                filterMode = FilterMode.Point,
                wrapMode   = TextureWrapMode.Clamp,
                anisoLevel = 0
            };

            // Apply the changes to the layer data texture
            layerArrayTexture.SetPixels32(layerData);
            layerArrayTexture.Apply();

            // Save the layer data texture as .asset file
            AssetDatabase.AddObjectToAsset(layerArrayTexture, tilemapData);
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(layerArrayTexture));
        }
Beispiel #2
0
        /// <summary>
        /// Updates the 3D Tileset texture when any important settings are changed.
        /// </summary>
        private void UpdateTilesetTexture()
        {
            // Get the tile palette of each layer
            TilePalette[] paletteList = new TilePalette[tilemapLayerList.Count];
            for (int i = 0; i < tilemapLayerList.Count; i++)
            {
                paletteList[i] = tilemapLayerList[i].tilePalette;
            }

            // Get the total number of tiles in all the palettes
            var tilesCount = 0;

            for (var i = 0; i < paletteList.Length; i++)
            {
                if (paletteList[i])
                {
                    tilesCount += paletteList[i].tilesCount;

                    // Return if the tile palette is empty
                    if (paletteList[i].tilesCount == 0)
                    {
                        Debug.LogWarning("<b>3D Tilemap System:</b> Can't pack the 3D Tileset texture because the Tile Palette field of the Layer" + i + " is empty. Please, add the tiles to the Tile Palette first and try to regenerate the Tilemap Data again.");
                        return;
                    }

                    // Return if the tiles size in the palette do not match with the 3D Tileset texture size
                    if (paletteList[i].tileSize != tilesetSize)
                    {
                        Debug.LogWarning("<b>3D Tilemap System:</b> Can't pack the 3D Tileset texture because the tiles size in the Tile Palette of the Layer" + i + " do not match with the 3D Tileset texture size.");
                        return;
                    }
                }
                else
                {
                    // Return if there is no tile palette attached to some layer
                    Debug.LogWarning("<b>3D Tilemap System:</b> Can't pack the 3D Tileset texture because the Tile Palette field of the Layer" + i + " is null. Please, attach a Tile Palette to it and try to regenerate the Tilemap Data again.");
                    return;
                }
            }

            // Delete the existing 3D Tileset texture before creating a new one
            DestroyImmediate(tilesetTexture, true);

            // Initialize the tileset texture
            tilesetTexture = new Texture3D(tilesetSize, tilesetSize, tilesCount + 1, TextureFormat.RGBA32, false)
            {
                name       = "Tileset Texture",
                filterMode = FilterMode.Point,
                wrapMode   = TextureWrapMode.Repeat,
                anisoLevel = 0
            };

            // Pack the tiles from paletteList[] to Texture3D
            Color[] tilesetColor = tilesetTexture.GetPixels();
            int     layer        = 0;
            int     index        = 0;
            int     tileSize     = tilesetTexture.width;

            for (int z = 0; z < tilesCount; z++)
            {
                var tileColor = paletteList[layer].temporaryTileTextureArray[index].GetPixels32();

                for (int y = 0; y < tileSize; y++)
                {
                    for (int x = 0; x < tileSize; x++)
                    {
                        tilesetColor[x + (y * tileSize) + (z * tileSize * tileSize)] = tileColor[x + (y * tileSize)];
                    }
                }

                index++;
                if (index >= paletteList[layer].tilesCount)
                {
                    index = 0;
                    layer++;
                }
            }

            // Apply the changes to the tileset texture
            tilesetTexture.SetPixels(tilesetColor);
            tilesetTexture.Apply();

            // Save the tileset texture as .asset file
            AssetDatabase.AddObjectToAsset(tilesetTexture, tilemapData);
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tilesetTexture));
        }