설명 없음
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.

TilePaletteWhiteboxSamplesUtility.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using UnityEditor.PackageManager.Requests;
  6. using UnityEditor.PackageManager.UI;
  7. using UnityEngine;
  8. namespace UnityEditor.Tilemaps
  9. {
  10. internal static class TilePaletteWhiteboxSamplesUtility
  11. {
  12. private static readonly String whiteboxFilterText = "Whitebox";
  13. private static readonly List<String> m_WhiteboxSamplesPackages = new List<String>(new[] {"com.unity.2d.tilemap", "com.unity.2d.tilemap.extras"});
  14. private static ListRequest m_WhiteboxPackageRequest;
  15. private static List<Sample> m_WhiteboxSamples;
  16. private static List<String> m_WhiteboxSampleNames;
  17. private static String[] m_WhiteboxSampleNamesArray;
  18. private static int m_LongestNameIndex = -1;
  19. public static List<Sample> whiteboxSamples
  20. {
  21. get
  22. {
  23. if (m_WhiteboxSamples == null)
  24. GetWhiteboxSamples();
  25. return m_WhiteboxSamples;
  26. }
  27. }
  28. public static List<String> whiteboxSampleNames
  29. {
  30. get
  31. {
  32. if (m_WhiteboxSampleNames == null)
  33. GetWhiteboxSamples();
  34. return m_WhiteboxSampleNames;
  35. }
  36. }
  37. public static String[] whiteboxSampleNamesArray
  38. {
  39. get
  40. {
  41. if (m_WhiteboxSampleNamesArray == null)
  42. GetWhiteboxSamples();
  43. return m_WhiteboxSampleNamesArray;
  44. }
  45. }
  46. public static String longestName
  47. {
  48. get
  49. {
  50. if (m_LongestNameIndex >= 0 && m_LongestNameIndex < m_WhiteboxSampleNames.Count)
  51. return m_WhiteboxSampleNames[m_LongestNameIndex];
  52. return null;
  53. }
  54. }
  55. private static void GetWhiteboxSamples()
  56. {
  57. m_WhiteboxSamples = new List<Sample>();
  58. m_WhiteboxSampleNames = new List<String>();
  59. m_LongestNameIndex = -1;
  60. var packages = PackageManager.PackageInfo.GetAllRegisteredPackages();
  61. foreach (var packageInfo in packages)
  62. {
  63. if (!m_WhiteboxSamplesPackages.Contains(packageInfo.name))
  64. continue;
  65. var samples = Sample.FindByPackage(packageInfo.name, packageInfo.version);
  66. foreach (var sample in samples)
  67. {
  68. if (!sample.displayName.Contains(whiteboxFilterText, StringComparison.CurrentCultureIgnoreCase))
  69. continue;
  70. m_WhiteboxSamples.Add(sample);
  71. m_WhiteboxSampleNames.Add(sample.displayName);
  72. if (m_LongestNameIndex == -1 || longestName.Length < sample.displayName.Length)
  73. m_LongestNameIndex = m_WhiteboxSampleNames.Count - 1;
  74. }
  75. }
  76. m_WhiteboxSampleNamesArray = m_WhiteboxSampleNames.ToArray();
  77. }
  78. internal static void ImportWhiteboxSample(int index)
  79. {
  80. if (index >= whiteboxSamples.Count)
  81. return;
  82. var sample = whiteboxSamples[index];
  83. ImportWhiteboxSample(sample);
  84. }
  85. internal static void ImportWhiteboxSample(Sample sample)
  86. {
  87. sample.Import(Sample.ImportOptions.HideImportWindow);
  88. GridPalettes.CleanCache();
  89. // Try to select the new palette as the current palette
  90. var directoryInfo = new DirectoryInfo(sample.importPath);
  91. var fileInfos = directoryInfo.GetFiles("*.prefab");
  92. foreach (var file in fileInfos)
  93. {
  94. var dataPath = FileUtil.NiceWinPath(Application.dataPath);
  95. var absolutePath = FileUtil.NiceWinPath(file.FullName);
  96. if (!absolutePath.StartsWith(dataPath))
  97. return;
  98. var relativePath= FileUtil.CombinePaths("Assets", absolutePath.Substring(dataPath.Length));
  99. var gridPalette = AssetDatabase.LoadAssetAtPath(relativePath, typeof(GridPalette)) as GridPalette;
  100. if (gridPalette != null)
  101. {
  102. var go = AssetDatabase.LoadAssetAtPath(relativePath, typeof(GameObject)) as GameObject;
  103. GridPaintingState.palette = go;
  104. return;
  105. }
  106. }
  107. }
  108. internal static void DuplicateWhiteboxSample(int index)
  109. {
  110. if (index >= whiteboxSamples.Count)
  111. return;
  112. var sample = whiteboxSamples[index];
  113. DuplicateWhiteboxSample(sample);
  114. }
  115. internal static void DuplicateWhiteboxSample(Sample sample)
  116. {
  117. var path = ProjectBrowser.s_LastInteractedProjectBrowser != null
  118. ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath()
  119. : "Assets";
  120. path = EditorUtility.SaveFilePanelInProject("Generate Whitebox Palette Asset", sample.displayName, "prefab",
  121. "Generate Whitebox Palette Asset", path);
  122. if (String.IsNullOrWhiteSpace(path))
  123. return;
  124. var dirPath = FileUtil.UnityGetDirectoryName(path);
  125. var fileName = FileUtil.UnityGetFileNameWithoutExtension(path);
  126. DuplicateWhiteboxSample(sample, dirPath, fileName);
  127. }
  128. private static void ReplaceGuidInMetaFile(FileInfo fileInfo, out string oldGuid, out string newGuid)
  129. {
  130. const string kGuidLine = "guid: ";
  131. var reader = fileInfo.OpenText();
  132. reader.ReadLine();
  133. var guidLine = reader.ReadLine();
  134. if (!guidLine.StartsWith(kGuidLine))
  135. {
  136. reader.Close();
  137. throw new FormatException("Meta File does not have a valid GUID.");
  138. }
  139. oldGuid = guidLine.Remove(0, kGuidLine.Length);
  140. oldGuid.TrimEnd(Environment.NewLine.ToCharArray());
  141. newGuid = GUID.Generate().ToString();
  142. reader.Close();
  143. var absolutePath = FileUtil.NiceWinPath(fileInfo.FullName);
  144. var readAllText = File.ReadAllText(absolutePath);
  145. var replace = readAllText.Replace(oldGuid, newGuid);
  146. File.WriteAllText(absolutePath, replace);
  147. }
  148. internal static void DuplicateWhiteboxSample(Sample sample, string path, string paletteName = null)
  149. {
  150. AssetDatabase.DisallowAutoRefresh();
  151. AssetDatabase.StartAssetEditing();
  152. var sampleDirectoryInfo = new DirectoryInfo(sample.resolvedPath);
  153. var sampleMetaFileInfos = sampleDirectoryInfo.GetFiles("*.meta");
  154. var sampleMetaNames = new HashSet<string>();
  155. foreach (var sampleMetaFileInfo in sampleMetaFileInfos)
  156. sampleMetaNames.Add(sampleMetaFileInfo.Name);
  157. var tempPath = AssetDatabase.GetUniquePathNameAtSelectedPath("Temp");
  158. FileUtil.CreateOrCleanDirectory(tempPath);
  159. FileUtil.CopyDirectoryRecursiveFiltered(sample.resolvedPath, tempPath, true, @"\.sample\.json$");
  160. // Fix meta references for copied files
  161. var textureIdMap = new Dictionary<string, string>();
  162. var textureDirectoryInfo = new DirectoryInfo(FileUtil.CombinePaths(tempPath, "Textures"));
  163. var textureMetaFileInfos = textureDirectoryInfo.GetFiles("*.meta");
  164. foreach (var metaFileInfo in textureMetaFileInfos)
  165. {
  166. ReplaceGuidInMetaFile(metaFileInfo, out var oldGuid, out var newGuid);
  167. textureIdMap.Add(oldGuid, newGuid);
  168. }
  169. var tileIdMap = new Dictionary<string, string>();
  170. var tileDirectoryInfo = new DirectoryInfo(FileUtil.CombinePaths(tempPath, "Tiles"));
  171. var tileMetaFileInfos = tileDirectoryInfo.GetFiles("*.meta");
  172. foreach (var metaFileInfo in tileMetaFileInfos)
  173. {
  174. ReplaceGuidInMetaFile(metaFileInfo, out var oldGuid, out var newGuid);
  175. tileIdMap.Add(oldGuid, newGuid);
  176. }
  177. var tempBaseDirectoryInfo = new DirectoryInfo(tempPath);
  178. var baseMetaFileInfos = tempBaseDirectoryInfo.GetFiles("*.meta");
  179. foreach (var metaFileInfo in baseMetaFileInfos)
  180. {
  181. if (sampleMetaNames.Contains(metaFileInfo.Name))
  182. ReplaceGuidInMetaFile(metaFileInfo, out var oldGuid, out var newGuid);
  183. }
  184. var tileAssetFileInfos = tileDirectoryInfo.GetFiles("*.asset");
  185. var textureIdReplaceArray = new string[textureIdMap.Count * 2];
  186. {
  187. var i = 0;
  188. foreach (var pair in textureIdMap)
  189. {
  190. textureIdReplaceArray[i++] = pair.Key;
  191. textureIdReplaceArray[i++] = pair.Value;
  192. }
  193. }
  194. foreach (var tileAssetFileInfo in tileAssetFileInfos)
  195. {
  196. var absolutePath = FileUtil.NiceWinPath(tileAssetFileInfo.FullName);
  197. FileUtil.ReplaceTextRegex(absolutePath, textureIdReplaceArray);
  198. }
  199. var prefabFileInfos = tempBaseDirectoryInfo.GetFiles("*.prefab");
  200. var tileIdReplaceArray = new string[tileIdMap.Count * 2];
  201. {
  202. var i = 0;
  203. foreach (var pair in tileIdMap)
  204. {
  205. tileIdReplaceArray[i++] = pair.Key;
  206. tileIdReplaceArray[i++] = pair.Value;
  207. }
  208. }
  209. foreach (var prefabFileInfo in prefabFileInfos)
  210. {
  211. var prefabPath = FileUtil.NiceWinPath(prefabFileInfo.FullName);
  212. FileUtil.ReplaceTextRegex(prefabPath, tileIdReplaceArray);
  213. // Rename Palette if user has changed the name
  214. if (!String.IsNullOrEmpty(paletteName))
  215. {
  216. var paletteNameWithExtension = $"{paletteName}.prefab";
  217. if (!String.Equals(paletteNameWithExtension, prefabFileInfo.Name))
  218. {
  219. var prefabName = FileUtil.CombinePaths(prefabFileInfo.DirectoryName, paletteNameWithExtension);
  220. FileUtil.MoveFileIfExists(prefabPath, prefabName);
  221. FileUtil.MoveFileIfExists($"{prefabPath}.meta", FileUtil.CombinePaths(prefabFileInfo.DirectoryName, $"{paletteNameWithExtension}.meta"));
  222. }
  223. }
  224. }
  225. FileUtil.CopyDirectoryRecursive(tempPath, path, true);
  226. FileUtil.DeleteFileOrDirectory(tempPath);
  227. AssetDatabase.StopAssetEditing();
  228. AssetDatabase.AllowAutoRefresh();
  229. AssetDatabase.Refresh();
  230. GridPalettes.CleanCache();
  231. // Try to select the new palette as the current palette
  232. var baseDirectoryInfo = new DirectoryInfo(path);
  233. prefabFileInfos = baseDirectoryInfo.GetFiles("*.prefab");
  234. foreach (var prefabFileInfo in prefabFileInfos)
  235. {
  236. var dataPath = FileUtil.NiceWinPath(Application.dataPath);
  237. var absolutePath = FileUtil.NiceWinPath(prefabFileInfo.FullName);
  238. if (!absolutePath.StartsWith(dataPath))
  239. return;
  240. var relativePath= FileUtil.CombinePaths("Assets", absolutePath.Substring(dataPath.Length));
  241. var gridPalette = AssetDatabase.LoadAssetAtPath(relativePath, typeof(GridPalette)) as GridPalette;
  242. if (gridPalette != null)
  243. {
  244. var go = AssetDatabase.LoadAssetAtPath(relativePath, typeof(GameObject)) as GameObject;
  245. GridPaintingState.palette = go;
  246. return;
  247. }
  248. }
  249. }
  250. }
  251. }