using UnityEngine; using UnityEngine.Scripting.APIUpdating; using UnityEngine.Tilemaps; namespace UnityEditor.Tilemaps { /// Base class for Grid Brush Editor. [MovedFrom(true, "UnityEditor", "UnityEditor")] [CustomEditor(typeof(GridBrushBase))] public class GridBrushEditorBase : Editor { private static class Styles { public static readonly Color activeColor = new Color(1f, .5f, 0f); public static readonly Color executingColor = new Color(1f, .75f, 0.25f); } /// Checks if the Brush allows the changing of Z Position. /// Whether the Brush can change Z Position. public virtual bool canChangeZPosition { get { return true; } set {} } /// Callback for painting the GUI for the GridBrush in the Scene view. /// Grid that the brush is being used on. /// Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject. /// Current selected location of the brush. /// Current GridBrushBase::ref::Tool selected. /// Whether is brush is being used. /// Implement this for any special behaviours when the GridBrush is used on the Scene View. public virtual void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing) { OnPaintSceneGUIInternal(gridLayout, brushTarget, position, tool, executing); } /// Callback for painting the inspector GUI for the GridBrush in the tilemap palette. /// Implement this to have a custom editor in the tilemap palette for the GridBrush. public virtual void OnPaintInspectorGUI() { OnInspectorGUI(); } /// Callback for drawing the Inspector GUI when there is an active GridSelection made in a GridLayout. /// Override this to show custom Inspector GUI for the current selection. public virtual void OnSelectionInspectorGUI() {} /// Callback for painting custom gizmos when there is an active GridSelection made in a GridLayout. /// Grid that the brush is being used on. /// Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject. /// Override this to show custom gizmos for the current selection. public virtual void OnSelectionSceneGUI(GridLayout gridLayout, GameObject brushTarget) {} /// /// Callback for painting custom gizmos for the GridBrush for the brush target /// /// Grid that the brush is being used on. /// Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject. /// Override this to show custom gizmos for the brush target. public virtual void OnSceneGUI(GridLayout gridLayout, GameObject brushTarget) {} /// Callback when the mouse cursor leaves a paintable region. /// Implement this for any custom behaviour when the mouse cursor leaves a paintable region. public virtual void OnMouseLeave() {} /// Callback when the mouse cursor enters a paintable region. /// Implement this for any custom behaviour when the mouse cursor enters a paintable region. public virtual void OnMouseEnter() {} /// Callback when a GridBrushBase.Tool is activated. /// Tool that is activated. /// Implement this for any special behaviours when a Tool is activated. public virtual void OnToolActivated(GridBrushBase.Tool tool) {} /// Callback when a GridBrushBase.Tool is deactivated. /// Tool that is deactivated. /// Implement this for any special behaviours when a Tool is deactivated. public virtual void OnToolDeactivated(GridBrushBase.Tool tool) {} /// Callback for registering an Undo action before the GridBrushBase does the current GridBrushBase::ref::Tool action. /// Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject. /// Current GridBrushBase::ref::Tool selected. /// Implement this for any special Undo behaviours when a brush is used. public virtual void RegisterUndo(GameObject brushTarget, GridBrushBase.Tool tool) {} /// Returns all valid targets that the brush can edit. public virtual GameObject[] validTargets { get { return null; } } internal void OnEditStart(GridLayout gridLayout, GameObject brushTarget) { SetBufferSyncTile(brushTarget, true); } internal void OnEditEnd(GridLayout gridLayout, GameObject brushTarget) { SetBufferSyncTile(brushTarget, false); } private void SetBufferSyncTile(GameObject brushTarget, bool active) { if (brushTarget == null || !Tilemap.HasSyncTileCallback()) return; var tilemaps = brushTarget.GetComponentsInChildren(); foreach (var tilemap in tilemaps) { tilemap.bufferSyncTile = active; } } internal static void OnSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing) { if (Event.current.type != EventType.Repaint) return; if (tool == GridBrushBase.Tool.Select || tool == GridBrushBase.Tool.Move) { if (GridSelection.active && !executing) { Color color = Styles.activeColor; GridEditorUtility.DrawGridMarquee(gridLayout, position, color); } } } internal static void OnPaintSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing) { if (Event.current.type != EventType.Repaint) return; Color color = Color.white; if (tool == GridBrushBase.Tool.Pick && executing) color = Color.cyan; if (tool == GridBrushBase.Tool.Paint && executing) color = Color.yellow; if (tool == GridBrushBase.Tool.Select || tool == GridBrushBase.Tool.Move) { if (executing) color = Styles.executingColor; else if (GridSelection.active) color = Styles.activeColor; } if (brushTarget != null) { var targetLayout = brushTarget.GetComponent(); if (targetLayout != null) gridLayout = targetLayout; } if (position.zMin != 0) { var zeroBounds = position; zeroBounds.z = 0; GridEditorUtility.DrawGridMarquee(gridLayout, zeroBounds, color); color = Color.blue; } GridEditorUtility.DrawGridMarquee(gridLayout, position, color); } } }