/// <summary> /// Saves the current project or module to the assets directory /// /// NOTE: If the project has never been saved before a `Save As` is called instead /// </summary> public static bool Save() { var persistentObject = EditorContext.PersistentObject; // No `PersistenceId` means this object has never been saved, promt the user with the `Save As` instead if (string.IsNullOrEmpty(persistentObject.PersistenceId)) { return(SaveAs()); } // Use a DontSaveScope since saving may trigger the `OnWillSaveAssets` callback using (new UTinyModificationProcessor.DontSaveScope()) { var path = UTinyPersistence.PersistObject(persistentObject); if (string.IsNullOrEmpty(path)) { return(false); } } UTinyEditorPrefs.SaveWorkspace(EditorContext.Workspace, persistentObject.PersistenceId); OnSaveProject?.Invoke(EditorContext.Project); UTinyModificationProcessor.ClearChanged(); return(true); }
public static bool SaveAs() { var persistentObject = EditorContext.PersistentObject; var extension = UTinyPersistence.GetFileExtension(persistentObject); var path = EditorUtility.SaveFilePanelInProject($"Save {ContextType}", persistentObject.Name, extension.Substring(1), string.Empty); if (string.IsNullOrEmpty(path)) { return(false); } // Use a DontSaveScope since saving may trigger the `OnWillSaveAssets` callback using (new UTinyModificationProcessor.DontSaveScope()) { // Fix-up the name persistentObject.Name = Path.GetFileNameWithoutExtension(path); // Flush the caretaker so this operation is not undoable EditorContext.Caretaker.Update(); path = UTinyPersistence.PersistObjectAs(persistentObject, path); if (string.IsNullOrEmpty(path)) { return(false); } } UTinyEditorPrefs.SaveWorkspace(EditorContext.Workspace, persistentObject.PersistenceId); OnSaveProject?.Invoke(EditorContext.Project); UTinyModificationProcessor.ClearChanged(); return(true); }
/// <summary> /// Closes the current Tiny project /// </summary> public static void Close() { if (null == EditorContext) { return; } OnCloseProject?.Invoke(EditorContext.Project); EditorContext?.Unload(); EditorContext = null; UTinyTemp.Delete(); UTinyModificationProcessor.ClearChanged(); }
private static void LoadContext(UTinyEditorContext context, bool isChanged) { Assert.IsNotNull(context); UTinyModificationProcessor.ClearChanged(); // @NOTE Loading a project can cause a Unity scene to change or be loaded during this operation we dont want to trigger a save using (new UTinyModificationProcessor.DontSaveScope()) { // Cleanup the previous context if (context != EditorContext) { EditorContext?.Unload(); } // Load the new context EditorContext = context; EditorContext.Load(); // Setup the initial state s_WorkspaceVersion = EditorContext.Workspace.Version; OnLoadProject?.Invoke(EditorContext.Project); // Flush the Undo stack EditorContext.Undo.Update(); // Listen for ANY changes and flag the project as changed (*) EditorContext.Caretaker.OnObjectChanged += (originator, memento) => { UTinyModificationProcessor.MarkChanged(); }; EditorContext.Undo.OnUndoPerformed += UTinyModificationProcessor.MarkChanged; EditorContext.Undo.OnRedoPerformed += UTinyModificationProcessor.MarkChanged; } if (isChanged) { UTinyModificationProcessor.MarkChanged(); } }
private static void Update() { if (EditorApplication.isPlayingOrWillChangePlaymode) { return; } if (null == EditorContext) { // Flush asset changes from the persistence system // We don't care about any changes unless we have a project loaded UTinyPersistence.ClearChanges(); return; } // Poll for workspace changes if (null != EditorContext.Workspace && s_WorkspaceVersion != EditorContext.Workspace.Version) { UTinyEditorPrefs.SaveWorkspace(EditorContext.Workspace, EditorContext.PersistentObject.PersistenceId); s_WorkspaceVersion = EditorContext.Workspace.Version; } // Poll for file/asset changes var changes = UTinyPersistence.DetectChanges(Registry); if (changes.changesDetected) { var persistenceId = EditorContext.PersistentObject.PersistenceId; foreach (var change in changes.changedSources) { // The currently opened project or module has been changed on disc if (change.Equals(persistenceId)) { // Ask the user if they want to keep their changes or reload from disc if (EditorUtility.DisplayDialog($"{UTinyConstants.ApplicationName} assets changed", $"{UTinyConstants.ApplicationName} assets have changed on disk, would you like to reload the current project?", "Yes", "No")) { LoadPersistenceId(persistenceId); } else { UTinyModificationProcessor.MarkChanged(); } } else { // This is some other file. We assume they are in a readonly state and we silently reload the object UTinyPersistence.ReloadObject(EditorContext.Registry, change); } } foreach (var deletion in changes.deletedSources) { // The currently opened project or module has been deleted on disc if (deletion.Equals(persistenceId)) { // Ask the user if they want to keep their changes or close the project if (EditorUtility.DisplayDialog($"{UTinyConstants.ApplicationName} assets changed", "The current project has been deleted, would you like to close the current project?", "Yes", "No")) { // Force close the project Close(); } else { UTinyModificationProcessor.MarkChanged(); EditorContext.PersistentObject.PersistenceId = string.Empty; } } else { // This is some other file. We assume they are in a readonly state and we silently reload the object EditorContext.Registry.UnregisterAllBySource(deletion); } } foreach (var moved in changes.movedSources) { if (!moved.Equals(persistenceId)) { continue; } var path = AssetDatabase.GUIDToAssetPath(moved); var asset = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object)); if (null != asset) { EditorContext.PersistentObject.Name = asset.name; } } OnChangesDetected?.Invoke(); } // Poll for module or project changes if (EditorContext.ContextType == EditorContextType.Project && (s_ProjectVersion != EditorContext.Project.Version || s_ModuleVersion != EditorContext.Module.Version)) { EditorContext.Project.RefreshConfiguration(); s_ProjectVersion = EditorContext.Project.Version; s_ModuleVersion = EditorContext.Module.Version; } if (s_Save) { s_Save = false; // NOTE: It is possible that this call will fail Save(); } }