Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Editor / AIGraphWindow.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.UIElements;
  6. using System.IO;
  7. using UnityEditor.UIElements;
  8. public class AIGraphWindow : EditorWindow
  9. {
  10. public FSMSave saveFile;
  11. public string rootDirectory;
  12. FSMGraphView graphView;
  13. Button saveButton;
  14. [MenuItem("Window/FSM Graph")]
  15. static void Init()
  16. {
  17. AIGraphWindow window = (AIGraphWindow)GetWindow<AIGraphWindow>("FSM Graph");
  18. window.Show();
  19. }
  20. private void OnEnable()
  21. {
  22. // AddGraphView();
  23. AddGraphView();
  24. }
  25. private void AddGraphView()
  26. {
  27. graphView = new FSMGraphView();
  28. graphView.instance = this;
  29. graphView.StretchToParentSize();
  30. rootVisualElement.Add(graphView);
  31. AddToolbar();
  32. }
  33. private void OnGUI()
  34. {
  35. // graphView.OnGUI();
  36. /* if (saveFile == null)
  37. {
  38. if (GUILayout.Button("Create FSM"))
  39. {
  40. string path = EditorUtility.OpenFolderPanel("Choose save folder", Application.dataPath, string.Empty);
  41. string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
  42. rootDirectory = localPath;
  43. if (path != string.Empty)
  44. {
  45. if (!Directory.Exists(Path.Combine(localPath, "States")))
  46. {
  47. AssetDatabase.CreateFolder(localPath, "States");
  48. }
  49. if (!Directory.Exists(Path.Combine(localPath, "Transitions")))
  50. {
  51. AssetDatabase.CreateFolder(localPath, "Transitions");
  52. }
  53. if (!File.Exists(Path.Combine(localPath, "Nodes.asset")))
  54. {
  55. FSMSave sv = ScriptableObject.CreateInstance<FSMSave>();
  56. if (sv.states == null)
  57. {
  58. sv.states = new List<FSMSave.SavedElement>();
  59. }
  60. AssetDatabase.CreateAsset(sv, Path.Combine(localPath, "Nodes.asset"));
  61. }
  62. }
  63. }
  64. if (GUILayout.Button("Load FSM"))
  65. {
  66. string path = EditorUtility.OpenFilePanel("Choose save file", Application.dataPath, string.Empty);
  67. if (path != string.Empty)
  68. {
  69. string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
  70. rootDirectory = Path.GetDirectoryName(localPath);
  71. saveFile = AssetDatabase.LoadAssetAtPath<FSMSave>(localPath);
  72. AddGraphView();
  73. AddToolbar();
  74. graphView.Load(saveFile);
  75. }
  76. }
  77. }
  78. */
  79. if (saveButton != null)
  80. {
  81. if (saveFile == null)
  82. {
  83. saveButton.visible = false;
  84. } else
  85. {
  86. saveButton.visible = true;
  87. }
  88. }
  89. }
  90. public void CreateFSM()
  91. {
  92. string path = EditorUtility.OpenFolderPanel("Choose save folder", Application.dataPath, string.Empty);
  93. if (path == string.Empty) return;
  94. string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
  95. rootDirectory = localPath;
  96. if (path != string.Empty)
  97. {
  98. if (!Directory.Exists(Path.Combine(localPath, "States")))
  99. {
  100. AssetDatabase.CreateFolder(localPath, "States");
  101. }
  102. if (!Directory.Exists(Path.Combine(localPath, "Transitions")))
  103. {
  104. AssetDatabase.CreateFolder(localPath, "Transitions");
  105. }
  106. if (!File.Exists(Path.Combine(localPath, "Nodes.asset")))
  107. {
  108. FSMSave sv = ScriptableObject.CreateInstance<FSMSave>();
  109. if (sv.states == null)
  110. {
  111. sv.states = new List<FSMSave.SavedElement>();
  112. }
  113. AssetDatabase.CreateAsset(sv, Path.Combine(localPath, "Nodes.asset"));
  114. saveFile = sv;
  115. graphView.Load(sv);
  116. }
  117. }
  118. }
  119. public void LoadFSM()
  120. {
  121. string path = EditorUtility.OpenFilePanel("Choose save file", Application.dataPath, string.Empty);
  122. if (path != string.Empty)
  123. {
  124. string localPath = Path.Combine("Assets", Path.GetRelativePath(Application.dataPath, path));
  125. rootDirectory = Path.GetDirectoryName(localPath);
  126. saveFile = AssetDatabase.LoadAssetAtPath<FSMSave>(localPath);
  127. graphView.Load(saveFile);
  128. }
  129. }
  130. void AddToolbar()
  131. {
  132. Toolbar toolbar = new Toolbar();
  133. Label t = new Label(rootDirectory);
  134. saveButton = new Button(() =>
  135. {
  136. graphView.Save();
  137. t.text = rootDirectory;
  138. });
  139. saveButton.text = "Save";
  140. Button newFSMButton = new Button(() =>
  141. {
  142. CreateFSM();
  143. t.text = rootDirectory;
  144. });
  145. newFSMButton.text = "New";
  146. Button loadButton = new Button(() =>
  147. {
  148. LoadFSM();
  149. t.text = rootDirectory;
  150. });
  151. loadButton.text = "Load";
  152. Button editTransitionButton = new Button(()=> {
  153. TransitionEditorWindow.Init(graphView.OpenTransitions());
  154. });
  155. editTransitionButton.text = "Edit Selected Transition";
  156. toolbar.Add(t);
  157. toolbar.Add(saveButton);
  158. toolbar.Add(loadButton);
  159. toolbar.Add(newFSMButton);
  160. toolbar.Add(editTransitionButton);
  161. rootVisualElement.Add(toolbar);
  162. }
  163. }