示例#1
0
        /// <summary>
        /// Save a texture asset
        /// </summary>
        /// <param name="texture"></param>
        /// <returns></returns>
        public bool SaveTextureAsset(Texture2D texture) //NOTE: this was moved from UtilsAutoTileMap because File.WriteAllBytes is not found for WebPlayer settings otherwise
        {
#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);
        }
示例#2
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);
        }
示例#3
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
        }
示例#4
0
        void OnGUI_TilesetAtlas(bool isFirstUpdate)
        {
            GUILayout.Label("Tileset Atlas Configuration", EditorStyles.boldLabel);

            GUILayout.BeginHorizontal();
            GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.AddAutoTileset? Color.gray : Color.white;
            if (GUILayout.Button("Add AutoTileset", GUILayout.Height(25)))
            {
                m_atlasEditMode = eAtlasEditMode.AddAutoTileset;
            }
            GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.AddNormalTileset ? Color.gray : Color.white;
            if (GUILayout.Button("Add NormalTileset", GUILayout.Height(25)))
            {
                m_atlasEditMode = eAtlasEditMode.AddNormalTileset;
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.EditTileset ? Color.gray : Color.white;
            if (GUILayout.Button("Edit Tileset", GUILayout.Height(25)))
            {
                m_atlasEditMode = eAtlasEditMode.EditTileset;
            }
            GUI.backgroundColor = m_atlasEditMode == eAtlasEditMode.RemoveTileset ? Color.gray : Color.white;
            if (GUILayout.Button("Remove Tileset", GUILayout.Height(25)))
            {
                m_atlasEditMode = eAtlasEditMode.RemoveTileset;
            }
            GUI.backgroundColor = Color.white;
            GUILayout.EndHorizontal();

            int cols = m_autoTileset.AtlasTexture.width / m_autoTileset.TilesetSlotSize;
            //int rows = m_autoTileset.AtlasTexture.height / k_TilesetSlotSize;

            Rect rAtlas = new Rect(0, 0, m_autoTileset.AtlasTexture.width, m_autoTileset.AtlasTexture.height);

            if (rAtlas.width - position.width > rAtlas.height - (position.height - 120f))
            {
                rAtlas.width   = Mathf.Min(position.width, rAtlas.width);
                rAtlas.height *= rAtlas.width / m_autoTileset.AtlasTexture.width;
            }
            else
            {
                rAtlas.height = Mathf.Min((position.height - 120f), rAtlas.height);
                rAtlas.width *= rAtlas.height / m_autoTileset.AtlasTexture.height;
            }

            GUILayout.Box(m_autoTileset.AtlasTexture, GUILayout.Width(rAtlas.width), GUILayout.Height(rAtlas.height));

            Rect     rGuiAtlas = GUILayoutUtility.GetLastRect();
            Vector2  vBtnOff   = new Vector2(0.2f * rGuiAtlas.width / cols, 0.40f * rGuiAtlas.width / cols);
            Rect     rButton   = new Rect(0, 0, rGuiAtlas.width / cols - 2f * vBtnOff.x, rGuiAtlas.width / cols - 2f * vBtnOff.y);
            float    scale     = rAtlas.width / m_autoTileset.AtlasTexture.width;
            GUIStyle btnStyle  = new GUIStyle("button");

            btnStyle.richText  = true;
            btnStyle.fontSize  = (int)(50 * scale);
            btnStyle.fontStyle = FontStyle.Bold;

            for (int i = 0; i < m_autoTileset.AtlasSlots.Count; ++i)
            {
                AtlasSlot atlasSlot = m_autoTileset.AtlasSlots[i];
                Rect      rSlot     = m_autoTileset.CalculateAtlasSlotRectByIdx(i);
                rButton.position = vBtnOff + rGuiAtlas.position + rSlot.position * scale;
                rButton.y        = rGuiAtlas.y + rGuiAtlas.height - (rButton.y - rGuiAtlas.y) - rButton.height;

                if (atlasSlot.SubTilesets.Count == 0)
                {
                    GUI.color = new Color(0f, 0.7f, 1f, 0.8f);
                    Rect rSlotBox = rSlot;
                    rSlotBox.position *= scale;
                    rSlotBox.position += rGuiAtlas.position;
                    rSlotBox.width    *= scale;
                    rSlotBox.height   *= scale;
                    rSlotBox.y         = rGuiAtlas.y + rGuiAtlas.height - (rSlotBox.y - rGuiAtlas.y) - rSlotBox.height;
                    GUI.Box(rSlotBox, "\n\nSlot " + (i + 1));
                }

                GUI.color = new Color(0f, 0.7f, 1f, 0.8f);

                //+++ Show name list
                if (m_atlasEditMode == eAtlasEditMode.None)
                {
                    if (atlasSlot.SubTilesets.Count == 1)
                    {
                        List <string> availableNames = m_autoTileset.CreateAvailableNameList();
                        availableNames.Insert(0, atlasSlot.SubTilesets[0].Name);
                        Rect rPopup  = new Rect(rButton.x, rButton.y, 50, 20); rPopup.position -= vBtnOff;
                        int  nameIdx = EditorGUI.Popup(rPopup, 0, availableNames.ToArray(), new GUIStyle("popup"));
                        atlasSlot.SubTilesets[0].Name = availableNames[nameIdx];
                    }
                    else
                    {
                        for (int j = 0; j < atlasSlot.SubTilesets.Count; ++j)
                        {
                            List <string> availableNames = m_autoTileset.CreateAvailableNameList();
                            availableNames.Insert(0, atlasSlot.SubTilesets[j].Name);
                            Rect rPopup = new Rect(rButton.x, rButton.y, 50, 20); rPopup.position -= vBtnOff;
                            if (j == 0)
                            {
                                rPopup.y += rSlot.height * scale / 2;
                            }
                            else if (j == 2)
                            {
                                rPopup.y += rSlot.height * scale / 2;
                                rPopup.x += rSlot.width * scale / 2;
                            }
                            else if (j == 3)
                            {
                                rPopup.x += rSlot.width * scale / 2;
                            }
                            int nameIdx = EditorGUI.Popup(rPopup, 0, availableNames.ToArray(), new GUIStyle("popup"));
                            atlasSlot.SubTilesets[j].Name = availableNames[nameIdx];
                        }
                    }
                }
                //---

                GUI.color = new Color(.5f, .5f, 0.5f, 0.7f);

                if (m_atlasEditMode == eAtlasEditMode.EditTileset && atlasSlot.SubTilesets.Count >= 1)
                {
                    if (GUI.Button(rButton, "<color=white>" + (atlasSlot.SubTilesets.Count > 1 ? "[+]" : "[A]") + "Edit</color>", btnStyle))
                    {
                        m_editAtlasSlot     = atlasSlot;
                        m_editMode          = eEditMode.ChangeTileset;
                        m_clearSlotOnCancel = false;
                        m_atlasEditMode     = eAtlasEditMode.None;
                    }
                }
                else if (m_atlasEditMode == eAtlasEditMode.RemoveTileset && atlasSlot.SubTilesets.Count >= 1)
                {
                    if (GUI.Button(rButton, "<color=white>" + (atlasSlot.SubTilesets.Count > 1 ? "[+]" : "[A]") + "Remove</color>", btnStyle))
                    {
                        foreach (SubTilesetConf conf in atlasSlot.SubTilesets)
                        {
                            UtilsAutoTileMap.ClearAtlasArea(m_autoTileset.AtlasTexture, (int)conf.AtlasRec.x, (int)conf.AtlasRec.y, (int)conf.AtlasRec.width, (int)conf.AtlasRec.height);
                        }
                        atlasSlot.SubTilesets.Clear();
                        m_autoTileset.BuildSubTilesetsList();
                        EditorUtility.SetDirty(m_autoTileset);
                        if (m_autoTileset.SubTilesets.Count == 0)
                        {
                            m_atlasEditMode = eAtlasEditMode.None;
                        }
                    }
                }
                else if (m_atlasEditMode == eAtlasEditMode.AddAutoTileset && atlasSlot.SubTilesets.Count == 0)
                {
                    if (GUI.Button(rButton, "<color=white>Add AutoTileset</color>", btnStyle))
                    {
                        List <string> availableNames = m_autoTileset.CreateAvailableNameList();
                        atlasSlot.SubTilesets.Add(new SubTilesetConf()
                        {
                            Name = availableNames.Count > 0 ? availableNames[0] : "-", AtlasRec = rSlot, HasAutotiles = true
                        });
                        m_autoTileset.BuildSubTilesetsList();
                        m_editAtlasSlot     = atlasSlot;
                        m_editMode          = eEditMode.ChangeTileset;
                        m_clearSlotOnCancel = true;
                        EditorUtility.SetDirty(m_autoTileset);
                    }
                }
                else if (m_atlasEditMode == eAtlasEditMode.AddNormalTileset && atlasSlot.SubTilesets.Count == 0)
                {
                    if (GUI.Button(rButton, "<color=white>Add Normal Tileset</color>", btnStyle))
                    {
                        List <string> availableNames = m_autoTileset.CreateAvailableNameList();
                        Rect          rect           = new Rect(rSlot.x, rSlot.y, rSlot.width / 2, rSlot.height / 2);
                        atlasSlot.SubTilesets.Add(new SubTilesetConf()
                        {
                            Name = availableNames.Count > 0 ? availableNames[0] : "-", AtlasRec = rect, HasAutotiles = false
                        });
                        rect.y = rSlot.y + rect.height;
                        atlasSlot.SubTilesets.Add(new SubTilesetConf()
                        {
                            Name = availableNames.Count > 1 ? availableNames[1] : "-", AtlasRec = rect, HasAutotiles = false
                        });
                        rect.x = rSlot.x + rect.width;
                        rect.y = rSlot.y;
                        atlasSlot.SubTilesets.Add(new SubTilesetConf()
                        {
                            Name = availableNames.Count > 2 ? availableNames[2] : "-", AtlasRec = rect, HasAutotiles = false
                        });
                        rect.y = rSlot.y + rect.height;
                        atlasSlot.SubTilesets.Add(new SubTilesetConf()
                        {
                            Name = availableNames.Count > 3 ? availableNames[3] : "-", AtlasRec = rect, HasAutotiles = false
                        });
                        m_autoTileset.BuildSubTilesetsList();
                        m_editAtlasSlot     = atlasSlot;
                        m_editMode          = eEditMode.ChangeTileset;
                        m_clearSlotOnCancel = true;
                        EditorUtility.SetDirty(m_autoTileset);
                    }
                }
            }
            GUI.color = Color.white;
            if (m_atlasEditMode != eAtlasEditMode.None && GUILayout.Button("Cancel Action", GUILayout.Height(25)))
            {
                m_atlasEditMode = eAtlasEditMode.None;
            }
        }
示例#5
0
        void OnGUI_Collisions(bool isFirstUpdate)
        {
            GUILayout.Label("Tileset Collision Configuration", EditorStyles.boldLabel);

            if (m_autoTileset.AtlasTexture == null)
            {
                Selection.activeObject = m_autoTileset;
                GUILayout.Label("You have to create a texture atlas first");
                return;
            }

            GUILayout.BeginHorizontal();

            if (GUILayout.Button("Default Configuration"))
            {
                bool isOk = EditorUtility.DisplayDialog("Set Default Collision Data", "Are you sure?", "Yes", "No");
                if (isOk)
                {
                    SetDefaultConfig();
                    EditorUtility.SetDirty(m_autoTileset);
                }
            }

            if (GUILayout.Button("Clear"))
            {
                bool isOk = EditorUtility.DisplayDialog("Clear Collision Data", "Are you sure?", "Yes", "No");
                if (isOk)
                {
                    System.Array.Clear(m_autoTileset.AutotileCollType, m_subTilesetIdx * AutoTileset.k_TilesPerSubTileset, AutoTileset.k_TilesPerSubTileset);
                    EditorUtility.SetDirty(m_autoTileset);
                }
            }

            GUILayout.EndHorizontal();

            EditorGUILayout.HelpBox("Press left or right mouse button over the tiles to change the collision type. You can do this also while editing a map in the editor or while playing.", MessageType.Info);

            // refresh data if needed
            if (isFirstUpdate)
            {
                m_autoTileset.GenerateAutoTileData(); // be sure to data is ok before editing in case of some modification was made
                _refreshSubTilesetNames();
                m_subTilesetIdx = Mathf.Clamp(m_subTilesetIdx, 0, m_autoTileset.SubTilesets.Count);
            }
            m_subTilesetIdx = EditorGUILayout.Popup("Tileset: ", m_subTilesetIdx, m_subTilesetNames);

            if (GUI.changed || m_tilesetTexture == null)
            {
                m_tilesetTexture = UtilsAutoTileMap.GenerateTilesetTexture(m_autoTileset, m_autoTileset.SubTilesets[m_subTilesetIdx]);
            }

            float fScrollBarWidth = 16f;
            float fScale          = 32f / m_autoTileset.TileWidth; // scale texture to have the same size as when tile size was 32x32
            Rect  rTileset        = new Rect(0f, 0f, (float)m_tilesetTexture.width * fScale, (float)m_tilesetTexture.height * fScale);
            Rect  rScrollView     = new Rect(50, 140, rTileset.width + fScrollBarWidth, position.height - 150);

            if (m_tilesetTexture != null)
            {
                Rect rTile = new Rect(0, 0, 32, 32);
                // BeginScrollView
                m_scrollPos = GUI.BeginScrollView(rScrollView, m_scrollPos, rTileset);
                {
                    GUI.DrawTexture(rTileset, m_tilesetTexture);

                    for (int autoTileLocalIdx = 0; autoTileLocalIdx < 256; ++autoTileLocalIdx)                      //autoTileLocalIdx: index of current tileset group
                    {
                        rTile.x = rTileset.x + (autoTileLocalIdx % m_autoTileset.AutoTilesPerRow) * 32;
                        rTile.y = rTileset.y + (autoTileLocalIdx / m_autoTileset.AutoTilesPerRow) * 32;

                        int autoTileIdx = autoTileLocalIdx + (int)m_subTilesetIdx * 256;                         // global autotile idx
                        if (Event.current.type == EventType.MouseUp)
                        {
                            if (rTile.Contains(Event.current.mousePosition))
                            {
                                int collType = (int)m_autoTileset.AutotileCollType[autoTileIdx];
                                if (Event.current.button == 0)
                                {
                                    collType += 1;                                     // go next
                                }
                                else if (Event.current.button == 1)
                                {
                                    collType += (int)eTileCollisionType._SIZE - 1;                                     // go back
                                }
                                collType %= (int)eTileCollisionType._SIZE;
                                m_autoTileset.AutotileCollType[autoTileIdx] = (eTileCollisionType)(collType);
                            }
                            EditorUtility.SetDirty(m_autoTileset);
                        }

                        string sCollision = "";
                        switch (m_autoTileset.AutotileCollType[autoTileIdx])
                        {
                        case eTileCollisionType.BLOCK: sCollision = "■"; break;

                        case eTileCollisionType.FENCE: sCollision = "#"; break;

                        case eTileCollisionType.WALL: sCollision = "□"; break;

                        case eTileCollisionType.OVERLAY: sCollision = "★"; break;
                        }
                        if (sCollision.Length > 0)
                        {
                            GUI.color = new Color(1f, 1f, 1f, 1f);
                            GUIStyle style = new GUIStyle();
                            style.fontSize         = 30;
                            style.fontStyle        = FontStyle.Bold;
                            style.alignment        = TextAnchor.MiddleCenter;
                            style.normal.textColor = Color.white;
                            GUI.Box(rTile, sCollision, style);
                            GUI.color = Color.white;
                        }

                        //Show alpha tiles (debug)

                        /*/
                         * if( m_autoTileset.IsAutoTileHasAlpha[autoTileIdx] )
                         * {
                         *      GUIStyle style = new GUIStyle();
                         *      style.fontSize = 30;
                         *      style.fontStyle = FontStyle.Bold;
                         *      style.alignment = TextAnchor.MiddleCenter;
                         *      style.normal.textColor = Color.blue;
                         *      GUI.Box( rTile, "A", style );
                         * }
                         * //*/
                    }
                }
                GUI.EndScrollView();

                // Help Info
                {
                    GUIStyle style = new GUIStyle();
                    style.fontSize  = 15;
                    style.fontStyle = FontStyle.Bold;
                    //NOTE: if you don't see the special characters properly, be sure this file is saved in UTF-8
                    GUI.Label(new Rect(rScrollView.xMax + 30, rScrollView.y + 40, 100, 100), "■ - Block Collision", style);
                    GUI.Label(new Rect(rScrollView.xMax + 30, rScrollView.y + 60, 100, 100), "□ - Wall Collision", style);
                    GUI.Label(new Rect(rScrollView.xMax + 30, rScrollView.y + 80, 100, 100), "# - Fence Collision", style);
                    GUI.Label(new Rect(rScrollView.xMax + 30, rScrollView.y + 100, 100, 100), "★ - Overlay", style);
                }

                Repaint();
            }
        }
示例#6
0
        void OnGUI_ChangeTileset(bool isFirstUpdate)
        {
            GUILayout.Label("Sub Tileset Configuration", EditorStyles.boldLabel);
            GUILayout.Space(20);
            GUILayout.Label("Tile Size: " + m_autoTileset.TileWidth);
            m_changeTilesetScrollPos = GUILayout.BeginScrollView(m_changeTilesetScrollPos);

            if (m_editAtlasSlot == null || m_editAtlasSlot.SubTilesets.Count == 0)
            {
                EditorGUILayout.HelpBox("There is no tileset selected! Go to TilesetAtlas tab, press Edit Tileset button and select a tileset to be edited.", MessageType.Warning);
                if (GUILayout.Button("Cancel"))
                {
                    m_editMode = eEditMode.TilesetAtlas;
                    if (m_clearSlotOnCancel)
                    {
                        m_clearSlotOnCancel = false;
                        m_editAtlasSlot.SubTilesets.Clear();
                    }
                }
            }
            else
            {
                GUILayout.Space(20);
                if (GUILayout.Button("Accept", GUILayout.Height(25)))
                {
                    if (_validateTilesetTextures(m_editAtlasSlot))
                    {
                        foreach (SubTilesetConf tilesetConf in m_editAtlasSlot.SubTilesets)
                        {
                            UtilsAutoTileMap.CopySubTilesetInAtlas(m_autoTileset, tilesetConf);
                        }
                        m_autoTileset.AtlasTexture.Apply();
                        SaveTextureAsset(m_autoTileset.AtlasTexture);
                        m_autoTileset.GenerateAutoTileData();
                        EditorUtility.SetDirty(m_autoTileset);
                        m_editMode = eEditMode.TilesetAtlas;
                    }
                }
                GUILayout.Space(20);
                if (GUILayout.Button("Cancel", GUILayout.Height(25)))
                {
                    m_editMode = eEditMode.TilesetAtlas;
                    if (m_clearSlotOnCancel)
                    {
                        m_clearSlotOnCancel = false;
                        m_editAtlasSlot.SubTilesets.Clear();
                    }
                }
                GUILayout.Space(20);

                EditorGUILayout.BeginVertical(GUILayout.MinWidth(300));
                if (m_editAtlasSlot.SubTilesets.Count == 1)
                { // if it is an autotile tilesets, it's made of 5 textures
                    for (int i = 0; i < m_editAtlasSlot.SubTilesets[0].SourceTexture.Length; ++i)
                    {
                        EditorGUILayout.LabelField(_GetSrcTextureName(i, m_editAtlasSlot.SubTilesets[0].HasAutotiles));
                        m_editAtlasSlot.SubTilesets[0].SourceTexture[i] = EditorGUILayout.ObjectField(m_editAtlasSlot.SubTilesets[0].SourceTexture[i] == null ? "" : m_editAtlasSlot.SubTilesets[0].SourceTexture[i].name, m_editAtlasSlot.SubTilesets[0].SourceTexture[i], typeof(Texture2D), false) as Texture2D;
                    }
                }
                else
                { // if it is an object tileset, we have to modify this and the other 3 as object tilesets came in groups of 4
                    for (int i = 0; i < m_editAtlasSlot.SubTilesets.Count; ++i)
                    {
                        EditorGUILayout.LabelField(_GetSrcTextureName(i, m_editAtlasSlot.SubTilesets[i].HasAutotiles));
                        m_editAtlasSlot.SubTilesets[i].SourceTexture[0] = EditorGUILayout.ObjectField(m_editAtlasSlot.SubTilesets[i].SourceTexture[0] == null ? "" : m_editAtlasSlot.SubTilesets[i].SourceTexture[0].name, m_editAtlasSlot.SubTilesets[i].SourceTexture[0], typeof(Texture2D), false) as Texture2D;
                    }
                }
                EditorGUILayout.EndVertical();
            }

            GUILayout.EndScrollView();
        }
        // Use this for initialization
        public void Init()
        {
            m_autoTileMap = GetComponent <AutoTileMap>();

            if (m_autoTileMap != null && m_autoTileMap.IsInitialized)
            {
                m_isInitialized = true;

                m_tileGroupNames = new string[m_autoTileMap.Tileset.SubTilesets.Count];
                for (int i = 0; i < m_autoTileMap.Tileset.SubTilesets.Count; ++i)
                {
                    m_tileGroupNames[i] = m_autoTileMap.Tileset.SubTilesets[i].Name;
                }

                if (m_autoTileMap.ViewCamera == null)
                {
                    Debug.LogWarning("AutoTileMap has no ViewCamera set. Camera.main will be set as ViewCamera");
                    m_autoTileMap.ViewCamera = Camera.main;
                }
                m_camera2D = m_autoTileMap.ViewCamera.GetComponent <Camera2DController>();

                if (m_camera2D == null)
                {
                    m_camera2D = m_autoTileMap.ViewCamera.gameObject.AddComponent <Camera2DController>();
                }

                m_camera2DFollowBehaviour = m_camera2D.transform.GetComponent <FollowObjectBehaviour>();

                // Generate thumbnail textures
                m_thumbnailTextures = new Texture2D[m_autoTileMap.Tileset.SubTilesets.Count];
                for (int i = 0; i < m_thumbnailTextures.Length; ++i)
                {
                    m_thumbnailTextures[i] = UtilsAutoTileMap.GenerateTilesetTexture(m_autoTileMap.Tileset, m_autoTileMap.Tileset.SubTilesets[i]);
                }

                #region Collision Layer
                m_spriteCollLayer                  = new GameObject();
                m_spriteCollLayer.name             = "CollisionLayer";
                m_spriteCollLayer.transform.parent = transform;
                SpriteRenderer sprRender = m_spriteCollLayer.AddComponent <SpriteRenderer>();
                sprRender.sortingOrder = 50;                 //TODO: +50 temporal? see for a const number later
                _GenerateCollisionTexture();
                #endregion

                #region Layers Combobox
                string[] toolBarNames = m_autoTileMap.MapLayers.Select(x => x.Name).ToArray();
                comboBoxList = new GUIContent[toolBarNames.Length];
                for (int i = 0; i < toolBarNames.Length; ++i)
                {
                    comboBoxList[i] = new GUIContent("Layer: " + toolBarNames[i]);
                }

                listStyle.normal.textColor           = Color.white;
                listStyle.onHover.background         =
                    listStyle.hover.background       = new Texture2D(2, 2);
                listStyle.padding.left               =
                    listStyle.padding.right          =
                        listStyle.padding.top        =
                            listStyle.padding.bottom = 4;

                comboBoxControl = new ComboBox(new Rect(0, 0, 150, 20), comboBoxList[0], comboBoxList, "button", "box", listStyle);
                comboBoxControl.SelectedItemIndex = m_autoTileMap.BrushGizmo.SelectedLayer;
                #endregion
            }
        }
        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();
        }
示例#9
0
        public void OnInspectorGUI()
        {
            // refresh data if needed
            if (m_subTilesetNames == null || m_subTilesetNames.Length != m_autoTileMap.Tileset.SubTilesets.Count)
            {
                _refreshSubTilesetNames();
                m_subTilesetIdx = Mathf.Clamp(m_subTilesetIdx, 0, m_autoTileMap.Tileset.SubTilesets.Count);
            }
            m_subTilesetIdx = EditorGUILayout.Popup("Tileset: ", m_subTilesetIdx, m_subTilesetNames);

            if (GUI.changed || m_tilesetTexture == null)
            {
                m_tilesetTexture = UtilsAutoTileMap.GenerateTilesetTexture(m_autoTileMap.Tileset, m_autoTileMap.Tileset.SubTilesets[m_subTilesetIdx]);
            }


            if (m_tilesetTexture != null)
            {
                Rect rTileset = new Rect();
                Rect rTile    = new Rect(0, 0, k_visualTileWidth, k_visualTileHeight);

                if (IsEditCollision)
                {
                    //NOTE: if you don't see the special characters properly, be sure this file is saved in UTF-8
                    EditorGUILayout.HelpBox("■ - Block Collision\n□ - Wall Collision\n# - Fence Collision\n★ - Overlay", MessageType.Info);
                }
                else
                {
                    EditorGUILayout.HelpBox("Press Shift Key to change collisions by pressing left/right mouse button over the tile", MessageType.Info);
                }

                // BeginScrollView
                m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, GUILayout.MinHeight(16f * k_visualTileHeight));
                {
                    GUIStyle tilesetStyle = new GUIStyle(GUI.skin.box);
                    tilesetStyle.normal.background = m_tilesetTexture;
                    tilesetStyle.border            = tilesetStyle.margin = tilesetStyle.padding = new RectOffset(0, 0, 0, 0);
                    float fWidth  = m_autoTileMap.Tileset.AutoTilesPerRow * k_visualTileWidth;
                    float fHeight = m_tilesetTexture.height * fWidth / m_tilesetTexture.width;
                    GUILayout.Box("", tilesetStyle, GUILayout.Width(fWidth), GUILayout.Height(fHeight));
                    rTileset = GUILayoutUtility.GetLastRect();

                    if (IsEditCollision)
                    {
                        for (int autoTileLocalIdx = 0; autoTileLocalIdx < 256; ++autoTileLocalIdx)                          //autoTileLocalIdx: index of current tileset group
                        {
                            rTile.x = rTileset.x + (autoTileLocalIdx % m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileWidth;
                            rTile.y = rTileset.y + (autoTileLocalIdx / m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileHeight;

                            int autoTileIdx = autoTileLocalIdx + (int)m_subTilesetIdx * 256;                             // global autotile idx
                            if (Event.current.type == EventType.MouseUp)
                            {
                                if (rTile.Contains(Event.current.mousePosition))
                                {
                                    int collType = (int)m_autoTileMap.Tileset.AutotileCollType[autoTileIdx];
                                    if (Event.current.button == 0)
                                    {
                                        collType += 1;                                         // go next
                                    }
                                    else if (Event.current.button == 1)
                                    {
                                        collType += (int)eTileCollisionType._SIZE - 1;                                         // go back
                                    }
                                    collType %= (int)eTileCollisionType._SIZE;
                                    m_autoTileMap.Tileset.AutotileCollType[autoTileIdx] = (eTileCollisionType)(collType);
                                }
                                EditorUtility.SetDirty(m_autoTileMap.Tileset);
                            }


                            string sCollision = "";
                            switch (m_autoTileMap.Tileset.AutotileCollType[autoTileIdx])
                            {
                            //NOTE: if you don't see the special characters properly, be sure this file is saved in UTF-8
                            case eTileCollisionType.BLOCK: sCollision = "■"; break;

                            case eTileCollisionType.FENCE: sCollision = "#"; break;

                            case eTileCollisionType.WALL: sCollision = "□"; break;

                            case eTileCollisionType.OVERLAY: sCollision = "★"; break;
                            }

                            if (sCollision.Length > 0)
                            {
                                GUI.color = new Color(1f, 1f, 1f, 1f);
                                GUIStyle style = new GUIStyle();
                                style.fontSize         = 30;
                                style.fontStyle        = FontStyle.Bold;
                                style.alignment        = TextAnchor.MiddleCenter;
                                style.normal.textColor = Color.white;
                                GUI.Box(rTile, sCollision, style);
                                GUI.color = Color.white;
                            }

                            //debug Alpha tiles

                            /*/
                             * if( m_autoTileMap.Tileset.IsAutoTileHasAlpha[autoTileIdx] )
                             * {
                             *      GUIStyle style = new GUIStyle();
                             *      style.fontSize = 30;
                             *      style.fontStyle = FontStyle.Bold;
                             *      style.alignment = TextAnchor.MiddleCenter;
                             *      style.normal.textColor = Color.blue;
                             *      GUI.Box( rTile, "A", style );
                             * }
                             * //*/
                        }
                    }
                    else
                    {
                        UpdateTilesetOnInspector(rTileset);

                        Rect rSelected          = new Rect(0, 0, k_visualTileWidth, k_visualTileHeight);
                        int  tileWithSelectMark = m_selectedTileId;
                        tileWithSelectMark -= (int)m_subTilesetIdx * 256;
                        rSelected.position  = rTileset.position + new Vector2((tileWithSelectMark % m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileWidth, (tileWithSelectMark / m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileHeight);
                        UtilsGuiDrawing.DrawRectWithOutline(rSelected, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f));
                    }
                }
                EditorGUILayout.EndScrollView();
            }
        }