/// <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); }
/// <summary> /// Save the runtime graph to a prefab /// </summary> /// <param name="runtimeGraph"></param> /// <param name="graphAsset"></param> public static void SaveRuntimeGraph(uNodeRuntime runtimeGraph) { if (!Application.isPlaying) { throw new System.Exception("Saving runtime graph can only be done in playmode"); } if (runtimeGraph.originalGraph == null) { throw new System.Exception("Cannot save runtime graph because the original graph was null / missing"); } var graph = runtimeGraph.originalGraph; if (!EditorUtility.IsPersistent(graph)) { throw new System.Exception("Cannot save graph to unpersistent asset"); } var prefabContent = PrefabUtility.LoadPrefabContents(AssetDatabase.GetAssetPath(graph)); var originalGraph = uNodeHelper.GetGraphComponent(prefabContent, graph.GraphName); if (originalGraph != null) { if (runtimeGraph.RootObject != null) { //Duplicate graph data var tempRoot = Object.Instantiate(runtimeGraph.RootObject); tempRoot.name = "Root"; //Move graph data to original graph tempRoot.transform.SetParent(originalGraph.transform); //Retarget graph data owner AnalizerUtility.RetargetNodeOwner(runtimeGraph, originalGraph, tempRoot.GetComponentsInChildren <MonoBehaviour>(true)); if (originalGraph.RootObject != null) { //Destroy old graph data Object.DestroyImmediate(originalGraph.RootObject); } //Update graph data to new originalGraph.RootObject = tempRoot; //Save the graph to prefab uNodeEditorUtility.SavePrefabAsset(prefabContent, graph); //GraphUtility.DestroyTempGraphObject(originalGraph.gameObject); //This will update the original graph GraphUtility.DestroyTempGraphObject(graph.gameObject); //Refresh uNode Editor window uNodeEditor.window?.Refresh(); } } else { Debug.LogError("Cannot save instanced graph because the cannot find original graph with id:" + graph.GraphName); } PrefabUtility.UnloadPrefabContents(prefabContent); }