GraphUpdateSceneEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace Pathfinding {
  5. /// <summary>Editor for GraphUpdateScene</summary>
  6. [CustomEditor(typeof(GraphUpdateScene))]
  7. [CanEditMultipleObjects]
  8. public class GraphUpdateSceneEditor : EditorBase {
  9. int selectedPoint = -1;
  10. const float pointGizmosRadius = 0.09F;
  11. static Color PointColor = new Color(1, 0.36F, 0, 0.6F);
  12. static Color PointSelectedColor = new Color(1, 0.24F, 0, 1.0F);
  13. GraphUpdateScene[] scripts;
  14. protected override void Inspector () {
  15. // Find all properties
  16. var points = FindProperty("points");
  17. var legacyMode = FindProperty("legacyMode");
  18. // Get a list of inspected components
  19. scripts = new GraphUpdateScene[targets.Length];
  20. targets.CopyTo(scripts, 0);
  21. EditorGUI.BeginChangeCheck();
  22. // Make sure no point arrays are null
  23. for (int i = 0; i < scripts.Length; i++) {
  24. scripts[i].points = scripts[i].points ?? new Vector3[0];
  25. }
  26. if (!points.hasMultipleDifferentValues && points.arraySize == 0) {
  27. if (scripts[0].GetComponent<PolygonCollider2D>() != null) {
  28. EditorGUILayout.HelpBox("Using polygon collider shape", MessageType.Info);
  29. } else if (scripts[0].GetComponent<Collider>() != null || scripts[0].GetComponent<Collider2D>() != null) {
  30. EditorGUILayout.HelpBox("No points, using collider.bounds", MessageType.Info);
  31. } else if (scripts[0].GetComponent<Renderer>() != null) {
  32. EditorGUILayout.HelpBox("No points, using renderer.bounds", MessageType.Info);
  33. } else {
  34. EditorGUILayout.HelpBox("No points and no collider or renderer attached, will not affect anything\nPoints can be added using the transform tool and holding shift", MessageType.Warning);
  35. }
  36. }
  37. DrawPointsField();
  38. EditorGUI.indentLevel = 0;
  39. DrawPhysicsField();
  40. PropertyField("updateErosion", null, "Recalculate erosion for grid graphs.\nSee online documentation for more info");
  41. DrawConvexField();
  42. // Minimum bounds height is not applied when using the bounds from a collider or renderer
  43. if (points.hasMultipleDifferentValues || points.arraySize > 0) {
  44. FloatField("minBoundsHeight", min: 0.1f);
  45. }
  46. PropertyField("applyOnStart");
  47. PropertyField("applyOnScan");
  48. DrawWalkableField();
  49. DrawPenaltyField();
  50. DrawTagField();
  51. EditorGUILayout.Separator();
  52. if (legacyMode.hasMultipleDifferentValues || legacyMode.boolValue) {
  53. EditorGUILayout.HelpBox("Legacy mode is enabled because you have upgraded from an earlier version of the A* Pathfinding Project. " +
  54. "Disabling legacy mode is recommended but you may have to tweak the point locations or object rotation in some cases", MessageType.Warning);
  55. if (GUILayout.Button("Disable Legacy Mode")) {
  56. for (int i = 0; i < scripts.Length; i++) {
  57. Undo.RecordObject(scripts[i], "Disable Legacy Mode");
  58. scripts[i].DisableLegacyMode();
  59. }
  60. }
  61. }
  62. if (scripts.Length == 1 && scripts[0].points.Length >= 3) {
  63. var size = scripts[0].GetBounds().size;
  64. if (Mathf.Min(Mathf.Min(Mathf.Abs(size.x), Mathf.Abs(size.y)), Mathf.Abs(size.z)) < 0.05f) {
  65. EditorGUILayout.HelpBox("The bounding box is very thin. Your shape might be oriented incorrectly. The shape will be projected down on the XZ plane in local space. Rotate this object " +
  66. "so that the local XZ plane corresponds to the plane in which you want to create your shape. For example if you want to create your shape in the XY plane then " +
  67. "this object should have the rotation (-90,0,0). You will need to recreate your shape after rotating this object.", MessageType.Warning);
  68. }
  69. }
  70. if (GUILayout.Button("Clear all points")) {
  71. for (int i = 0; i < scripts.Length; i++) {
  72. Undo.RecordObject(scripts[i], "Clear points");
  73. scripts[i].points = new Vector3[0];
  74. scripts[i].RecalcConvex();
  75. }
  76. }
  77. if (EditorGUI.EndChangeCheck()) {
  78. for (int i = 0; i < scripts.Length; i++) {
  79. EditorUtility.SetDirty(scripts[i]);
  80. }
  81. // Repaint the scene view if necessary
  82. if (!Application.isPlaying || EditorApplication.isPaused) SceneView.RepaintAll();
  83. }
  84. }
  85. void DrawPointsField () {
  86. EditorGUI.BeginChangeCheck();
  87. PropertyField("points");
  88. if (EditorGUI.EndChangeCheck()) {
  89. serializedObject.ApplyModifiedProperties();
  90. for (int i = 0; i < scripts.Length; i++) {
  91. scripts[i].RecalcConvex();
  92. }
  93. HandleUtility.Repaint();
  94. }
  95. }
  96. void DrawPhysicsField () {
  97. if (PropertyField("updatePhysics", "Update Physics", "Perform similar calculations on the nodes as during scan.\n" +
  98. "Grid Graphs will update the position of the nodes and also check walkability using collision.\nSee online documentation for more info.")) {
  99. EditorGUI.indentLevel++;
  100. PropertyField("resetPenaltyOnPhysics");
  101. EditorGUI.indentLevel--;
  102. }
  103. }
  104. void DrawConvexField () {
  105. EditorGUI.BeginChangeCheck();
  106. PropertyField("convex");
  107. if (EditorGUI.EndChangeCheck()) {
  108. serializedObject.ApplyModifiedProperties();
  109. for (int i = 0; i < scripts.Length; i++) {
  110. scripts[i].RecalcConvex();
  111. }
  112. HandleUtility.Repaint();
  113. }
  114. }
  115. void DrawWalkableField () {
  116. if (PropertyField("modifyWalkability")) {
  117. EditorGUI.indentLevel++;
  118. PropertyField("setWalkability", "Walkability Value");
  119. EditorGUI.indentLevel--;
  120. }
  121. }
  122. void DrawPenaltyField () {
  123. PropertyField("penaltyDelta", "Penalty Delta");
  124. if (!FindProperty("penaltyDelta").hasMultipleDifferentValues && FindProperty("penaltyDelta").intValue < 0) {
  125. EditorGUILayout.HelpBox("Be careful when lowering the penalty. Negative penalties are not supported and will instead underflow and get really high.\n" +
  126. "You can set an initial penalty on graphs (see their settings) and then lower them like this to get regions which are easier to traverse.", MessageType.Warning);
  127. }
  128. }
  129. void DrawTagField () {
  130. if (PropertyField("modifyTag")) {
  131. var tagValue = FindProperty("setTag");
  132. EditorGUI.indentLevel++;
  133. EditorGUI.showMixedValue = tagValue.hasMultipleDifferentValues;
  134. EditorGUI.BeginChangeCheck();
  135. var newTag = EditorGUILayoutx.TagField("Tag Value", tagValue.intValue, () => AstarPathEditor.EditTags());
  136. if (EditorGUI.EndChangeCheck()) {
  137. tagValue.intValue = newTag;
  138. }
  139. if (GUILayout.Button("Tags can be used to restrict which units can walk on what ground. Click here for more info", "HelpBox")) {
  140. Application.OpenURL(AstarUpdateChecker.GetURL("tags"));
  141. }
  142. EditorGUI.indentLevel--;
  143. }
  144. }
  145. static void SphereCap (int controlID, Vector3 position, Quaternion rotation, float size) {
  146. #if UNITY_5_5_OR_NEWER
  147. Handles.SphereHandleCap(controlID, position, rotation, size, Event.current.type);
  148. #else
  149. Handles.SphereCap(controlID, position, rotation, size);
  150. #endif
  151. }
  152. public void OnSceneGUI () {
  153. var script = target as GraphUpdateScene;
  154. // Don't allow editing unless it is the active object
  155. if (Selection.activeGameObject != script.gameObject || script.legacyMode) return;
  156. // Make sure the points array is not null
  157. if (script.points == null) {
  158. script.points = new Vector3[0];
  159. EditorUtility.SetDirty(script);
  160. }
  161. List<Vector3> points = Pathfinding.Util.ListPool<Vector3>.Claim();
  162. points.AddRange(script.points);
  163. Matrix4x4 invMatrix = script.transform.worldToLocalMatrix;
  164. Matrix4x4 matrix = script.transform.localToWorldMatrix;
  165. for (int i = 0; i < points.Count; i++) points[i] = matrix.MultiplyPoint3x4(points[i]);
  166. if (Tools.current != Tool.View && Event.current.type == EventType.Layout) {
  167. for (int i = 0; i < script.points.Length; i++) {
  168. HandleUtility.AddControl(-i - 1, HandleUtility.DistanceToLine(points[i], points[i]));
  169. }
  170. }
  171. if (Tools.current != Tool.View)
  172. HandleUtility.AddDefaultControl(0);
  173. for (int i = 0; i < points.Count; i++) {
  174. if (i == selectedPoint && Tools.current == Tool.Move) {
  175. Handles.color = PointSelectedColor;
  176. SphereCap(-i-1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i])*pointGizmosRadius*2);
  177. Vector3 pre = points[i];
  178. Vector3 post = Handles.PositionHandle(points[i], Quaternion.identity);
  179. if (pre != post) {
  180. Undo.RecordObject(script, "Moved Point");
  181. script.points[i] = invMatrix.MultiplyPoint3x4(post);
  182. }
  183. } else {
  184. Handles.color = PointColor;
  185. SphereCap(-i-1, points[i], Quaternion.identity, HandleUtility.GetHandleSize(points[i])*pointGizmosRadius);
  186. }
  187. }
  188. if (Event.current.type == EventType.MouseDown) {
  189. int pre = selectedPoint;
  190. selectedPoint = -(HandleUtility.nearestControl+1);
  191. if (pre != selectedPoint) GUI.changed = true;
  192. }
  193. if (Event.current.shift && Tools.current == Tool.Move) {
  194. HandleUtility.Repaint();
  195. if (((int)Event.current.modifiers & (int)EventModifiers.Alt) != 0) {
  196. if (Event.current.type == EventType.MouseDown && selectedPoint >= 0 && selectedPoint < points.Count) {
  197. Undo.RecordObject(script, "Removed Point");
  198. var arr = new List<Vector3>(script.points);
  199. arr.RemoveAt(selectedPoint);
  200. points.RemoveAt(selectedPoint);
  201. script.points = arr.ToArray();
  202. GUI.changed = true;
  203. } else if (points.Count > 0) {
  204. var index = -(HandleUtility.nearestControl+1);
  205. if (index >= 0 && index < points.Count) {
  206. Handles.color = Color.red;
  207. SphereCap(0, points[index], Quaternion.identity, HandleUtility.GetHandleSize(points[index])*2f*pointGizmosRadius);
  208. }
  209. }
  210. } else {
  211. // Find the closest segment
  212. int insertionIndex = points.Count;
  213. float minDist = float.PositiveInfinity;
  214. for (int i = 0; i < points.Count; i++) {
  215. float dist = HandleUtility.DistanceToLine(points[i], points[(i+1)%points.Count]);
  216. if (dist < minDist) {
  217. insertionIndex = i + 1;
  218. minDist = dist;
  219. }
  220. }
  221. var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
  222. System.Object hit = HandleUtility.RaySnap(ray);
  223. Vector3 rayhit = Vector3.zero;
  224. bool didHit = false;
  225. if (hit != null) {
  226. rayhit = ((RaycastHit)hit).point;
  227. didHit = true;
  228. } else {
  229. var plane = new Plane(script.transform.up, script.transform.position);
  230. float distance;
  231. plane.Raycast(ray, out distance);
  232. if (distance > 0) {
  233. rayhit = ray.GetPoint(distance);
  234. didHit = true;
  235. }
  236. }
  237. if (didHit) {
  238. if (Event.current.type == EventType.MouseDown) {
  239. points.Insert(insertionIndex, rayhit);
  240. Undo.RecordObject(script, "Added Point");
  241. var arr = new List<Vector3>(script.points);
  242. arr.Insert(insertionIndex, invMatrix.MultiplyPoint3x4(rayhit));
  243. script.points = arr.ToArray();
  244. GUI.changed = true;
  245. } else if (points.Count > 0) {
  246. Handles.color = Color.green;
  247. Handles.DrawDottedLine(points[(insertionIndex-1 + points.Count) % points.Count], rayhit, 8);
  248. Handles.DrawDottedLine(points[insertionIndex % points.Count], rayhit, 8);
  249. SphereCap(0, rayhit, Quaternion.identity, HandleUtility.GetHandleSize(rayhit)*pointGizmosRadius);
  250. // Project point down onto a plane
  251. var zeroed = invMatrix.MultiplyPoint3x4(rayhit);
  252. zeroed.y = 0;
  253. Handles.color = new Color(1, 1, 1, 0.5f);
  254. Handles.DrawDottedLine(matrix.MultiplyPoint3x4(zeroed), rayhit, 4);
  255. }
  256. }
  257. }
  258. if (Event.current.type == EventType.MouseDown) {
  259. Event.current.Use();
  260. }
  261. }
  262. // Make sure the convex hull stays up to date
  263. script.RecalcConvex();
  264. Pathfinding.Util.ListPool<Vector3>.Release(ref points);
  265. if (GUI.changed) HandleUtility.Repaint();
  266. }
  267. }
  268. }