暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridBrushEditorBase.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using UnityEngine;
  2. using UnityEngine.Scripting.APIUpdating;
  3. using UnityEngine.Tilemaps;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. /// <summary>Base class for Grid Brush Editor.</summary>
  7. [MovedFrom(true, "UnityEditor", "UnityEditor")]
  8. [CustomEditor(typeof(GridBrushBase))]
  9. public class GridBrushEditorBase : Editor
  10. {
  11. private static class Styles
  12. {
  13. public static readonly Color activeColor = new Color(1f, .5f, 0f);
  14. public static readonly Color executingColor = new Color(1f, .75f, 0.25f);
  15. }
  16. /// <summary>Checks if the Brush allows the changing of Z Position.</summary>
  17. /// <returns>Whether the Brush can change Z Position.</returns>
  18. public virtual bool canChangeZPosition
  19. {
  20. get { return true; }
  21. set {}
  22. }
  23. /// <summary>Callback for painting the GUI for the GridBrush in the Scene view.</summary>
  24. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  25. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  26. /// <param name="position">Current selected location of the brush.</param>
  27. /// <param name="tool">Current GridBrushBase::ref::Tool selected.</param>
  28. /// <param name="executing">Whether is brush is being used.</param>
  29. /// <remarks>Implement this for any special behaviours when the GridBrush is used on the Scene View.</remarks>
  30. public virtual void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  31. {
  32. OnPaintSceneGUIInternal(gridLayout, brushTarget, position, tool, executing);
  33. }
  34. /// <summary>Callback for painting the inspector GUI for the GridBrush in the tilemap palette.</summary>
  35. /// <remarks>Implement this to have a custom editor in the tilemap palette for the GridBrush.</remarks>
  36. public virtual void OnPaintInspectorGUI()
  37. {
  38. OnInspectorGUI();
  39. }
  40. /// <summary>Callback for drawing the Inspector GUI when there is an active GridSelection made in a GridLayout.</summary>
  41. /// <remarks>Override this to show custom Inspector GUI for the current selection.</remarks>
  42. public virtual void OnSelectionInspectorGUI() {}
  43. /// <summary>Callback for painting custom gizmos when there is an active GridSelection made in a GridLayout.</summary>
  44. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  45. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  46. /// <remarks>Override this to show custom gizmos for the current selection.</remarks>
  47. public virtual void OnSelectionSceneGUI(GridLayout gridLayout, GameObject brushTarget) {}
  48. /// <summary>
  49. /// Callback for painting custom gizmos for the GridBrush for the brush target
  50. /// </summary>
  51. /// <param name="gridLayout">Grid that the brush is being used on.</param>
  52. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  53. /// <remarks>Override this to show custom gizmos for the brush target.</remarks>
  54. public virtual void OnSceneGUI(GridLayout gridLayout, GameObject brushTarget) {}
  55. /// <summary>Callback when the mouse cursor leaves a paintable region.</summary>
  56. /// <remarks>Implement this for any custom behaviour when the mouse cursor leaves a paintable region.</remarks>
  57. public virtual void OnMouseLeave() {}
  58. /// <summary>Callback when the mouse cursor enters a paintable region.</summary>
  59. /// <remarks>Implement this for any custom behaviour when the mouse cursor enters a paintable region.</remarks>
  60. public virtual void OnMouseEnter() {}
  61. /// <summary>Callback when a GridBrushBase.Tool is activated.</summary>
  62. /// <param name="tool">Tool that is activated.</param>
  63. /// <remarks>Implement this for any special behaviours when a Tool is activated.</remarks>
  64. public virtual void OnToolActivated(GridBrushBase.Tool tool) {}
  65. /// <summary>Callback when a GridBrushBase.Tool is deactivated.</summary>
  66. /// <param name="tool">Tool that is deactivated.</param>
  67. /// <remarks>Implement this for any special behaviours when a Tool is deactivated.</remarks>
  68. public virtual void OnToolDeactivated(GridBrushBase.Tool tool) {}
  69. /// <summary>Callback for registering an Undo action before the GridBrushBase does the current GridBrushBase::ref::Tool action.</summary>
  70. /// <param name="brushTarget">Target of the GridBrushBase::ref::Tool operation. By default the currently selected GameObject.</param>
  71. /// <param name="tool">Current GridBrushBase::ref::Tool selected.</param>
  72. /// <remarks>Implement this for any special Undo behaviours when a brush is used.</remarks>
  73. public virtual void RegisterUndo(GameObject brushTarget, GridBrushBase.Tool tool) {}
  74. /// <summary>Returns all valid targets that the brush can edit.</summary>
  75. public virtual GameObject[] validTargets { get { return null; } }
  76. internal void OnEditStart(GridLayout gridLayout, GameObject brushTarget)
  77. {
  78. SetBufferSyncTile(brushTarget, true);
  79. }
  80. internal void OnEditEnd(GridLayout gridLayout, GameObject brushTarget)
  81. {
  82. SetBufferSyncTile(brushTarget, false);
  83. }
  84. private void SetBufferSyncTile(GameObject brushTarget, bool active)
  85. {
  86. if (brushTarget == null || !Tilemap.HasSyncTileCallback())
  87. return;
  88. var tilemaps = brushTarget.GetComponentsInChildren<Tilemap>();
  89. foreach (var tilemap in tilemaps)
  90. {
  91. tilemap.bufferSyncTile = active;
  92. }
  93. }
  94. internal static void OnSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  95. {
  96. if (Event.current.type != EventType.Repaint)
  97. return;
  98. if (tool == GridBrushBase.Tool.Select ||
  99. tool == GridBrushBase.Tool.Move)
  100. {
  101. if (GridSelection.active && !executing)
  102. {
  103. Color color = Styles.activeColor;
  104. GridEditorUtility.DrawGridMarquee(gridLayout, position, color);
  105. }
  106. }
  107. }
  108. internal static void OnPaintSceneGUIInternal(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, GridBrushBase.Tool tool, bool executing)
  109. {
  110. if (Event.current.type != EventType.Repaint)
  111. return;
  112. Color color = Color.white;
  113. if (tool == GridBrushBase.Tool.Pick && executing)
  114. color = Color.cyan;
  115. if (tool == GridBrushBase.Tool.Paint && executing)
  116. color = Color.yellow;
  117. if (tool == GridBrushBase.Tool.Select ||
  118. tool == GridBrushBase.Tool.Move)
  119. {
  120. if (executing)
  121. color = Styles.executingColor;
  122. else if (GridSelection.active)
  123. color = Styles.activeColor;
  124. }
  125. if (brushTarget != null)
  126. {
  127. var targetLayout = brushTarget.GetComponent<GridLayout>();
  128. if (targetLayout != null)
  129. gridLayout = targetLayout;
  130. }
  131. if (position.zMin != 0)
  132. {
  133. var zeroBounds = position;
  134. zeroBounds.z = 0;
  135. GridEditorUtility.DrawGridMarquee(gridLayout, zeroBounds, color);
  136. color = Color.blue;
  137. }
  138. GridEditorUtility.DrawGridMarquee(gridLayout, position, color);
  139. }
  140. }
  141. }