GenericEditor.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************
  2. * Copyright © 2018 Mogoson. All rights reserved.
  3. *------------------------------------------------------------------------
  4. * File : GenericEditor.cs
  5. * Description : Define generic editor.
  6. *------------------------------------------------------------------------
  7. * Author : Mogoson
  8. * Version : 0.1.0
  9. * Date : 2/26/2018
  10. * Description : Initial development version.
  11. *
  12. * Author : Mogoson
  13. * Version : 0.1.1
  14. * Date : 6/20/2018
  15. * Description : Add method for draw adaptive graph.
  16. *************************************************************************/
  17. using System;
  18. using UnityEditor;
  19. using UnityEngine;
  20. #if UNITY_5_3_OR_NEWER
  21. using UnityEditor.SceneManagement;
  22. #endif
  23. namespace Mogoson.UEditor
  24. {
  25. public class GenericEditor : Editor
  26. {
  27. #region Field and Property
  28. protected readonly Color Blue = new Color(0, 1, 1, 1);
  29. protected readonly Color TransparentBlue = new Color(0, 1, 1, 0.1f);
  30. protected readonly Vector3 MoveSnap = Vector3.one;
  31. #if UNITY_5_5_OR_NEWER
  32. protected readonly Handles.CapFunction CircleCap = Handles.CircleHandleCap;
  33. protected readonly Handles.CapFunction SphereCap = Handles.SphereHandleCap;
  34. #else
  35. protected readonly Handles.DrawCapFunction CircleCap = Handles.CircleCap;
  36. protected readonly Handles.DrawCapFunction SphereCap = Handles.SphereCap;
  37. #endif
  38. protected const float NodeSize = 0.125f;
  39. protected const float AreaRadius = 1.25f;
  40. protected const float ArrowLength = 2f;
  41. protected const float LineLength = 10;
  42. protected const float FixedAreaRadius = 0.5f;
  43. protected const float FixedArrowLength = 0.75f;
  44. #endregion
  45. #region Protected Method
  46. protected void DrawCircleCap(Vector3 position, Quaternion rotation, float size)
  47. {
  48. #if UNITY_5_5_OR_NEWER
  49. if (Event.current.type == EventType.Repaint)
  50. {
  51. CircleCap(0, position, rotation, size, EventType.Repaint);
  52. }
  53. #else
  54. CircleCap(0, position, rotation, size);
  55. #endif
  56. }
  57. protected void DrawAdaptiveCircleCap(Vector3 position, Quaternion rotation, float size)
  58. {
  59. DrawCircleCap(position, rotation, size * GetHandleSize(position));
  60. }
  61. protected void DrawSphereCap(Vector3 position, Quaternion rotation, float size)
  62. {
  63. #if UNITY_5_5_OR_NEWER
  64. if (Event.current.type == EventType.Repaint)
  65. {
  66. SphereCap(0, position, rotation, size, EventType.Repaint);
  67. }
  68. #else
  69. SphereCap(0, position, rotation, size);
  70. #endif
  71. }
  72. protected void DrawAdaptiveSphereCap(Vector3 position, Quaternion rotation, float size)
  73. {
  74. DrawSphereCap(position, rotation, size * GetHandleSize(position));
  75. }
  76. protected void DrawAdaptiveWireArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius)
  77. {
  78. Handles.DrawWireArc(center, normal, from, angle, radius * GetHandleSize(center));
  79. }
  80. protected void DrawAdaptiveSolidArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius)
  81. {
  82. Handles.DrawSolidArc(center, normal, from, angle, radius * GetHandleSize(center));
  83. }
  84. protected void DrawAdaptiveSolidDisc(Vector3 center, Vector3 normal, float radius)
  85. {
  86. Handles.DrawSolidDisc(center, normal, radius * GetHandleSize(center));
  87. }
  88. protected void DrawSphereArrow(Vector3 start, Vector3 end, float size, string text = "")
  89. {
  90. Handles.DrawLine(start, end);
  91. DrawAdaptiveSphereCap(end, Quaternion.identity, size);
  92. Handles.Label(end, text);
  93. }
  94. protected void DrawSphereArrow(Vector3 start, Vector3 direction, float length, float size, string text = "")
  95. {
  96. DrawSphereArrow(start, start + direction.normalized * length, size, text);
  97. }
  98. protected void DrawAdaptiveSphereArrow(Vector3 start, Vector3 direction, float length, float size, string text = "")
  99. {
  100. DrawSphereArrow(start, direction, length * GetHandleSize(start), size, text);
  101. }
  102. protected void DrawPositionHandle(Transform transform)
  103. {
  104. EditorGUI.BeginChangeCheck();
  105. var position = Handles.PositionHandle(transform.position, GetPivotRotation(transform));
  106. if (EditorGUI.EndChangeCheck())
  107. {
  108. Undo.RecordObject(transform, "Change Position");
  109. transform.position = position;
  110. MarkSceneDirty();
  111. }
  112. }
  113. protected void DrawRotationHandle(Transform transform)
  114. {
  115. EditorGUI.BeginChangeCheck();
  116. var rotation = Handles.RotationHandle(transform.rotation, transform.position);
  117. if (EditorGUI.EndChangeCheck())
  118. {
  119. Undo.RecordObject(transform, "Change Rotation");
  120. transform.rotation = rotation;
  121. MarkSceneDirty();
  122. }
  123. }
  124. #if UNITY_5_5_OR_NEWER
  125. protected void DrawFreeMoveHandle(Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.CapFunction capFunc, Action<Vector3> callback)
  126. #else
  127. protected void DrawFreeMoveHandle(Vector3 position, Quaternion rotation, float size, Vector3 snap, Handles.DrawCapFunction capFunc, Action<Vector3> callback)
  128. #endif
  129. {
  130. EditorGUI.BeginChangeCheck();
  131. var newPosition = Handles.FreeMoveHandle(position, Quaternion.identity, GetHandleSize(position) * size, snap, capFunc);
  132. if (EditorGUI.EndChangeCheck())
  133. {
  134. Undo.RecordObject(target, "Move Handle");
  135. callback.Invoke(newPosition);
  136. MarkSceneDirty();
  137. }
  138. }
  139. #if UNITY_5_5_OR_NEWER
  140. protected void DrawAdaptiveButton(Vector3 position, Quaternion direction, float size, float pickSize, Handles.CapFunction capFunc, Action callback)
  141. #else
  142. protected void DrawAdaptiveButton(Vector3 position, Quaternion direction, float size, float pickSize, Handles.DrawCapFunction capFunc, Action callback)
  143. #endif
  144. {
  145. var scale = GetHandleSize(position);
  146. if (Handles.Button(position, direction, size * scale, pickSize * scale, capFunc))
  147. {
  148. Undo.RecordObject(target, "Click Button");
  149. callback.Invoke();
  150. MarkSceneDirty();
  151. }
  152. }
  153. protected float GetHandleSize(Vector3 position)
  154. {
  155. return HandleUtility.GetHandleSize(position);
  156. }
  157. protected Quaternion GetPivotRotation(Transform transform)
  158. {
  159. if (Tools.pivotRotation == PivotRotation.Local)
  160. {
  161. return transform.rotation;
  162. }
  163. else
  164. {
  165. return Quaternion.identity;
  166. }
  167. }
  168. protected void MarkSceneDirty()
  169. {
  170. #if UNITY_5_3_OR_NEWER
  171. EditorSceneManager.MarkAllScenesDirty();
  172. #else
  173. EditorApplication.MarkSceneDirty();
  174. #endif
  175. }
  176. #endregion
  177. }
  178. }