Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AssetDatabaseUtils.cs 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.IO;
  4. using UnityEditor;
  5. namespace UnityEngine.InputSystem
  6. {
  7. /// Provides convenience functions for creating and managing assets for test purposes.
  8. /// Note that all returned paths are converted to Unix paths when running on Windows
  9. /// for consistency and to avoid mixed path names.
  10. public static class AssetDatabaseUtils
  11. {
  12. private const string kAssetPath = "Assets";
  13. private const string kTestPath = "TestFiles";
  14. private const string kMetaExtension = ".meta";
  15. private const string kDefaultAssetExtension = "asset";
  16. // Perform an operation equivalent to a file delete operation outside of Unity Editor.
  17. // Note that meta file is also removed to avoid generating warnings about non-clean delete.
  18. public static void ExternalDeleteFileOrDirectory(string path)
  19. {
  20. FileUtil.DeleteFileOrDirectory(path);
  21. FileUtil.DeleteFileOrDirectory(path + kMetaExtension);
  22. }
  23. // Perform an operation equivalent to a file move operation outside of Unity Editor.
  24. // Note that meta file is also moved to avoid generating warnings about non-clean move.
  25. public static void ExternalMoveFileOrDirectory(string source, string dest)
  26. {
  27. FileUtil.MoveFileOrDirectory(source, dest);
  28. FileUtil.MoveFileOrDirectory(source + kMetaExtension, dest + kMetaExtension);
  29. }
  30. // Create an asset at the given path containing the given text content.
  31. private static T CreateAssetAtPath<T>(string path, string content) where T : UnityEngine.Object
  32. {
  33. Debug.Assert(!File.Exists(path));
  34. T obj;
  35. try
  36. {
  37. CreateDirectories(Path.GetDirectoryName(path));
  38. File.WriteAllText(path, content);
  39. AssetDatabase.ImportAsset(path);
  40. obj = AssetDatabase.LoadAssetAtPath<T>(path);
  41. if (obj == null)
  42. throw new Exception($"Failed to create asset at \"{path}\"");
  43. }
  44. catch (Exception)
  45. {
  46. AssetDatabase.DeleteAsset(path);
  47. throw;
  48. }
  49. return obj;
  50. }
  51. private static string SanitizePath(string path)
  52. {
  53. return path?.Replace("\\", "/");
  54. }
  55. private static void CreateRootDirectory()
  56. {
  57. CreateDirectories(RootPath());
  58. }
  59. // Creates all directories (including intermediate) defined in path.
  60. private static string CreateDirectories(string path)
  61. {
  62. if (Directory.Exists(path))
  63. return SanitizePath(path);
  64. var parentFolder = kAssetPath;
  65. path = path.Replace("\\", "/"); // Make sure we only get '/' separators.
  66. var directories = path.Split('/');
  67. if (directories[0] != kAssetPath)
  68. throw new ArgumentException(path);
  69. for (var i = 1; i < directories.Length; ++i)
  70. {
  71. var guid = AssetDatabase.CreateFolder(parentFolder, directories[i]);
  72. if (guid == string.Empty)
  73. throw new Exception("Failed to create path \"" + path + "\"");
  74. parentFolder = SanitizePath(Path.Combine(parentFolder, directories[i]));
  75. }
  76. AssetDatabase.Refresh();
  77. return SanitizePath(path);
  78. }
  79. // Creates a random test directory within asset folder that is automatically removed after test run.
  80. public static string CreateDirectory()
  81. {
  82. return CreateDirectories(RandomDirectoryPath());
  83. }
  84. // Creates an asset in the given directory path with an explicit or random file name containing the
  85. // given content or the default content based on type.
  86. public static T CreateAsset<T>(string directoryPath, string filename = null, string content = null) where T : UnityEngine.Object
  87. {
  88. Debug.Assert(directoryPath == null || directoryPath.Contains(RootPath()));
  89. Debug.Assert(filename == null || !filename.Contains("/"));
  90. if (directoryPath == null)
  91. directoryPath = RootPath();
  92. string path;
  93. if (filename != null)
  94. {
  95. path = SanitizePath(Path.Combine(directoryPath, filename));
  96. if (File.Exists(path))
  97. throw new Exception($"File already exists: {path}");
  98. }
  99. else
  100. {
  101. path = RandomAssetFilePath(directoryPath, AssetFileExtensionFromType(typeof(T)));
  102. }
  103. return CreateAsset<T>(path: path, content: content);
  104. }
  105. // Creates an asset at the given path containing the specified content.
  106. // If path is null, a unique random file name is assigned, if content is null the default content based
  107. // on type (extension) is used.
  108. public static T CreateAsset<T>(string path = null, string content = null) where T : UnityEngine.Object
  109. {
  110. if (path == null)
  111. path = RandomAssetFilePath(RootPath(), AssetFileExtensionFromType(typeof(T)));
  112. if (content == null)
  113. content = DefaultContentFromType(typeof(T));
  114. return CreateAssetAtPath<T>(path, content);
  115. }
  116. public static void Restore()
  117. {
  118. var root = RootPath();
  119. // Delete all files in test folder
  120. if (!Directory.Exists(root))
  121. return;
  122. foreach (var asset in AssetDatabase.FindAssets("", new string[] { root }))
  123. {
  124. var path = AssetDatabase.GUIDToAssetPath(asset);
  125. AssetDatabase.DeleteAsset(path);
  126. }
  127. AssetDatabase.DeleteAsset(root);
  128. }
  129. private static string RandomName()
  130. {
  131. const double scale = int.MaxValue;
  132. double r = UnityEngine.Random.value;
  133. return "Test_" + (int)(Math.Floor(r * scale));
  134. }
  135. private static string RandomAssetFilePath<T>(string directoryPath = null)
  136. {
  137. return RandomAssetFilePath(directoryPath, AssetFileExtensionFromType(typeof(T)));
  138. }
  139. private static string RandomAssetFilePath(string directoryPath = null, string extension = null)
  140. {
  141. // Default to using test files root path
  142. if (directoryPath == null)
  143. directoryPath = RootPath();
  144. // Default to default extension
  145. if (extension == null)
  146. extension = kDefaultAssetExtension;
  147. string path;
  148. do
  149. {
  150. path = SanitizePath(Path.Combine(directoryPath, RandomName() + "." + extension)); // EDIT
  151. }
  152. while (File.Exists(path));
  153. return path;
  154. }
  155. private static string RootPath()
  156. {
  157. return SanitizePath(Path.Combine(kAssetPath, kTestPath));
  158. }
  159. public static string RandomDirectoryPath()
  160. {
  161. string path;
  162. do
  163. {
  164. path = Path.Combine(RootPath(), RandomName());
  165. }
  166. while (File.Exists(path));
  167. return SanitizePath(path);
  168. }
  169. private static string AssetFileExtensionFromType(Type type)
  170. {
  171. if (type == typeof(InputActionAsset))
  172. return InputActionAsset.Extension;
  173. return kDefaultAssetExtension;
  174. }
  175. private static string DefaultContentFromType(Type type)
  176. {
  177. if (type == typeof(InputActionAsset))
  178. return "{}";
  179. return string.Empty;
  180. }
  181. }
  182. }
  183. #endif // UNITY_EDITOR