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

RuntimeGraphBase.cs 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if VISUAL_SCRIPT_INTERNAL
  2. using System;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace Unity.VisualScripting
  7. {
  8. public abstract class RuntimeGraphBase<TMacro, TGraph, TCanvas, TMachine>
  9. where TMacro : LudiqScriptableObject, IMacro
  10. where TGraph : IGraph
  11. where TCanvas : ICanvas
  12. where TMachine : IMachine
  13. {
  14. protected const string k_AssetPath = "Assets/test.asset";
  15. protected TMacro m_Macro;
  16. protected TGraph m_Graph;
  17. protected TCanvas m_Canvas;
  18. protected TMachine m_Machine;
  19. protected GraphReference m_Reference;
  20. protected GameObject m_GameObject;
  21. }
  22. public abstract class RuntimeFlowGraph : RuntimeGraphBase<ScriptGraphAsset, FlowGraph, FlowCanvas, ScriptMachine>
  23. {
  24. protected void CreateGraph()
  25. {
  26. m_Macro = ScriptableObject.CreateInstance<ScriptGraphAsset>();
  27. AssetDatabase.CreateAsset(m_Macro, k_AssetPath);
  28. m_Graph = m_Macro.graph;
  29. m_Canvas = new FlowCanvas(m_Graph);
  30. m_Reference = GraphReference.New(m_Macro, false);
  31. m_GameObject = new GameObject();
  32. m_Machine = m_GameObject.AddComponent<ScriptMachine>();
  33. m_Machine.nest.macro = m_Macro;
  34. }
  35. protected void AddUnit(IUnit unit)
  36. {
  37. AddUnit(unit, Vector2.down);
  38. }
  39. protected void AddUnit(IUnit unit, Vector2 position)
  40. {
  41. Undo.IncrementCurrentGroup();
  42. LudiqEditorUtility.editedObject.BeginOverride(m_Reference.serializedObject);
  43. m_Canvas.AddUnit(unit, position);
  44. LudiqEditorUtility.editedObject.EndOverride();
  45. }
  46. protected void Connect(ControlOutput source, ControlInput destination)
  47. {
  48. Undo.IncrementCurrentGroup();
  49. LudiqEditorUtility.editedObject.BeginOverride(m_Reference.serializedObject);
  50. var widget = new ControlOutputWidget(m_Canvas, source);
  51. var connectMethodInfo = GetConnectionMethodInfo(widget.GetType());
  52. connectMethodInfo?.Invoke(widget, new object[] { widget.port, destination });
  53. LudiqEditorUtility.editedObject.EndOverride();
  54. }
  55. protected void Connect(ValueOutput source, ValueInput destination)
  56. {
  57. Undo.IncrementCurrentGroup();
  58. LudiqEditorUtility.editedObject.BeginOverride(m_Reference.serializedObject);
  59. var widget = new ValueOutputWidget(m_Canvas, source);
  60. var connectMethodInfo = GetConnectionMethodInfo(widget.GetType());
  61. connectMethodInfo?.Invoke(widget, new object[] { widget.port, destination });
  62. LudiqEditorUtility.editedObject.EndOverride();
  63. }
  64. static MethodInfo GetConnectionMethodInfo(Type type)
  65. {
  66. while (true)
  67. {
  68. if (!(type is null))
  69. {
  70. foreach (var mi in type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic))
  71. {
  72. if (mi.Name == "FinishConnection")
  73. return mi;
  74. }
  75. type = type.BaseType;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. #endif