Esempio n. 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);
            }
        }