暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SaveAssets.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.IO;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEditor.SceneManagement;
  5. using UnityEngine.SceneManagement;
  6. using Codice.Client.BaseCommands;
  7. using Codice.Client.Common;
  8. using Unity.PlasticSCM.Editor.AssetUtils.Processor;
  9. namespace Unity.PlasticSCM.Editor.AssetUtils
  10. {
  11. internal static class SaveAssets
  12. {
  13. internal static void ForChangesWithConfirmation(
  14. List<ChangeInfo> changes,
  15. WorkspaceOperationsMonitor workspaceOperationsMonitor,
  16. out bool isCancelled)
  17. {
  18. ForPaths(
  19. GetPaths(changes), true,
  20. workspaceOperationsMonitor,
  21. out isCancelled);
  22. }
  23. internal static void ForPathsWithConfirmation(
  24. List<string> paths,
  25. WorkspaceOperationsMonitor workspaceOperationsMonitor,
  26. out bool isCancelled)
  27. {
  28. ForPaths(
  29. paths, true,
  30. workspaceOperationsMonitor,
  31. out isCancelled);
  32. }
  33. internal static void ForChangesWithoutConfirmation(
  34. List<ChangeInfo> changes,
  35. WorkspaceOperationsMonitor workspaceOperationsMonitor)
  36. {
  37. bool isCancelled;
  38. ForPaths(
  39. GetPaths(changes), false,
  40. workspaceOperationsMonitor,
  41. out isCancelled);
  42. }
  43. internal static void ForPathsWithoutConfirmation(
  44. List<string> paths,
  45. WorkspaceOperationsMonitor workspaceOperationsMonitor)
  46. {
  47. bool isCancelled;
  48. ForPaths(
  49. paths, false,
  50. workspaceOperationsMonitor,
  51. out isCancelled);
  52. }
  53. static void ForPaths(
  54. List<string> paths,
  55. bool askForUserConfirmation,
  56. WorkspaceOperationsMonitor workspaceOperationsMonitor,
  57. out bool isCancelled)
  58. {
  59. workspaceOperationsMonitor.Disable();
  60. try
  61. {
  62. SaveDirtyScenes(
  63. paths,
  64. askForUserConfirmation,
  65. out isCancelled);
  66. if (isCancelled)
  67. return;
  68. AssetDatabase.SaveAssets();
  69. }
  70. finally
  71. {
  72. workspaceOperationsMonitor.Enable();
  73. }
  74. }
  75. static void SaveDirtyScenes(
  76. List<string> paths,
  77. bool askForUserConfirmation,
  78. out bool isCancelled)
  79. {
  80. isCancelled = false;
  81. List<Scene> scenesToSave = new List<Scene>();
  82. foreach (Scene dirtyScene in GetDirtyScenes())
  83. {
  84. if (Contains(paths, dirtyScene))
  85. scenesToSave.Add(dirtyScene);
  86. }
  87. if (scenesToSave.Count == 0)
  88. return;
  89. if (askForUserConfirmation)
  90. {
  91. isCancelled = !EditorSceneManager.
  92. SaveModifiedScenesIfUserWantsTo(
  93. scenesToSave.ToArray());
  94. return;
  95. }
  96. EditorSceneManager.SaveScenes(
  97. scenesToSave.ToArray());
  98. }
  99. static List<Scene> GetDirtyScenes()
  100. {
  101. List<Scene> dirtyScenes = new List<Scene>();
  102. for (int i = 0; i < SceneManager.sceneCount; i++)
  103. {
  104. Scene scene = SceneManager.GetSceneAt(i);
  105. if (!scene.isDirty)
  106. continue;
  107. dirtyScenes.Add(scene);
  108. }
  109. return dirtyScenes;
  110. }
  111. static bool Contains(
  112. List<string> paths,
  113. Scene scene)
  114. {
  115. if (string.IsNullOrEmpty(scene.path))
  116. return false;
  117. foreach (string path in paths)
  118. {
  119. if (PathHelper.IsSamePath(
  120. path,
  121. Path.GetFullPath(scene.path)))
  122. return true;
  123. }
  124. return false;
  125. }
  126. static List<string> GetPaths(
  127. List<ChangeInfo> changeInfos)
  128. {
  129. List<string> result = new List<string>();
  130. foreach (ChangeInfo change in changeInfos)
  131. result.Add(change.GetFullPath());
  132. return result;
  133. }
  134. }
  135. }