GraphEditor.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace Pathfinding {
  4. public class GraphEditor : GraphEditorBase {
  5. public AstarPathEditor editor;
  6. /// <summary>Stores if the graph is visible or not in the inspector</summary>
  7. public FadeArea fadeArea;
  8. /// <summary>Stores if the graph info box is visible or not in the inspector</summary>
  9. public FadeArea infoFadeArea;
  10. /// <summary>
  11. /// Called by editor scripts to rescan the graphs e.g when the user moved a graph.
  12. /// Will only scan graphs if not playing and time to scan last graph was less than some constant (to avoid lag with large graphs)
  13. /// </summary>
  14. public bool AutoScan () {
  15. if (!Application.isPlaying && AstarPath.active != null && AstarPath.active.lastScanTime < 0.11F) {
  16. AstarPath.active.Scan();
  17. return true;
  18. }
  19. return false;
  20. }
  21. public virtual void OnEnable () {
  22. }
  23. public static Object ObjectField (string label, Object obj, System.Type objType, bool allowSceneObjects, bool assetsMustBeInResourcesFolder) {
  24. return ObjectField(new GUIContent(label), obj, objType, allowSceneObjects, assetsMustBeInResourcesFolder);
  25. }
  26. public static Object ObjectField (GUIContent label, Object obj, System.Type objType, bool allowSceneObjects, bool assetsMustBeInResourcesFolder) {
  27. obj = EditorGUILayout.ObjectField(label, obj, objType, allowSceneObjects);
  28. if (obj != null) {
  29. if (allowSceneObjects && !EditorUtility.IsPersistent(obj)) {
  30. // Object is in the scene
  31. var com = obj as Component;
  32. var go = obj as GameObject;
  33. if (com != null) {
  34. go = com.gameObject;
  35. }
  36. if (go != null && go.GetComponent<UnityReferenceHelper>() == null) {
  37. if (FixLabel("Object's GameObject must have a UnityReferenceHelper component attached")) {
  38. go.AddComponent<UnityReferenceHelper>();
  39. }
  40. }
  41. } else if (EditorUtility.IsPersistent(obj)) {
  42. if (assetsMustBeInResourcesFolder) {
  43. string path = AssetDatabase.GetAssetPath(obj).Replace("\\", "/");
  44. var rg = new System.Text.RegularExpressions.Regex(@"Resources/.*$");
  45. if (!rg.IsMatch(path)) {
  46. if (FixLabel("Object must be in the 'Resources' folder")) {
  47. if (!System.IO.Directory.Exists(Application.dataPath+"/Resources")) {
  48. System.IO.Directory.CreateDirectory(Application.dataPath+"/Resources");
  49. AssetDatabase.Refresh();
  50. }
  51. string ext = System.IO.Path.GetExtension(path);
  52. string error = AssetDatabase.MoveAsset(path, "Assets/Resources/"+obj.name+ext);
  53. if (error == "") {
  54. path = AssetDatabase.GetAssetPath(obj);
  55. } else {
  56. Debug.LogError("Couldn't move asset - "+error);
  57. }
  58. }
  59. }
  60. if (!AssetDatabase.IsMainAsset(obj) && obj.name != AssetDatabase.LoadMainAssetAtPath(path).name) {
  61. if (FixLabel("Due to technical reasons, the main asset must\nhave the same name as the referenced asset")) {
  62. string error = AssetDatabase.RenameAsset(path, obj.name);
  63. if (error != "") {
  64. Debug.LogError("Couldn't rename asset - "+error);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. return obj;
  72. }
  73. /// <summary>Draws common graph settings</summary>
  74. public void OnBaseInspectorGUI (NavGraph target) {
  75. int penalty = EditorGUILayout.IntField(new GUIContent("Initial Penalty", "Initial Penalty for nodes in this graph. Set during Scan."), (int)target.initialPenalty);
  76. if (penalty < 0) penalty = 0;
  77. target.initialPenalty = (uint)penalty;
  78. }
  79. /// <summary>Override to implement graph inspectors</summary>
  80. public virtual void OnInspectorGUI (NavGraph target) {
  81. }
  82. /// <summary>Override to implement scene GUI drawing for the graph</summary>
  83. public virtual void OnSceneGUI (NavGraph target) {
  84. }
  85. /// <summary>Draws a thin separator line</summary>
  86. public static void Separator () {
  87. GUIStyle separator = AstarPathEditor.astarSkin.FindStyle("PixelBox3Separator") ?? new GUIStyle();
  88. Rect r = GUILayoutUtility.GetRect(new GUIContent(), separator);
  89. if (Event.current.type == EventType.Repaint) {
  90. separator.Draw(r, false, false, false, false);
  91. }
  92. }
  93. /// <summary>Draws a small help box with a 'Fix' button to the right. Returns: Boolean - Returns true if the button was clicked</summary>
  94. public static bool FixLabel (string label, string buttonLabel = "Fix", int buttonWidth = 40) {
  95. GUILayout.BeginHorizontal();
  96. GUILayout.Space(14*EditorGUI.indentLevel);
  97. GUILayout.BeginHorizontal(AstarPathEditor.helpBox);
  98. GUILayout.Label(label, EditorGUIUtility.isProSkin ? EditorStyles.whiteMiniLabel : EditorStyles.miniLabel, GUILayout.ExpandWidth(true));
  99. var returnValue = GUILayout.Button(buttonLabel, EditorStyles.miniButton, GUILayout.Width(buttonWidth));
  100. GUILayout.EndHorizontal();
  101. GUILayout.EndHorizontal();
  102. return returnValue;
  103. }
  104. /// <summary>Draws a toggle with a bold label to the right. Does not enable or disable GUI</summary>
  105. public bool ToggleGroup (string label, bool value) {
  106. return ToggleGroup(new GUIContent(label), value);
  107. }
  108. /// <summary>Draws a toggle with a bold label to the right. Does not enable or disable GUI</summary>
  109. public static bool ToggleGroup (GUIContent label, bool value) {
  110. GUILayout.BeginHorizontal();
  111. GUILayout.Space(13*EditorGUI.indentLevel);
  112. value = GUILayout.Toggle(value, "", GUILayout.Width(10));
  113. GUIStyle boxHeader = AstarPathEditor.astarSkin.FindStyle("CollisionHeader");
  114. if (GUILayout.Button(label, boxHeader, GUILayout.Width(100))) {
  115. value = !value;
  116. }
  117. GUILayout.EndHorizontal();
  118. return value;
  119. }
  120. }
  121. }