/// <summary> /// Saves the the specified NodeCanvas as a new asset at path, optionally as a working copy and overwriting any existing save at path /// </summary> public static void SaveNodeCanvas(string path, ref NodeCanvas nodeCanvas, bool createWorkingCopy, bool safeOverwrite = true) { #if !UNITY_EDITOR throw new System.NotImplementedException(); #else nodeCanvas.CheckConnectivity(); if (string.IsNullOrEmpty(path)) { throw new System.ArgumentNullException("Cannot save NodeCanvas: No path specified!"); } if (nodeCanvas == null) { throw new System.ArgumentNullException("Cannot save NodeCanvas: The specified NodeCanvas that should be saved to path '" + path + "' is null!"); } if (nodeCanvas.GetType() == typeof(NodeCanvas)) { throw new System.ArgumentException("Cannot save NodeCanvas: The NodeCanvas has no explicit type! Please convert it to a valid sub-type of NodeCanvas!"); } if (nodeCanvas.allowSceneSaveOnly) { throw new System.InvalidOperationException("Cannot save NodeCanvas: NodeCanvas is marked to contain scene data and cannot be saved as an asset!"); } nodeCanvas.Validate(); if (nodeCanvas.livesInScene) { Debug.LogWarning("Attempting to save scene canvas '" + nodeCanvas.name + "' to an asset, references to scene object may be broken!" + (!createWorkingCopy? " Forcing creation of working copy!" : "")); createWorkingCopy = true; } if (!createWorkingCopy && UnityEditor.AssetDatabase.Contains(nodeCanvas) && UnityEditor.AssetDatabase.GetAssetPath(nodeCanvas) != path) { Debug.LogWarning("Trying to create a duplicate save file for '" + nodeCanvas.name + "'! Forcing creation of working copy!"); nodeCanvas = CreateWorkingCopy(nodeCanvas); } // Prepare and update source path of the canvas path = ResourceManager.PreparePath(path); nodeCanvas.UpdateSource(path); // Preprocess the canvas NodeCanvas processedCanvas = nodeCanvas; processedCanvas.OnBeforeSavingCanvas(); if (createWorkingCopy) { processedCanvas = CreateWorkingCopy(processedCanvas); } // Differenciate canvasSave as the canvas asset and nodeCanvas as the source incase an existing save has been overwritten NodeCanvas canvasSave = processedCanvas; NodeCanvas prevSave = ResourceManager.LoadResource <NodeCanvas>(path); if (canvasSave == prevSave) { Debug.LogWarning("Overwriting same canvas impossible, please use another path!"); return; } if (safeOverwrite && prevSave != null && prevSave.GetType() == canvasSave.GetType()) { // OVERWRITE: Delete contents of old save Object[] subAssets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(path); for (int i = 0; i < subAssets.Length; i++) { // Delete all subassets except the main canvas to preserve references if (subAssets[i] != prevSave) { Object.DestroyImmediate(subAssets[i], true); } } // Overwrite main canvas OverwriteCanvas(ref prevSave, processedCanvas); canvasSave = prevSave; } else { // Write main canvas UnityEditor.AssetDatabase.DeleteAsset(path); UnityEditor.AssetDatabase.CreateAsset(processedCanvas, path); } // Write editorStates AddSubAssets(processedCanvas.editorStates, canvasSave); // Write nodes + contents foreach (Node node in processedCanvas.nodes) { // Write node and additional scriptable objects AddSubAsset(node, canvasSave); AddSubAssets(node.GetScriptableObjects(), node); // Add all ports ConnectionPortManager.UpdatePortLists(node, false); foreach (ConnectionPort port in node.staticConnectionPorts) { AddSubAsset(port, node); } foreach (ConnectionPort port in node.dynamicConnectionPorts) { AddSubAsset(port, node); } } UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); NodeEditorCallbacks.IssueOnSaveCanvas(canvasSave); #endif }
/// <summary> /// Saves the nodeCanvas in the current scene under the specified name, optionally as a working copy and overwriting any existing save at path /// If the specified canvas is stored as an asset, the saved canvas will loose the reference to the asset /// </summary> public static void SaveSceneNodeCanvas(string saveName, ref NodeCanvas nodeCanvas, bool createWorkingCopy, bool safeOverwrite = true) { nodeCanvas.CheckConnectivity(); if (string.IsNullOrEmpty(saveName)) { throw new System.ArgumentNullException("Cannot save Canvas to scene: No save name specified!"); } if (nodeCanvas == null) { throw new System.ArgumentNullException("Cannot save NodeCanvas: The specified NodeCanvas that should be saved as '" + saveName + "' is null!"); } if (nodeCanvas.GetType() == typeof(NodeCanvas)) { throw new System.ArgumentException("Cannot save NodeCanvas: The NodeCanvas has no explicit type! Please convert it to a valid sub-type of NodeCanvas!"); } if (saveName.StartsWith("SCENE/")) { saveName = saveName.Substring(6); } nodeCanvas.Validate(); if (!nodeCanvas.livesInScene #if UNITY_EDITOR // Make sure the canvas has no reference to an asset || UnityEditor.AssetDatabase.Contains(nodeCanvas) #endif ) { Debug.LogWarning("Creating scene save '" + nodeCanvas.name + "' for canvas saved as an asset! Forcing creation of working copy!"); nodeCanvas = CreateWorkingCopy(nodeCanvas); } // Update the source of the canvas nodeCanvas.UpdateSource("SCENE/" + saveName); // Preprocess the canvas NodeCanvas processedCanvas = nodeCanvas; processedCanvas.OnBeforeSavingCanvas(); if (createWorkingCopy) { processedCanvas = CreateWorkingCopy(processedCanvas); } // Get the saveHolder and store the canvas NodeCanvas savedCanvas = processedCanvas; NodeCanvasSceneSave sceneSave = FindSceneSave(saveName, true); #if UNITY_EDITOR if (sceneSave.savedNodeCanvas != null && safeOverwrite && sceneSave.savedNodeCanvas.GetType() == savedCanvas.GetType()) // OVERWRITE { OverwriteCanvas(ref sceneSave.savedNodeCanvas, savedCanvas); } if (!Application.isPlaying) { // Set Dirty UnityEditor.EditorUtility.SetDirty(sceneSave.gameObject); #if UNITY_5_3_OR_NEWER || UNITY_5_3 UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(sceneSave.gameObject.scene); #else UnityEditor.EditorApplication.MarkSceneDirty(); #endif } #endif sceneSave.savedNodeCanvas = savedCanvas; NodeEditorCallbacks.IssueOnSaveCanvas(savedCanvas); }