using System.IO; using System.Collections.Generic; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine.SceneManagement; using Codice.Client.BaseCommands; using Codice.Client.Common; namespace Unity.PlasticSCM.Editor.AssetUtils { internal static class SaveAssets { internal static void ForChangesWithConfirmation( List changes, out bool isCancelled) { ForPaths( GetPaths(changes), true, out isCancelled); } internal static void ForPathsWithConfirmation( List paths, out bool isCancelled) { ForPaths( paths, true, out isCancelled); } internal static void ForChangesWithoutConfirmation( List changes) { bool isCancelled; ForPaths( GetPaths(changes), false, out isCancelled); } internal static void ForPathsWithoutConfirmation( List paths) { bool isCancelled; ForPaths( paths, false, out isCancelled); } static void ForPaths( List paths, bool askForUserConfirmation, out bool isCancelled) { SaveDirtyScenes( paths, askForUserConfirmation, out isCancelled); if (isCancelled) return; AssetDatabase.SaveAssets(); } static void SaveDirtyScenes( List paths, bool askForUserConfirmation, out bool isCancelled) { isCancelled = false; List scenesToSave = new List(); foreach (Scene dirtyScene in GetDirtyScenes()) { if (Contains(paths, dirtyScene)) scenesToSave.Add(dirtyScene); } if (scenesToSave.Count == 0) return; if (askForUserConfirmation) { isCancelled = !EditorSceneManager. SaveModifiedScenesIfUserWantsTo( scenesToSave.ToArray()); return; } EditorSceneManager.SaveScenes( scenesToSave.ToArray()); } static List GetDirtyScenes() { List dirtyScenes = new List(); for (int i = 0; i < SceneManager.sceneCount; i++) { Scene scene = SceneManager.GetSceneAt(i); if (!scene.isDirty) continue; dirtyScenes.Add(scene); } return dirtyScenes; } static bool Contains( List paths, Scene scene) { foreach (string path in paths) { if (PathHelper.IsSamePath( path, Path.GetFullPath(scene.path))) return true; } return false; } static List GetPaths(List changeInfos) { List result = new List(); foreach (ChangeInfo change in changeInfos) result.Add(change.GetFullPath()); return result; } } }