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