暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

EmptyGraphWindow.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEditor.SceneManagement;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityObject = UnityEngine.Object;
  9. namespace Unity.VisualScripting
  10. {
  11. internal class DropdownOptions
  12. {
  13. internal bool interactable = true;
  14. internal string label;
  15. internal string tooltip;
  16. internal Action callback;
  17. }
  18. internal class SplitDropdown
  19. {
  20. private bool reset;
  21. private List<DropdownOptions> listOfOptions;
  22. internal SplitDropdown(List<DropdownOptions> listOfOptions)
  23. {
  24. this.listOfOptions = listOfOptions;
  25. }
  26. internal DropdownOptions GetOption(int index)
  27. {
  28. return listOfOptions[index];
  29. }
  30. internal void Reset()
  31. {
  32. reset = true;
  33. }
  34. internal bool Draw(Rect area, string label, ref bool toggleState)
  35. {
  36. GUI.Box(area, string.Empty, EmptyGraphWindow.buttonStyle);
  37. if (GUI.Button(new Rect(area.x, area.y, area.width - area.height, area.height), label,
  38. EmptyGraphWindow.labelStyleButton))
  39. {
  40. toggleState = false;
  41. return true;
  42. }
  43. Rect toggleRect = new Rect(area.x + area.width - area.height - 2, area.y - 1, area.height + 3,
  44. area.height + 3);
  45. toggleState = GUI.Toggle(toggleRect, toggleState, "", EmptyGraphWindow.labelStyleButton);
  46. GUI.Label(toggleRect, EmptyGraphWindow.dropdownIcon, EmptyGraphWindow.titleStyle);
  47. if (toggleState)
  48. {
  49. GUI.BeginGroup(new Rect(area.x, area.y + area.height - 1, area.width, area.height * 2),
  50. EmptyGraphWindow.buttonStyle);
  51. Rect areaChild = new Rect(0, 0, EmptyGraphWindow.buttonWidth,
  52. EmptyGraphWindow.buttonHeight);
  53. GUIContent contentOption;
  54. foreach (DropdownOptions dropdownOption in listOfOptions)
  55. {
  56. EditorGUI.BeginDisabledGroup(!dropdownOption.interactable);
  57. if (dropdownOption.interactable)
  58. {
  59. contentOption = new GUIContent(dropdownOption.label);
  60. }
  61. else
  62. {
  63. contentOption = new GUIContent(dropdownOption.label, dropdownOption.tooltip);
  64. }
  65. if (GUI.Button(areaChild, contentOption, EmptyGraphWindow.labelStyleDropdownOptions))
  66. {
  67. toggleState = false;
  68. dropdownOption.callback();
  69. }
  70. EditorGUI.EndDisabledGroup();
  71. areaChild.y += areaChild.height;
  72. }
  73. GUI.EndGroup();
  74. }
  75. if (reset)
  76. {
  77. toggleState = false;
  78. reset = false;
  79. }
  80. return false;
  81. }
  82. }
  83. internal class EmptyGraphWindow : EditorWindow
  84. {
  85. internal static GUIContent dropdownIcon;
  86. private bool toggleOnScript = false;
  87. private bool toggleOnState = false;
  88. internal static GUIStyle buttonStyle;
  89. internal static GUIStyle labelStyleButton;
  90. internal static GUIStyle labelStyleDropdownOptions;
  91. internal static GUIStyle titleStyle;
  92. internal static int titleHeight = 120;
  93. internal static int buttonWidth = 200;
  94. internal static int buttonHeight = 22;
  95. private bool shouldCloseWindow;
  96. private Vector2 scrollPosition;
  97. private Rect scrollArea;
  98. private SplitDropdown splitDropdownScriptGraph;
  99. private SplitDropdown splitDropdownStateGraph;
  100. const string k_OnSelectedGameObject = "...on selected GameObject";
  101. const string k_OnNewGameObject = "...on new GameObject";
  102. const string k_SelectedGameObject = "Please, select a GameObject";
  103. [MenuItem("Window/Visual Scripting/Visual Scripting Graph", false, 3010)]
  104. private static void ShowWindow()
  105. {
  106. EmptyGraphWindow window = GetWindow<EmptyGraphWindow>();
  107. window.titleContent = new GUIContent("Visual Scripting");
  108. }
  109. private void OnEnable()
  110. {
  111. string pathRoot = PathUtility.GetPackageRootPath();
  112. UnityObject icon = EditorGUIUtility.Load(Path.Combine(pathRoot,
  113. "Editor/VisualScripting.Shared/EditorAssetResources/SplitButtonArrow.png"));
  114. dropdownIcon = new GUIContent(icon as Texture2D);
  115. scrollArea = new Rect(0, 0, 630, 300);
  116. toggleOnScript = false;
  117. toggleOnState = false;
  118. shouldCloseWindow = false;
  119. var listOfOptions = new List<DropdownOptions>
  120. {
  121. new DropdownOptions
  122. {
  123. label = k_OnSelectedGameObject,
  124. tooltip = k_SelectedGameObject,
  125. callback = CreateScriptGraphOnSelectedGameObject
  126. },
  127. new DropdownOptions
  128. {
  129. label = k_OnNewGameObject,
  130. callback = CreateScriptGraphOnNewGameObject
  131. }
  132. };
  133. splitDropdownScriptGraph = new SplitDropdown(listOfOptions);
  134. listOfOptions = new List<DropdownOptions>
  135. {
  136. new DropdownOptions
  137. {
  138. label = k_OnSelectedGameObject,
  139. tooltip = k_SelectedGameObject,
  140. callback = CreateStateGraphOnSelectedGameObject
  141. },
  142. new DropdownOptions
  143. {
  144. label = k_OnNewGameObject,
  145. callback = CreateStateGraphOnNewGameObject
  146. }
  147. };
  148. splitDropdownStateGraph = new SplitDropdown(listOfOptions);
  149. }
  150. private void OpenGraphAsset(UnityObject unityObject, bool shouldSetSceneAsDirty)
  151. {
  152. shouldCloseWindow = true;
  153. ScriptGraphAsset scriptGraphAsset = unityObject as ScriptGraphAsset;
  154. GraphReference graphReference = null;
  155. if (scriptGraphAsset != null)
  156. {
  157. graphReference = GraphReference.New(scriptGraphAsset, true);
  158. }
  159. else
  160. {
  161. StateGraphAsset stateGraphAsset = unityObject as StateGraphAsset;
  162. if (stateGraphAsset != null)
  163. {
  164. graphReference = GraphReference.New(stateGraphAsset, true);
  165. }
  166. }
  167. if (shouldSetSceneAsDirty)
  168. {
  169. EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
  170. }
  171. GraphWindow.OpenActive(graphReference);
  172. }
  173. private void OpenGraphFromPath(string path, bool shouldSetSceneAsDirty)
  174. {
  175. path = path.Replace(Application.dataPath, "Assets");
  176. UnityObject unityObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityObject));
  177. OpenGraphAsset(unityObject, shouldSetSceneAsDirty);
  178. }
  179. private void OpenGraph()
  180. {
  181. EditorGUIUtility.ShowObjectPicker<MacroScriptableObject>(null, false, String.Empty,
  182. EditorGUIUtility.GetControlID(FocusType.Passive));
  183. }
  184. private bool CreateScriptGraphAsset(GameObject gameObject = null, bool updateName = false)
  185. {
  186. var path = EditorUtility.SaveFilePanelInProject("Save Graph", "New Script Graph", "asset", null);
  187. if (string.IsNullOrEmpty(path))
  188. {
  189. return false;
  190. }
  191. VSUsageUtility.isVisualScriptingUsed = true;
  192. var macro = (IMacro)CreateInstance(typeof(ScriptGraphAsset));
  193. var macroObject = (UnityObject)macro;
  194. macro.graph = FlowGraph.WithStartUpdate();
  195. if (gameObject != null)
  196. {
  197. ScriptMachine flowMachine = gameObject.AddComponent<ScriptMachine>();
  198. flowMachine.nest.macro = (ScriptGraphAsset)macro;
  199. }
  200. string filename = Path.GetFileNameWithoutExtension(path);
  201. if (updateName)
  202. {
  203. gameObject.name = filename;
  204. }
  205. macroObject.name = filename;
  206. AssetDatabase.CreateAsset(macroObject, path);
  207. bool shouldSetSceneAsDirty = gameObject != null;
  208. OpenGraphFromPath(path, shouldSetSceneAsDirty);
  209. return true;
  210. }
  211. private void CreateScriptGraph()
  212. {
  213. Selection.activeGameObject = null;
  214. if (CreateScriptGraphAsset())
  215. {
  216. shouldCloseWindow = true;
  217. }
  218. }
  219. private void CreateScriptGraphOnNewGameObject()
  220. {
  221. Selection.activeGameObject = null;
  222. GameObject newGameObject = new GameObject();
  223. if (!CreateScriptGraphAsset(newGameObject, true))
  224. {
  225. DestroyImmediate(newGameObject);
  226. }
  227. }
  228. private void CreateScriptGraphOnSelectedGameObject()
  229. {
  230. if (Selection.activeGameObject != null)
  231. {
  232. if (CreateScriptGraphAsset(Selection.activeGameObject))
  233. {
  234. shouldCloseWindow = true;
  235. }
  236. }
  237. }
  238. private bool CreateStateGraphAsset(GameObject gameObject = null, bool updateName = false)
  239. {
  240. var path = EditorUtility.SaveFilePanelInProject("Save Graph", "New State Graph", "asset", null);
  241. if (string.IsNullOrEmpty(path))
  242. {
  243. return false;
  244. }
  245. VSUsageUtility.isVisualScriptingUsed = true;
  246. var macro = (IMacro)CreateInstance(typeof(StateGraphAsset));
  247. var macroObject = (UnityObject)macro;
  248. macro.graph = StateGraph.WithStart();
  249. if (gameObject != null)
  250. {
  251. StateMachine stateMachine = gameObject.AddComponent<StateMachine>();
  252. stateMachine.nest.macro = (StateGraphAsset)macro;
  253. }
  254. string filename = Path.GetFileNameWithoutExtension(path);
  255. if (updateName)
  256. {
  257. gameObject.name = filename;
  258. }
  259. macroObject.name = filename;
  260. AssetDatabase.CreateAsset(macroObject, path);
  261. bool shouldSetSceneAsDirty = gameObject != null;
  262. OpenGraphFromPath(path, shouldSetSceneAsDirty);
  263. return true;
  264. }
  265. private void CreateStateGraph()
  266. {
  267. Selection.activeGameObject = null;
  268. if (CreateStateGraphAsset())
  269. {
  270. shouldCloseWindow = true;
  271. }
  272. }
  273. private void CreateStateGraphOnNewGameObject()
  274. {
  275. Selection.activeGameObject = null;
  276. GameObject newGameObject = new GameObject();
  277. if (!CreateStateGraphAsset(newGameObject, true))
  278. {
  279. DestroyImmediate(newGameObject);
  280. }
  281. }
  282. private void CreateStateGraphOnSelectedGameObject()
  283. {
  284. if (Selection.activeGameObject != null)
  285. {
  286. if (CreateStateGraphAsset(Selection.activeGameObject))
  287. {
  288. shouldCloseWindow = true;
  289. }
  290. }
  291. }
  292. private void CreateStyles()
  293. {
  294. if (buttonStyle == null)
  295. {
  296. buttonStyle = new GUIStyle("Button");
  297. }
  298. if (labelStyleButton == null)
  299. {
  300. labelStyleButton = new GUIStyle("Label") { alignment = TextAnchor.MiddleCenter };
  301. }
  302. if (labelStyleDropdownOptions == null)
  303. {
  304. labelStyleDropdownOptions = new GUIStyle("ToolbarButton")
  305. {
  306. alignment = TextAnchor.MiddleLeft,
  307. padding = new RectOffset(20, 0, 0, 0)
  308. };
  309. }
  310. if (titleStyle == null)
  311. {
  312. titleStyle = new GUIStyle("Label") { alignment = TextAnchor.MiddleCenter, fontSize = 20 };
  313. }
  314. }
  315. private void ResetToggle()
  316. {
  317. if (Event.current.rawType == EventType.MouseUp)
  318. {
  319. if (toggleOnScript)
  320. {
  321. splitDropdownScriptGraph.Reset();
  322. Repaint();
  323. }
  324. if (toggleOnState)
  325. {
  326. splitDropdownStateGraph.Reset();
  327. Repaint();
  328. }
  329. }
  330. }
  331. private void OpenGraphFromPicker()
  332. {
  333. if (Event.current.commandName == "ObjectSelectorUpdated")
  334. {
  335. UnityObject selectedObject = EditorGUIUtility.GetObjectPickerObject();
  336. OpenGraphAsset(selectedObject, false);
  337. }
  338. }
  339. private void OnGUI()
  340. {
  341. CreateStyles();
  342. ResetToggle();
  343. DropAreaGUI();
  344. OpenGraphFromPicker();
  345. scrollPosition = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height), scrollPosition,
  346. scrollArea);
  347. GUILayout.BeginVertical(GUILayout.ExpandHeight(true));
  348. Vector2 groupSize = new Vector2(scrollArea.width, 280);
  349. GUI.BeginGroup(new Rect((position.width / 2) - (groupSize.x / 2), (position.height / 2) - (groupSize.y / 2),
  350. groupSize.x, groupSize.y));
  351. GUI.Label(new Rect(0, 0, groupSize.x, titleHeight), "Drag and drop a Visual Scripting Graph asset here\nor",
  352. titleStyle);
  353. int buttonX = 10;
  354. if (GUI.Button(new Rect(buttonX, titleHeight, buttonWidth, buttonHeight), "Browse to open a Graph"))
  355. {
  356. OpenGraph();
  357. }
  358. buttonX += (buttonWidth + 10);
  359. Rect area = new Rect(buttonX, titleHeight, buttonWidth, buttonHeight);
  360. splitDropdownScriptGraph.GetOption(0).interactable = Selection.activeGameObject != null;
  361. if (splitDropdownScriptGraph.Draw(area, "Create new Script Graph", ref toggleOnScript))
  362. {
  363. CreateScriptGraph();
  364. }
  365. if (toggleOnScript)
  366. {
  367. toggleOnState = false;
  368. }
  369. buttonX += (buttonWidth + 10);
  370. area = new Rect(buttonX, titleHeight, buttonWidth, buttonHeight);
  371. splitDropdownStateGraph.GetOption(0).interactable = Selection.activeGameObject != null;
  372. if (splitDropdownStateGraph.Draw(area, "Create new State Graph", ref toggleOnState))
  373. {
  374. CreateStateGraph();
  375. }
  376. if (toggleOnState)
  377. {
  378. toggleOnScript = false;
  379. }
  380. GUI.EndGroup();
  381. GUILayout.EndVertical();
  382. GUI.EndScrollView();
  383. if (shouldCloseWindow)
  384. {
  385. Close();
  386. }
  387. }
  388. private void DropAreaGUI()
  389. {
  390. Event currentEvent = Event.current;
  391. Rect activeArea = GUILayoutUtility.GetRect(0, 0, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
  392. switch (currentEvent.type)
  393. {
  394. case EventType.DragUpdated:
  395. case EventType.DragPerform:
  396. if (!activeArea.Contains(currentEvent.mousePosition))
  397. {
  398. return;
  399. }
  400. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  401. if (currentEvent.type == EventType.DragPerform)
  402. {
  403. DragAndDrop.AcceptDrag();
  404. foreach (UnityObject draggedObject in DragAndDrop.objectReferences)
  405. {
  406. ScriptGraphAsset scriptGraphAsset = draggedObject as ScriptGraphAsset;
  407. if (scriptGraphAsset != null)
  408. {
  409. shouldCloseWindow = true;
  410. GraphWindow.OpenActive(GraphReference.New(scriptGraphAsset, true));
  411. break;
  412. }
  413. }
  414. }
  415. break;
  416. }
  417. }
  418. }
  419. }