示例#1
0
        /// <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);
        }