/// <summary> /// Get the graph singleton instance /// </summary> /// <param name="graphSingleton"></param> /// <returns></returns> internal static uNodeRuntime GetRuntimeGraph(uNodeComponentSingleton graphSingleton) { uNodeRuntime instance; if (!graphSingletons.TryGetValue(graphSingleton, out instance) || instance == null) { var objs = GameObject.FindObjectsOfType <uNodeRuntime>(); instance = objs.FirstOrDefault(g => g.GraphName == graphSingleton.GraphName); if (instance == null) { GameObject mainObject = new GameObject($"[Singleton:{graphSingleton.GraphName}]"); if (graphSingleton.IsPersistence) { GameObject.DontDestroyOnLoad(mainObject); } uNodeRoot graph = UnityEngine.Object.Instantiate(graphSingleton); uNodeRuntime main = mainObject.AddComponent <uNodeRuntime>(); main.originalGraph = graphSingleton; main.Name = graphSingleton.GraphName; //This will ensure the uid is valid main.Variables = graph.Variables; main.RootObject = graph.RootObject; main.RootObject.transform.SetParent(mainObject.transform); AnalizerUtility.RetargetNodeOwner(graph, main, main.RootObject.GetComponentsInChildren <MonoBehaviour>(true)); main.Refresh(); UnityEngine.Object.Destroy(graph.gameObject); instance = main; graphSingletons[instance] = instance; } } return(instance); }
internal static RuntimeBehaviour GetNativeGraph(uNodeComponentSingleton graphSingleton) { RuntimeBehaviour instance; if (!nativeGraphSingletons.TryGetValue(graphSingleton, out instance) || instance == null) { var type = graphSingleton.GeneratedTypeName.ToType(false); if (type == null) { return(null); } var objs = GameObject.FindObjectsOfType(type); instance = objs.FirstOrDefault() as RuntimeBehaviour; if (instance == null) { GameObject mainObject = new GameObject($"[Singleton:{graphSingleton.GraphName}]"); if (graphSingleton.IsPersistence) { GameObject.DontDestroyOnLoad(mainObject); } instance = mainObject.AddComponent(type) as RuntimeBehaviour; //Initialize the references var references = graphSingleton.graphData.unityObjects; for (int i = 0; i < references.Count; i++) { instance.SetVariable(references[i].name, references[i].value); } //Initialize the variable for (int i = 0; i < graphSingleton.Variables.Count; i++) { VariableData var = graphSingleton.Variables[i]; instance.SetVariable(var.Name, var.value); } } nativeGraphSingletons[graphSingleton] = instance; } return(instance); }