Пример #1
0
        public override void OnInspectorGUI()
        {
            Event e = Event.current;

            if (e.type == EventType.ValidateCommand)
            {
                if (e.commandName == "UndoRedoPerformed")
                {
                    m_tilemap.Refresh();
                }
            }
            serializedObject.Update();

            Tileset prevTileset = m_tilemap.Tileset;

            GUI.backgroundColor = Color.yellow;
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            m_tilemap.Tileset = (Tileset)EditorGUILayout.ObjectField("Tileset", m_tilemap.Tileset, typeof(Tileset), false);
            EditorGUILayout.EndVertical();
            GUI.backgroundColor = Color.white;

            if (prevTileset != m_tilemap.Tileset)
            {
                UnregisterTilesetEvents(prevTileset);
                RegisterTilesetEvents(m_tilemap.Tileset);
                m_tilemapTileset = m_tilemap.Tileset;
            }

            if (m_tilemap.Tileset == null)
            {
                EditorGUILayout.HelpBox("There is no tileset selected", MessageType.Info);
                return;
            }

            string[] editModeNames = System.Enum.GetNames(typeof(eEditMode));
            s_editMode = (eEditMode)GUILayout.Toolbar((int)s_editMode, editModeNames);
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            {
                if (s_editMode == eEditMode.Renderer)
                {
                    EditorGUI.BeginChangeCheck();
                    Material prevMaterial = m_tilemap.Material;
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_material"));
                    if (EditorGUI.EndChangeCheck())
                    {
                        serializedObject.ApplyModifiedProperties();
                        m_tilemap.Refresh();
                        if (m_tilemap.Material != prevMaterial && !AssetDatabase.Contains(prevMaterial))
                        {
                            //avoid memory leak
                            DestroyImmediate(prevMaterial);
                        }
                    }
                    // Draw Material Control

                    /*
                     * if (m_matEditor == null || EditorGUI.EndChangeCheck())
                     * {
                     *  if (m_matEditor != null) DestroyImmediate(m_matEditor);
                     *  m_matEditor = MaterialEditor.CreateEditor(m_tilemap.Material);
                     *  m_matEditor.hideFlags = HideFlags.DontSave;
                     * }
                     * float savedLabelWidth = EditorGUIUtility.labelWidth;
                     * m_matEditor.DrawHeader();
                     * m_matEditor.OnInspectorGUI();
                     * EditorGUIUtility.labelWidth = savedLabelWidth;
                     */
                    //---

                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_tintColor"));

                    //Pixel Snap
                    if (m_tilemap.Material.HasProperty("PixelSnap"))
                    {
                        EditorGUI.BeginChangeCheck();
                        bool isPixelSnapOn = EditorGUILayout.Toggle("Pixel Snap", m_tilemap.PixelSnap);
                        if (EditorGUI.EndChangeCheck())
                        {
                            m_tilemap.PixelSnap = isPixelSnapOn;
                        }
                    }

                    // Sorting Layer and Order in layer
                    EditorGUI.BeginChangeCheck();
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_sortingLayer"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_orderInLayer"));
                    serializedObject.FindProperty("m_orderInLayer").intValue = (serializedObject.FindProperty("m_orderInLayer").intValue << 16) >> 16; // convert from int32 to int16 keeping sign
                    if (EditorGUI.EndChangeCheck())
                    {
                        serializedObject.ApplyModifiedProperties();
                        m_tilemap.RefreshChunksSortingAttributes();
                        SceneView.RepaintAll();
                    }
                    //---

                    EditorGUILayout.PropertyField(serializedObject.FindProperty("InnerPadding"), new GUIContent("Inner Padding", "The size, in pixels, the tile UV will be stretched. Use this to fix pixel precision artifacts when tiles have no padding border in the atlas."));

                    m_tilemap.IsVisible = EditorGUILayout.Toggle("Visible", m_tilemap.IsVisible);
                }
                else if (s_editMode == eEditMode.Map)
                {
                    EditorGUILayout.Space();

                    if (GUILayout.Button("Refresh Map", GUILayout.MaxWidth(125)))
                    {
                        m_tilemap.Refresh(true, true, true, true);
                    }
                    if (GUILayout.Button("Clear Map", GUILayout.MaxWidth(125)))
                    {
                        if (EditorUtility.DisplayDialog("Warning!", "Are you sure you want to clear the map?\nThis action will remove all children objects under the tilemap", "Yes", "No"))
                        {
                            Undo.RegisterFullObjectHierarchyUndo(m_tilemap.gameObject, "Clear Map " + m_tilemap.name);
                            m_tilemap.IsUndoEnabled = true;
                            m_tilemap.ClearMap();
                            m_tilemap.IsUndoEnabled = false;
                        }
                    }
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("m_cellSize"));
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("ShowGrid"), new GUIContent("Show Grid", "Show the tilemap grid."));
                    EditorGUILayout.EndVertical();
                    EditorGUILayout.Space();

                    EditorGUILayout.LabelField("Map Size (" + m_tilemap.GridWidth + "," + m_tilemap.GridHeight + ")");

                    //+++ Display Map Bounds
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    {
                        EditorGUILayout.LabelField("Map Bounds (in tiles):", EditorStyles.boldLabel);
                        m_toggleMapBoundsEdit = EditorUtils.DoToggleIconButton("Edit Map Bounds", m_toggleMapBoundsEdit, EditorGUIUtility.IconContent("EditCollider"));

                        float savedLabelWidth = EditorGUIUtility.labelWidth;
                        EditorGUIUtility.labelWidth = 80;
                        EditorGUI.indentLevel      += 2;

                        EditorGUI.BeginChangeCheck();
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_minGridX"), new GUIContent("Left"));
                        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_minGridY"), new GUIContent("Bottom"));
                        EditorGUILayout.EndVertical();

                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_maxGridX"), new GUIContent("Right"));
                        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_maxGridY"), new GUIContent("Top"));
                        EditorGUILayout.EndVertical();
                        if (EditorGUI.EndChangeCheck())
                        {
                            serializedObject.ApplyModifiedProperties();
                            m_tilemap.RecalculateMapBounds();
                        }

                        EditorGUI.indentLevel      -= 2;
                        EditorGUIUtility.labelWidth = savedLabelWidth;
                    }
                    EditorGUILayout.EndVertical();
                    //---

                    EditorGUILayout.Space();

                    m_tilemap.AllowPaintingOutOfBounds = EditorGUILayout.ToggleLeft("Allow Painting Out of Bounds", m_tilemap.AllowPaintingOutOfBounds);

                    EditorGUILayout.Space();

                    if (GUILayout.Button("Shrink to Visible Area", GUILayout.MaxWidth(150)))
                    {
                        m_tilemap.ShrinkMapBoundsToVisibleArea();
                    }

                    EditorGUILayout.Space();
                    EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                    {
                        EditorGUILayout.LabelField("Advanced Options", EditorStyles.boldLabel);
                        EditorGUI.BeginChangeCheck();
                        bool isChunksVisible = IsTilemapChunksVisible();
                        isChunksVisible = EditorGUILayout.Toggle(new GUIContent("Show Tile Chunks", "Show tilemap chunk objects for debugging or other purposes. Hiding will be refreshed after collapsing the tilemap."), isChunksVisible);
                        if (EditorGUI.EndChangeCheck())
                        {
                            SetTilemapChunkHideFlag(HideFlags.HideInHierarchy, !isChunksVisible);
                        }
                        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_enableUndoWhilePainting"), new GUIContent("Enable Undo", "Disable Undo when painting on big maps to improve performance."));
                    }
                    EditorGUILayout.EndVertical();
                }
                else if (s_editMode == eEditMode.Collider)
                {
                    EditorGUI.BeginChangeCheck();
                    {
                        //EditorGUILayout.PropertyField(serializedObject.FindProperty("ColliderType"));
                        EditorGUILayout.Space();

                        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                        EditorGUILayout.LabelField("Collider Type:", EditorStyles.boldLabel);
                        EditorGUI.indentLevel += 2;
                        SerializedProperty colliderTypeProperty = serializedObject.FindProperty("ColliderType");
                        string[]           colliderTypeNames    = new List <string>(System.Enum.GetNames(typeof(eColliderType)).Select(x => x.Replace('_', ' '))).ToArray();

                        colliderTypeProperty.intValue = GUILayout.Toolbar(colliderTypeProperty.intValue, colliderTypeNames);
                        EditorGUI.indentLevel        -= 2;
                        EditorGUILayout.Space();
                        EditorGUILayout.EndVertical();

                        EditorGUILayout.Space();

                        if (m_tilemap.ColliderType == eColliderType._3D)
                        {
                            SerializedProperty colliderDepthProperty = serializedObject.FindProperty("ColliderDepth");
                            EditorGUILayout.PropertyField(colliderDepthProperty);
                            colliderDepthProperty.floatValue = Mathf.Clamp(colliderDepthProperty.floatValue, Vector3.kEpsilon, Mathf.Max(colliderDepthProperty.floatValue));
                        }
                        else if (m_tilemap.ColliderType == eColliderType._2D)
                        {
                            EditorGUILayout.PropertyField(serializedObject.FindProperty("Collider2DType"));
                            EditorGUILayout.PropertyField(serializedObject.FindProperty("ShowColliderNormals"));
                        }

                        EditorGUILayout.PropertyField(serializedObject.FindProperty("m_isTrigger"));

                        if (m_tilemap.IsTrigger)
                        {
                            EditorGUILayout.HelpBox("Activating IsTrigger could generate wrong collider lines if not used properly. Use Show Tilechunks in Map section to display the real collider lines.", MessageType.Warning);
                        }
                        else if (m_tilemap.ColliderType == eColliderType._2D && m_tilemap.Collider2DType == e2DColliderType.PolygonCollider2D)
                        {
                            EditorGUILayout.HelpBox("Using Polygon colliders could generate wrong collider lines if not used properly. Use Show Tilechunks in Map section to display the real collider lines.", MessageType.Warning);
                        }
                    }
                    if (EditorGUI.EndChangeCheck())
                    {
                        serializedObject.ApplyModifiedProperties();
                        m_tilemap.Refresh(false, true);
                    }

                    EditorGUILayout.Space();

                    if (GUILayout.Button("Update Collider Mesh"))
                    {
                        m_tilemap.Refresh(false, true);
                    }
                }
                else if (s_editMode == eEditMode.Paint)
                {
                    if (m_tilemap.Tileset != null)
                    {
                        if (m_tilesetCtrl == null)
                        {
                            m_tilesetCtrl = new TilesetControl();
                        }
                        m_tilesetCtrl.Tileset = m_tilemap.Tileset;
                        m_tilesetCtrl.Display();
                    }
                }
            }
            EditorGUILayout.EndVertical();

            Repaint();
            serializedObject.ApplyModifiedProperties();
            if (GUI.changed)
            {
                EditorUtility.SetDirty(target);
            }
        }
        private ReorderableList CreateTilemapReorderableList()
        {
            ReorderableList reordList = new ReorderableList(serializedObject, serializedObject.FindProperty("m_tilemaps"), true, true, true, true);

            reordList.displayAdd         = reordList.displayRemove = true;
            reordList.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, "Tilemaps", EditorStyles.boldLabel);
                Texture2D btnTexture = reordList.elementHeight == 0f ? EditorGUIUtility.FindTexture("winbtn_win_max_h") : EditorGUIUtility.FindTexture("winbtn_win_min_h");
                if (GUI.Button(new Rect(rect.x + rect.width - rect.height, rect.y, rect.height, rect.height), btnTexture, EditorStyles.label))
                {
                    GUI.changed             = true;
                    reordList.elementHeight = reordList.elementHeight == 0f ? 21f : 0f;
                    reordList.draggable     = reordList.elementHeight > 0f;
                    reordList.displayAdd    = reordList.draggable;
                    reordList.displayRemove = reordList.draggable;
                }
            };
            reordList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if (reordList.elementHeight == 0)
                {
                    return;
                }
                var element = reordList.serializedProperty.GetArrayElementAtIndex(index);
                rect.y += 2;
                Tilemap tilemap = element.objectReferenceValue as Tilemap;
                if (tilemap)
                {
                    SerializedObject tilemapSerialized    = new SerializedObject(tilemap);
                    SerializedObject tilemapObjSerialized = new SerializedObject(tilemapSerialized.FindProperty("m_GameObject").objectReferenceValue);

                    Rect rToggle       = new Rect(rect.x, rect.y, 16f, EditorGUIUtility.singleLineHeight);
                    Rect rName         = new Rect(rect.x + 20f, rect.y, rect.width - 130f - 20f, EditorGUIUtility.singleLineHeight);
                    Rect rColliders    = new Rect(rect.x + rect.width - 125f, rect.y, 125f, EditorGUIUtility.singleLineHeight);
                    Rect rSortingLayer = new Rect(rect.x + rect.width - 125f, rect.y, 80f, EditorGUIUtility.singleLineHeight);
                    Rect rSortingOrder = new Rect(rect.x + rect.width - 40f, rect.y, 40f, EditorGUIUtility.singleLineHeight);

                    tilemap.IsVisible = EditorGUI.Toggle(rToggle, GUIContent.none, tilemap.IsVisible, STEditorStyles.Instance.visibleToggleStyle);
                    EditorGUI.PropertyField(rName, tilemapObjSerialized.FindProperty("m_Name"), GUIContent.none);
                    if (TilemapEditor.EditMode == TilemapEditor.eEditMode.Collider)
                    {
                        SerializedProperty colliderTypeProperty = tilemapSerialized.FindProperty("ColliderType");
                        string[]           colliderTypeNames    = new List <string>(System.Enum.GetNames(typeof(eColliderType)).Select(x => x.Replace('_', ' '))).ToArray();
                        EditorGUI.BeginChangeCheck();
                        colliderTypeProperty.intValue = GUI.Toolbar(rColliders, colliderTypeProperty.intValue, colliderTypeNames);
                        if (EditorGUI.EndChangeCheck())
                        {
                            tilemapSerialized.ApplyModifiedProperties();
                            tilemap.Refresh(false, true);
                        }
                    }
                    else
                    {
                        // Sorting Layer and Order in layer
                        EditorGUI.BeginChangeCheck();
                        EditorGUI.PropertyField(rSortingLayer, tilemapSerialized.FindProperty("m_sortingLayer"), GUIContent.none);
                        EditorGUI.PropertyField(rSortingOrder, tilemapSerialized.FindProperty("m_orderInLayer"), GUIContent.none);
                        tilemapSerialized.FindProperty("m_orderInLayer").intValue = (tilemapSerialized.FindProperty("m_orderInLayer").intValue << 16) >> 16; // convert from int32 to int16 keeping sign
                        if (EditorGUI.EndChangeCheck())
                        {
                            tilemapSerialized.ApplyModifiedProperties();
                            tilemap.RefreshChunksSortingAttributes();
                            SceneView.RepaintAll();
                        }
                        //---
                    }

                    if (GUI.changed)
                    {
                        tilemapObjSerialized.ApplyModifiedProperties();
                    }
                }
            };
            reordList.onReorderCallback = (ReorderableList list) =>
            {
                var targetObj   = target as TilemapGroup;
                int sibilingIdx = 0;
                foreach (Tilemap tilemap in targetObj.Tilemaps)
                {
                    tilemap.transform.SetSiblingIndex(sibilingIdx++);
                }
                Repaint();
            };
            reordList.onSelectCallback = (ReorderableList list) =>
            {
                m_selectedIndexProp.intValue = reordList.index;
                serializedObject.ApplyModifiedProperties();
                GUI.changed = true;
                TileSelectionWindow.RefreshIfVisible();
                TilePropertiesWindow.RefreshIfVisible();
            };
            reordList.onAddCallback = (ReorderableList list) =>
            {
                var targetObj = target as TilemapGroup;
                Undo.RegisterCompleteObjectUndo(targetObj, "New Tilemap");
                GameObject obj = new GameObject();
                Undo.RegisterCreatedObjectUndo(obj, "New Tilemap");
                Tilemap newTilemap = obj.AddComponent <Tilemap>();
                obj.transform.parent = targetObj.transform;
                obj.name             = GameObjectUtility.GetUniqueNameForSibling(obj.transform.parent, "New Tilemap");

                Tilemap copiedTilemap = targetObj.SelectedTilemap;
                if (copiedTilemap)
                {
                    UnityEditorInternal.ComponentUtility.CopyComponent(copiedTilemap);
                    UnityEditorInternal.ComponentUtility.PasteComponentValues(newTilemap);
                    obj.name = GameObjectUtility.GetUniqueNameForSibling(obj.transform.parent, copiedTilemap.name);
                }
            };
            reordList.onRemoveCallback = (ReorderableList list) =>
            {
                m_tilemapRemovingList.Add(m_target.SelectedTilemap.gameObject);
            };

            return(reordList);
        }
        private ReorderableList CreateTilemapReorderableList()
        {
            ReorderableList reordList = new ReorderableList(serializedObject, serializedObject.FindProperty("m_tilemaps"), true, true, true, true);

            reordList.displayAdd         = reordList.displayRemove = true;
            reordList.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, "Tilemaps", EditorStyles.boldLabel);
                Texture2D btnTexture = reordList.elementHeight == 0f ? EditorGUIUtility.FindTexture("winbtn_win_max_h") : EditorGUIUtility.FindTexture("winbtn_win_min_h");
                if (GUI.Button(new Rect(rect.width - rect.height, rect.y, rect.height, rect.height), btnTexture, EditorStyles.label))
                {
                    reordList.elementHeight = reordList.elementHeight == 0f ? EditorGUIUtility.singleLineHeight : 0f;
                    reordList.draggable     = reordList.elementHeight > 0f;
                }
            };
            reordList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                var element = reordList.serializedProperty.GetArrayElementAtIndex(index);

                if (Event.current.type == EventType.Repaint)
                {
                    m_reordListRectsDic[m_reordIdx] = rect;
                }
                else if (Event.current.type == EventType.Layout)
                {
                    m_reordListRectsDic.TryGetValue(m_reordIdx, out rect);
                }
                m_reordIdx++;

                rect.y += 2;

                Tilemap          tilemap              = element.objectReferenceValue as Tilemap;
                SerializedObject tilemapSerialized    = new SerializedObject(tilemap);
                SerializedObject tilemapObjSerialized = new SerializedObject(tilemapSerialized.FindProperty("m_GameObject").objectReferenceValue);

                GUILayout.BeginArea(rect);
                EditorGUILayout.BeginHorizontal();
                tilemap.IsVisible = EditorGUILayout.Toggle(tilemap.IsVisible, GUILayout.Width(16));
                EditorGUILayout.PropertyField(tilemapObjSerialized.FindProperty("m_Name"), GUIContent.none);
                if (TilemapEditor.EditMode == TilemapEditor.eEditMode.Collider)
                {
                    SerializedProperty colliderTypeProperty = tilemapSerialized.FindProperty("ColliderType");
                    string[]           colliderTypeNames    = new List <string>(System.Enum.GetNames(typeof(eColliderType)).Select(x => x.Replace('_', ' '))).ToArray();
                    EditorGUI.BeginChangeCheck();
                    colliderTypeProperty.intValue = GUILayout.SelectionGrid(colliderTypeProperty.intValue, colliderTypeNames, colliderTypeNames.Length, GUILayout.MaxHeight(0.9f * EditorGUIUtility.singleLineHeight));
                    if (EditorGUI.EndChangeCheck())
                    {
                        tilemapSerialized.ApplyModifiedProperties();
                        tilemap.Refresh(false, true);
                    }
                }
                else
                {
                    // Sorting Layer and Order in layer
                    EditorGUI.BeginChangeCheck();
                    EditorGUIUtility.labelWidth = 1;
                    EditorGUILayout.PropertyField(tilemapSerialized.FindProperty("m_sortingLayer"), new GUIContent(" "));
                    EditorGUIUtility.labelWidth = 40;
                    EditorGUILayout.PropertyField(tilemapSerialized.FindProperty("m_orderInLayer"), new GUIContent("Order"), GUILayout.MaxWidth(90));
                    EditorGUIUtility.labelWidth = 0;
                    tilemapSerialized.FindProperty("m_orderInLayer").intValue = (tilemapSerialized.FindProperty("m_orderInLayer").intValue << 16) >> 16; // convert from int32 to int16 keeping sign
                    if (EditorGUI.EndChangeCheck())
                    {
                        tilemapSerialized.ApplyModifiedProperties();
                        tilemap.RefreshChunksSortingAttributes();
                        SceneView.RepaintAll();
                    }
                    //---
                }
                EditorGUILayout.EndHorizontal();
                GUILayout.EndArea();

                if (GUI.changed)
                {
                    tilemapObjSerialized.ApplyModifiedProperties();
                }
            };
            reordList.onReorderCallback = (ReorderableList list) =>
            {
                var targetObj   = target as TilemapGroup;
                int sibilingIdx = 0;
                foreach (Tilemap tilemap in targetObj.Tilemaps)
                {
                    tilemap.transform.SetSiblingIndex(sibilingIdx++);
                }
                Repaint();
            };
            reordList.onSelectCallback = (ReorderableList list) =>
            {
                serializedObject.FindProperty("m_selectedIndex").intValue = reordList.index;
                serializedObject.ApplyModifiedProperties();
                GUI.changed = true;
                TileSelectionWindow.RefreshIfVisible();
                TilePropertiesWindow.RefreshIfVisible();
            };
            reordList.onAddCallback = (ReorderableList list) =>
            {
                var targetObj = target as TilemapGroup;
                Undo.RegisterCompleteObjectUndo(targetObj, "New Tilemap");
                GameObject obj = new GameObject();
                Undo.RegisterCreatedObjectUndo(obj, "New Tilemap");
                Tilemap newTilemap = obj.AddComponent <Tilemap>();
                obj.transform.parent = targetObj.transform;
                obj.name             = GameObjectUtility.GetUniqueNameForSibling(obj.transform.parent, "New Tilemap");

                Tilemap copiedTilemap = targetObj.SelectedTilemap;
                if (copiedTilemap)
                {
                    UnityEditorInternal.ComponentUtility.CopyComponent(copiedTilemap);
                    UnityEditorInternal.ComponentUtility.PasteComponentValues(newTilemap);
                    obj.SendMessage("_DoDuplicate");
                    obj.name = GameObjectUtility.GetUniqueNameForSibling(obj.transform.parent, copiedTilemap.name);
                }
            };
            reordList.onRemoveCallback = (ReorderableList list) =>
            {
                var targetObj = target as TilemapGroup;
                Undo.DestroyObjectImmediate(targetObj.SelectedTilemap.gameObject);
                //NOTE: Fix argument exception
                if (m_tilemapReordList.index == targetObj.Tilemaps.Count - 1)
                {
                    serializedObject.FindProperty("m_selectedIndex").intValue = m_tilemapReordList.index = m_tilemapReordList.index - 1;
                }
            };

            return(reordList);
        }