public void Initialize() { if (!Application.isPlaying) { throw new System.Exception("Can't initialize when the aplication is not playing."); } if (target == null) { throw new System.Exception("Target graph can't be null"); } var type = target.GeneratedTypeName.ToType(false); if (type != null) { runtimeAsset = ScriptableObject.CreateInstance(type) as RuntimeAsset; for (int i = 0; i < target.Variables.Count; i++) { VariableData var = target.Variables[i]; for (int x = 0; x < variable.Count; x++) { if (var.Name.Equals(variable[x].Name)) { SetVariable(var.Name, variable[x].value); } } } } else { var mainObject = new GameObject(name); mainObject.SetActive(false); uNodeRoot graph = Instantiate(target); uNodeRuntime main = mainObject.AddComponent <uNodeRuntime>(); main.originalGraph = target; main.Name = target.GraphName; 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(); Destroy(graph.gameObject); for (int i = 0; i < main.Variables.Count; i++) { VariableData var = main.Variables[i]; for (int x = 0; x < variable.Count; x++) { if (var.Name.Equals(variable[x].Name)) { main.Variables[i] = new VariableData(variable[x]); } } } //Uncomment this to prevent resetting variable you're exiting play mode // this.variable = main.variable; this.runtimeInstance = main; GameObject.DontDestroyOnLoad(mainObject); } }
static RuntimeGraphData GetCachedGraph(uNodeRoot targetGraph) { RuntimeGraphData graphData; if (!runtimeGraphMap.TryGetValue(targetGraph, out graphData)) { uNodeRoot graph = Object.Instantiate(targetGraph); //Begin instantiate original graph graph.gameObject.name = targetGraph.gameObject.name; //Ensure the cached graph name is same with the original graph #if !UNODE_DEBUG graph.gameObject.hideFlags = HideFlags.HideInHierarchy; //Ensure the cached graph doesn't show in the hierarchy #endif Object.DontDestroyOnLoad(graph.gameObject); //Ensure this cached graph is cached across scenes graphData = new RuntimeGraphData() { graph = graph, originalGraph = targetGraph, retargetAction = AnalizerUtility.GetRetargetNodeOwnerAction(graph, graph.RootObject.GetComponentsInChildren <MonoBehaviour>(true)), }; runtimeGraphMap[targetGraph] = graphData; } return(graphData); }