Exemple #1
0
        /// <summary>Callback for drawing the Inspector GUI when there is an active GridSelection made in a Tilemap.</summary>
        public override void OnSelectionInspectorGUI()
        {
            BoundsInt selection = GridSelection.position;
            Tilemap   tilemap   = GridSelection.target.GetComponent <Tilemap>();

            int cellCount = selection.size.x * selection.size.y * selection.size.z;

            if (tilemap != null && cellCount > 0)
            {
                base.OnSelectionInspectorGUI();
                GUILayout.Space(10f);

                if (m_SelectionTiles == null || m_SelectionTiles.Length != cellCount)
                {
                    m_SelectionTiles         = new TileBase[cellCount];
                    m_SelectionColors        = new Color[cellCount];
                    m_SelectionMatrices      = new Matrix4x4[cellCount];
                    m_SelectionFlagsArray    = new TileFlags[cellCount];
                    m_SelectionSprites       = new Sprite[cellCount];
                    m_SelectionColliderTypes = new Tile.ColliderType[cellCount];
                }

                int index = 0;
                foreach (var p in selection.allPositionsWithin)
                {
                    m_SelectionTiles[index]         = tilemap.GetTile(p);
                    m_SelectionColors[index]        = tilemap.GetColor(p);
                    m_SelectionMatrices[index]      = tilemap.GetTransformMatrix(p);
                    m_SelectionFlagsArray[index]    = tilemap.GetTileFlags(p);
                    m_SelectionSprites[index]       = tilemap.GetSprite(p);
                    m_SelectionColliderTypes[index] = tilemap.GetColliderType(p);
                    index++;
                }

                EditorGUI.BeginChangeCheck();
                EditorGUI.showMixedValue = m_SelectionTiles.Any(tile => tile != m_SelectionTiles.First());
                var      position = new Vector3Int(selection.xMin, selection.yMin, selection.zMin);
                TileBase newTile  = EditorGUILayout.ObjectField(Styles.tileLabel, tilemap.GetTile(position), typeof(TileBase), false) as TileBase;
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(tilemap, "Edit Tilemap");
                    foreach (var p in selection.allPositionsWithin)
                    {
                        tilemap.SetTile(p, newTile);
                    }
                }

                using (new EditorGUI.DisabledScope(true))
                {
                    EditorGUI.showMixedValue = m_SelectionSprites.Any(sprite => sprite != m_SelectionSprites.First());
                    EditorGUILayout.ObjectField(Styles.spriteLabel, m_SelectionSprites[0], typeof(Sprite), false, GUILayout.Height(EditorGUI.kSingleLineHeight));
                }

                bool colorFlagsAllEqual = m_SelectionFlagsArray.All(flags => (flags & TileFlags.LockColor) == (m_SelectionFlagsArray.First() & TileFlags.LockColor));
                using (new EditorGUI.DisabledScope(!colorFlagsAllEqual || (m_SelectionFlagsArray[0] & TileFlags.LockColor) != 0))
                {
                    EditorGUI.showMixedValue = m_SelectionColors.Any(color => color != m_SelectionColors.First());
                    EditorGUI.BeginChangeCheck();
                    Color newColor = EditorGUILayout.ColorField(Styles.colorLabel, m_SelectionColors[0]);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(tilemap, "Edit Tilemap");
                        foreach (var p in selection.allPositionsWithin)
                        {
                            tilemap.SetColor(p, newColor);
                        }
                    }
                }

                using (new EditorGUI.DisabledScope(true))
                {
                    EditorGUI.showMixedValue = m_SelectionColliderTypes.Any(colliderType => colliderType != m_SelectionColliderTypes.First());
                    EditorGUILayout.EnumPopup(Styles.colliderTypeLabel, m_SelectionColliderTypes[0]);
                }

                bool transformFlagsAllEqual = m_SelectionFlagsArray.All(flags => (flags & TileFlags.LockTransform) == (m_SelectionFlagsArray.First() & TileFlags.LockTransform));
                using (new EditorGUI.DisabledScope(!transformFlagsAllEqual || (m_SelectionFlagsArray[0] & TileFlags.LockTransform) != 0))
                {
                    EditorGUI.showMixedValue = m_SelectionMatrices.Any(matrix => matrix != m_SelectionMatrices.First());
                    EditorGUI.BeginChangeCheck();
                    Matrix4x4 newTransformMatrix = TileEditor.TransformMatrixOnGUI(m_SelectionMatrices[0]);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(tilemap, "Edit Tilemap");
                        foreach (var p in selection.allPositionsWithin)
                        {
                            tilemap.SetTransformMatrix(p, newTransformMatrix);
                        }
                    }
                }

                using (new EditorGUI.DisabledScope(true))
                {
                    EditorGUI.showMixedValue = !colorFlagsAllEqual;
                    EditorGUILayout.Toggle(Styles.lockColorLabel, (m_SelectionFlagsArray[0] & TileFlags.LockColor) != 0);
                    EditorGUI.showMixedValue = !transformFlagsAllEqual;
                    EditorGUILayout.Toggle(Styles.lockTransformLabel, (m_SelectionFlagsArray[0] & TileFlags.LockTransform) != 0);
                }

                EditorGUI.showMixedValue = false;
            }
        }
Exemple #2
0
        /// <summary>Callback for drawing the Inspector GUI when there is an active GridSelection made in a Tilemap.</summary>
        public override void OnSelectionInspectorGUI()
        {
            BoundsInt selection = GridSelection.position;
            Tilemap   tilemap   = GridSelection.target.GetComponent <Tilemap>();

            int cellCount = selectionCellCount;

            if (tilemap != null && cellCount > 0)
            {
                base.OnSelectionInspectorGUI();

                if (!EditorGUIUtility.editingTextField &&
                    Event.current.type == EventType.KeyDown &&
                    (Event.current.keyCode == KeyCode.Delete ||
                     Event.current.keyCode == KeyCode.Backspace))
                {
                    DeleteSelection(tilemap, selection);
                    Event.current.Use();
                }

                GUILayout.Space(10f);

                EditorGUILayout.LabelField(Styles.gridSelectionPropertiesLabel, EditorStyles.boldLabel);

                UpdateSelection(tilemap);

                EditorGUI.BeginChangeCheck();
                EditorGUI.showMixedValue = m_SelectionTiles.Any(tile => tile != m_SelectionTiles.First());
                var      position = new Vector3Int(selection.xMin, selection.yMin, selection.zMin);
                TileBase newTile  = EditorGUILayout.ObjectField(Styles.tileLabel, tilemap.GetTile(position), typeof(TileBase), false) as TileBase;
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(tilemap, "Edit Tilemap");
                    foreach (var p in selection.allPositionsWithin)
                    {
                        tilemap.SetTile(p, newTile);
                    }
                }

                using (new EditorGUI.DisabledScope(true))
                {
                    EditorGUI.showMixedValue = m_SelectionSprites.Any(sprite => sprite != m_SelectionSprites.First());
                    EditorGUILayout.ObjectField(Styles.spriteLabel, m_SelectionSprites[0], typeof(Sprite), false, GUILayout.Height(EditorGUI.kSingleLineHeight));
                }

                bool colorFlagsAllEqual = m_SelectionFlagsArray.All(flags => (flags & TileFlags.LockColor) == (m_SelectionFlagsArray.First() & TileFlags.LockColor));
                using (new EditorGUI.DisabledScope(!colorFlagsAllEqual || (m_SelectionFlagsArray[0] & TileFlags.LockColor) != 0))
                {
                    EditorGUI.showMixedValue = m_SelectionColors.Any(color => color != m_SelectionColors.First());
                    EditorGUI.BeginChangeCheck();
                    Color newColor = EditorGUILayout.ColorField(Styles.colorLabel, m_SelectionColors[0]);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(tilemap, "Edit Tilemap");
                        foreach (var p in selection.allPositionsWithin)
                        {
                            tilemap.SetColor(p, newColor);
                        }
                    }
                }

                using (new EditorGUI.DisabledScope(true))
                {
                    EditorGUI.showMixedValue = m_SelectionColliderTypes.Any(colliderType => colliderType != m_SelectionColliderTypes.First());
                    EditorGUILayout.EnumPopup(Styles.colliderTypeLabel, m_SelectionColliderTypes[0]);
                }

                bool transformFlagsAllEqual = m_SelectionFlagsArray.All(flags => (flags & TileFlags.LockTransform) == (m_SelectionFlagsArray.First() & TileFlags.LockTransform));
                using (new EditorGUI.DisabledScope(!transformFlagsAllEqual || (m_SelectionFlagsArray[0] & TileFlags.LockTransform) != 0))
                {
                    EditorGUI.showMixedValue = m_SelectionMatrices.Any(matrix => matrix != m_SelectionMatrices.First());
                    EditorGUI.BeginChangeCheck();
                    Matrix4x4 newTransformMatrix = TileEditor.TransformMatrixOnGUI(m_SelectionMatrices[0]);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(tilemap, "Edit Tilemap");
                        foreach (var p in selection.allPositionsWithin)
                        {
                            tilemap.SetTransformMatrix(p, newTransformMatrix);
                        }
                    }
                }

                using (new EditorGUI.DisabledScope(true))
                {
                    EditorGUI.showMixedValue = !colorFlagsAllEqual;
                    EditorGUILayout.Toggle(Styles.lockColorLabel, (m_SelectionFlagsArray[0] & TileFlags.LockColor) != 0);
                    EditorGUI.showMixedValue = !transformFlagsAllEqual;
                    EditorGUILayout.Toggle(Styles.lockTransformLabel, (m_SelectionFlagsArray[0] & TileFlags.LockTransform) != 0);
                }

                EditorGUI.showMixedValue = false;

                if (GUILayout.Button(Styles.deleteSelectionLabel))
                {
                    DeleteSelection(tilemap, selection);
                }

                EditorGUILayout.Space();
                EditorGUILayout.LabelField(Styles.modifyTilemapLabel, EditorStyles.boldLabel);
                EditorGUILayout.Space();

                EditorGUI.BeginChangeCheck();
                m_SelectedTransformTool = GUILayout.Toolbar(m_SelectedTransformTool, Styles.selectionTools);
                if (EditorGUI.EndChangeCheck())
                {
                    SceneView.RepaintAll();
                }
                EditorGUILayout.Space();

                GUILayout.BeginHorizontal();
                m_ModifyCells = (ModifyCells)EditorGUILayout.EnumPopup(m_ModifyCells);
                m_CellCount   = EditorGUILayout.IntField(m_CellCount);
                if (GUILayout.Button(Styles.modifyLabel))
                {
                    RegisterUndoForTilemap(tilemap, Enum.GetName(typeof(ModifyCells), m_ModifyCells));
                    switch (m_ModifyCells)
                    {
                    case ModifyCells.InsertRow:
                    {
                        tilemap.InsertCells(GridSelection.position.position, 0, m_CellCount, 0);
                        break;
                    }

                    case ModifyCells.InsertRowBefore:
                    {
                        tilemap.InsertCells(GridSelection.position.position, 0, -m_CellCount, 0);
                        break;
                    }

                    case ModifyCells.InsertColumn:
                    {
                        tilemap.InsertCells(GridSelection.position.position, m_CellCount, 0, 0);
                        break;
                    }

                    case ModifyCells.InsertColumnBefore:
                    {
                        tilemap.InsertCells(GridSelection.position.position, -m_CellCount, 0, 0);
                        break;
                    }

                    case ModifyCells.DeleteRow:
                    {
                        tilemap.DeleteCells(GridSelection.position.position, 0, m_CellCount, 0);
                        break;
                    }

                    case ModifyCells.DeleteRowBefore:
                    {
                        tilemap.DeleteCells(GridSelection.position.position, 0, -m_CellCount, 0);
                        break;
                    }

                    case ModifyCells.DeleteColumn:
                    {
                        tilemap.DeleteCells(GridSelection.position.position, m_CellCount, 0, 0);
                        break;
                    }

                    case ModifyCells.DeleteColumnBefore:
                    {
                        tilemap.DeleteCells(GridSelection.position.position, -m_CellCount, 0, 0);
                        break;
                    }
                    }
                }
                GUILayout.EndHorizontal();
            }
        }