No Description
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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace Unity.PlasticSCM.Editor.AssetUtils
  9. {
  10. internal static class SaveAssets
  11. {
  12. internal static void ForChangesWithConfirmation(
  13. List<ChangeInfo> changes,
  14. out bool isCancelled)
  15. {
  16. ForPaths(
  17. GetPaths(changes), true,
  18. out isCancelled);
  19. }
  20. internal static void ForPathsWithConfirmation(
  21. List<string> paths,
  22. out bool isCancelled)
  23. {
  24. ForPaths(
  25. paths, true,
  26. out isCancelled);
  27. }
  28. internal static void ForChangesWithoutConfirmation(
  29. List<ChangeInfo> changes)
  30. {
  31. bool isCancelled;
  32. ForPaths(
  33. GetPaths(changes), false,
  34. out isCancelled);
  35. }
  36. internal static void ForPathsWithoutConfirmation(
  37. List<string> paths)
  38. {
  39. bool isCancelled;
  40. ForPaths(
  41. paths, false,
  42. out isCancelled);
  43. }
  44. static void ForPaths(
  45. List<string> paths,
  46. bool askForUserConfirmation,
  47. out bool isCancelled)
  48. {
  49. SaveDirtyScenes(
  50. paths,
  51. askForUserConfirmation,
  52. out isCancelled);
  53. if (isCancelled)
  54. return;
  55. AssetDatabase.SaveAssets();
  56. }
  57. static void SaveDirtyScenes(
  58. List<string> paths,
  59. bool askForUserConfirmation,
  60. out bool isCancelled)
  61. {
  62. isCancelled = false;
  63. List<Scene> scenesToSave = new List<Scene>();
  64. foreach (Scene dirtyScene in GetDirtyScenes())
  65. {
  66. if (Contains(paths, dirtyScene))
  67. scenesToSave.Add(dirtyScene);
  68. }
  69. if (scenesToSave.Count == 0)
  70. return;
  71. if (askForUserConfirmation)
  72. {
  73. isCancelled = !EditorSceneManager.
  74. SaveModifiedScenesIfUserWantsTo(
  75. scenesToSave.ToArray());
  76. return;
  77. }
  78. EditorSceneManager.SaveScenes(
  79. scenesToSave.ToArray());
  80. }
  81. static List<Scene> GetDirtyScenes()
  82. {
  83. List<Scene> dirtyScenes = new List<Scene>();
  84. for (int i = 0; i < SceneManager.sceneCount; i++)
  85. {
  86. Scene scene = SceneManager.GetSceneAt(i);
  87. if (!scene.isDirty)
  88. continue;
  89. dirtyScenes.Add(scene);
  90. }
  91. return dirtyScenes;
  92. }
  93. static bool Contains(
  94. List<string> paths,
  95. Scene scene)
  96. {
  97. foreach (string path in paths)
  98. {
  99. if (PathHelper.IsSamePath(
  100. path,
  101. Path.GetFullPath(scene.path)))
  102. return true;
  103. }
  104. return false;
  105. }
  106. static List<string> GetPaths(List<ChangeInfo> changeInfos)
  107. {
  108. List<string> result = new List<string>();
  109. foreach (ChangeInfo change in changeInfos)
  110. result.Add(change.GetFullPath());
  111. return result;
  112. }
  113. }
  114. }