Esempio n. 1
0
        /// <summary>
        /// Save a texture asset
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static bool SaveTextureAsset(Texture2D texture)
        {
#if UNITY_EDITOR
            if (texture != null)
            {
                string filePath = AssetDatabase.GetAssetPath(texture);
                if (filePath.Length > 0)
                {
                    byte[] bytes = texture.EncodeToPNG();
                    File.WriteAllBytes(filePath, bytes);

                    // Make sure LoadAssetAtPath and ImportTexture is going to work
                    AssetDatabase.Refresh();

                    UtilsAutoTileMap.ImportTexture(filePath);
                }
                else
                {
                    return(false);
                }
            }
#endif
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Generate all needed data for the tileset
        /// </summary>
        public void GenerateAutoTileData( )
        {
            // force to generate atlas material if it is not instantiated
            if (AtlasMaterial == null)
            {
                //Debug.LogError( "GenerateAutoTileData error: missing AtlasMaterial" );
                //return;
                CreateAtlasMaterial();
            }

            BuildAtlasSlotData();
            BuildSubTilesetsList();

            _GenerateTilesetSprites();

            int tileNb = SubTilesets.Count * k_TilesPerSubTileset;

            if (AutotileCollType == null || tileNb != AutotileCollType.Length)
            {
                AutotileCollType = new eTileCollisionType[tileNb];
            }

            // get the mapped tileIdx ( for animated tile supporting. Animated tiles are considered as one, skipping the other 2 frames )
            // used only by sub tileset with autotiles
            AutotileIdxMap = new int[k_TilesPerSubTileset];
            int tileIdx = 0;

            for (int i = 0; i < k_TilesPerSubTileset; ++i)
            {
                AutotileIdxMap[i] = tileIdx;
                tileIdx          += (i >= 0 && i < 16 && (i % 2) == 0)? 3 : 1;
            }

            IsAutoTileHasAlpha = new bool[tileNb];
            ThumbnailRects     = new List <Rect>(tileNb);
            foreach (SubTilesetConf tilesetConf in SubTilesets)
            {
                UtilsAutoTileMap.FillWithTilesetThumbnailSprites(ThumbnailRects, this, tilesetConf);
            }

            //+++ sometimes png texture loose isReadable value. Maybe a unity bug?
        #if UNITY_EDITOR
            string          assetPath       = AssetDatabase.GetAssetPath(AtlasTexture);
            TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter != null && textureImporter.isReadable == false)
            {                   // reimport texture
                Debug.LogWarning("TilesetsAtlasTexture " + assetPath + " isReadable is false. Will be re-imported to access pixels.");
                UtilsAutoTileMap.ImportTexture(AtlasTexture);
            }
        #endif
            //---
            Color32[] aAtlasColors = AtlasTexture.GetPixels32();             //NOTE: Color32 is faster than Color in this alpha check

            for (int i = 0; i < ThumbnailRects.Count; ++i)
            {
                int subTilesetIdx = i / k_TilesPerSubTileset;
                if (SubTilesets[subTilesetIdx].HasAutotiles && (i % k_TilesPerSubTileset) >= 48 && (i % k_TilesPerSubTileset) < 128)
                {
                    // wall and building tiles has no alpha by default
                    IsAutoTileHasAlpha[i] = false;
                }
                else
                {
                    Rect sprTileRect = ThumbnailRects[i];
                    int  pBaseIdx    = (int)(sprTileRect.y * AtlasTexture.width + sprTileRect.x);
                    for (float py = 0; py < TileHeight && !IsAutoTileHasAlpha[i]; py++)
                    {
                        for (float px = 0; px < TileWidth && !IsAutoTileHasAlpha[i]; px++)
                        {
                            int pIdx = pBaseIdx + (int)(py * AtlasTexture.width + px);
                            if (aAtlasColors[pIdx].a < 255)
                            {
                                IsAutoTileHasAlpha[i] = true;
                            }
                        }
                    }
                }
            }
        #if UNITY_EDITOR
            EditorUtility.SetDirty(this);
        #endif
        }
        public override void OnInspectorGUI()
        {
            // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
            serializedObject.Update();

            EditorGUILayout.HelpBox("Select a texture atlas directly or Generate a new atlas using separated textures", MessageType.Info);
            Texture2D prevTexture = MyAutoTileset.AtlasTexture;

            MyAutoTileset.AtlasTexture = EditorGUILayout.ObjectField("Tileset Atlas", MyAutoTileset.AtlasTexture, typeof(Texture2D), false) as Texture2D;
            if (MyAutoTileset.AtlasTexture != null)
            {
                //NOTE: MyAutoTileset.SubTilesets.Count should be 0 when loading old Tileset below version 1.2.0 only
                if (prevTexture != MyAutoTileset.AtlasTexture || MyAutoTileset.SubTilesets.Count == 0)
                {
                    MyAutoTileset.GenerateAutoTileData();
                    EditorUtility.SetDirty(MyAutoTileset);
                }

                EditorGUILayout.HelpBox("Add tilesets to any free slot", MessageType.Info);
                if (GUILayout.Button("Edit Tileset..."))
                {
                    AutoTilesetEditorWindow.ShowDialog(MyAutoTileset, AutoTilesetEditorWindow.eEditMode.TilesetAtlas);
                }
            }
            else
            {
                EditorGUILayout.HelpBox("Set the number of slots for the atlas to be filled with normal or auto tilesets. If there are too many slots, texture atlas could be too big for some systems. Check the size of the texture while changing the values.", MessageType.Info);
                m_hSlots = EditorGUILayout.IntSlider("Horizontal Slots:", m_hSlots, 1, AutoTileset.k_MaxTextureSize / MyAutoTileset.TilesetSlotSize);
                m_vSlots = EditorGUILayout.IntSlider("Vertical Slots:", m_vSlots, 1, AutoTileset.k_MaxTextureSize / MyAutoTileset.TilesetSlotSize);
                m_hSlots = Mathf.Max(m_hSlots, 1);
                m_vSlots = Mathf.Max(m_vSlots, 1);
                EditorGUILayout.HelpBox("Set the size of the tile", MessageType.Info);
                MyAutoTileset.TileWidth = MyAutoTileset.TileHeight = EditorGUILayout.IntField("Tile Size In Pixels:", MyAutoTileset.TileWidth);
                EditorGUILayout.Space();
                if (GUILayout.Button("Generate Atlas of " + m_hSlots * MyAutoTileset.TilesetSlotSize + "x" + m_vSlots * MyAutoTileset.TilesetSlotSize))
                {
                    Texture2D atlasTexture = UtilsAutoTileMap.GenerateAtlas(MyAutoTileset, m_hSlots, m_vSlots);
                    if (atlasTexture)
                    {
                        string path     = Path.GetDirectoryName(AssetDatabase.GetAssetPath(MyAutoTileset));
                        string filePath = EditorUtility.SaveFilePanel("Save Atlas", path, MyAutoTileset.name + ".png", "png");
                        if (filePath.Length > 0)
                        {
                            byte[] bytes = atlasTexture.EncodeToPNG();
                            File.WriteAllBytes(filePath, bytes);

                            // make path relative to project
                            filePath = "Assets" + filePath.Remove(0, Application.dataPath.Length);

                            // Make sure LoadAssetAtPath and ImportTexture is going to work
                            AssetDatabase.Refresh();

                            UtilsAutoTileMap.ImportTexture(filePath);

                            // Link Atlas with asset to be able to save it in the prefab
                            MyAutoTileset.AtlasTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(filePath, typeof(Texture2D));
                            UtilsAutoTileMap.ImportTexture(MyAutoTileset.AtlasTexture);
                        }
                        else
                        {
                            MyAutoTileset.AtlasTexture = null;
                        }
                    }
                    MyAutoTileset.GenerateAutoTileData();
                    EditorUtility.SetDirty(MyAutoTileset);
                }
            }

            // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
            serializedObject.ApplyModifiedProperties();
        }