/// <summary> /// Save the data under the specified filename. /// </summary> static public void Save(DataNode data, string path, DataNode.SaveType type) { if (data == null || string.IsNullOrEmpty(path)) { return; } data.Write(path, type); AssetDatabase.Refresh(ImportAssetOptions.Default); TextAsset asset = AssetDatabase.LoadAssetAtPath(FileUtil.GetProjectRelativePath(path), typeof(TextAsset)) as TextAsset; if (asset != null) { // Saved in the project folder -- select the saved asset Selection.activeObject = asset; if (asset != null) { Debug.Log("Saved as " + path + " (" + asset.bytes.Length.ToString("N0") + " bytes)", asset); } } else { // Saved outside of the project folder -- simply print its size System.IO.FileStream fs = System.IO.File.OpenRead(path); if (fs != null) { long pos = fs.Seek(0, System.IO.SeekOrigin.End); Debug.Log("Saved as " + path + " (" + pos.ToString("N0") + " bytes)"); fs.Close(); } } }
/// <summary> /// Asset Bundle-like export, except using DataNode's deep serialization. /// </summary> static internal void ExportAssets(DataNode.SaveType type) { var path = UnityEditorExtensions.ShowExportDialog("Export to DataNode", "Assets"); if (!string.IsNullOrEmpty(path)) { ExportAssets(type, path); } }
/// <summary> /// Save the data under the specified filename. /// </summary> static public void Save(DataNode data, string path, DataNode.SaveType type) { if (data == null || string.IsNullOrEmpty(path)) { return; } EditorUtility.DisplayCancelableProgressBar("Working", "Saving...", 1f); data.Write(path, type); EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(ImportAssetOptions.Default); var asset = AssetDatabase.LoadAssetAtPath(FileUtil.GetProjectRelativePath(path), typeof(TextAsset)) as TextAsset; if (asset != null) { // Saved in the project folder -- select the saved asset Selection.activeObject = asset; } else if (System.IO.File.Exists(path)) { System.Diagnostics.Process.Start("explorer.exe", "/select," + path.Replace('/', '\\')); } }
/// <summary> /// Asset Bundle-like export, except using DataNode's deep serialization. /// </summary> static public void ExportAssets(DataNode.SaveType type, string path) { EditorUtility.DisplayCancelableProgressBar("Working", "Collecting references...", 0f); ComponentSerialization.ClearReferences(); var objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); var components = new System.Collections.Generic.HashSet <string>(); foreach (var obj in objects) { if (obj is MonoScript) { var s = obj.name; if (!components.Contains(s)) { components.Add(s); } continue; } var go = obj as GameObject; if (go) { go.CollectReferencedPrefabs(true); var comps = go.GetComponentsInChildren <MonoBehaviour>(true); foreach (var comp in comps) { var t = comp.GetType().ToString(); if (!components.Contains(t)) { components.Add(t); } } } else { ComponentSerialization.AddReference(obj); } } EditorUtility.DisplayCancelableProgressBar("Working", "Copying scripts...", 0f); var dir = Tools.GetDirectoryFromPath(path); // Copy the scripts foreach (var c in components) { var fn = c + ".cs"; var p = Tools.FindFile(Application.dataPath, fn); if (!string.IsNullOrEmpty(p)) { System.IO.File.Copy(p, System.IO.Path.Combine(dir, fn), true); } } EditorUtility.DisplayCancelableProgressBar("Working", "Creating a DataNode...", 0f); foreach (var pair in ComponentSerialization.referencedPrefabs) { pair.Value.CollectReferencedResources(); } var data = ComponentSerialization.SerializeBundle(); if (data != null && data.children.size > 0) { Save(data, path, type); } else { Debug.LogWarning("No assets found to serialize"); } ComponentSerialization.ClearReferences(); EditorUtility.ClearProgressBar(); }