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

ImportUtilites.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using PDNWrapper;
  7. using Unity.Collections;
  8. using UnityEditor.U2D.Animation;
  9. using UnityEngine;
  10. namespace UnityEditor.U2D.PSD
  11. {
  12. class UniqueNameGenerator
  13. {
  14. HashSet<int> m_NameHash = new HashSet<int>();
  15. public bool ContainHash(int i)
  16. {
  17. return m_NameHash.Contains(i);
  18. }
  19. public void AddHash(int i)
  20. {
  21. m_NameHash.Add(i);
  22. }
  23. public void AddHash(string name)
  24. {
  25. m_NameHash.Add(GetStringHash(name));
  26. }
  27. public string GetUniqueName(string name, bool logNewNameGenerated = false, UnityEngine.Object context = null)
  28. {
  29. return GetUniqueName(name, m_NameHash);
  30. }
  31. static string GetUniqueName(string name, HashSet<int> stringHash, bool logNewNameGenerated = false, UnityEngine.Object context = null)
  32. {
  33. var sanitizedName = string.Copy(SanitizeName(name));
  34. string uniqueName = sanitizedName;
  35. int index = 1;
  36. while (true)
  37. {
  38. var hash = GetStringHash(uniqueName);
  39. if (!stringHash.Contains(hash))
  40. {
  41. stringHash.Add(hash);
  42. if (logNewNameGenerated && sanitizedName != uniqueName)
  43. Debug.Log(string.Format("Asset name {0} is changed to {1} to ensure uniqueness", name, uniqueName), context);
  44. return uniqueName;
  45. }
  46. uniqueName = string.Format("{0}_{1}", sanitizedName, index);
  47. ++index;
  48. }
  49. }
  50. static string SanitizeName(string name)
  51. {
  52. name = name.Replace('\0', ' ');
  53. string newName = null;
  54. // We can't create asset name with these name.
  55. if ((name.Length == 2 && name[0] == '.' && name[1] == '.')
  56. || (name.Length == 1 && name[0] == '.')
  57. || (name.Length == 1 && name[0] == '/'))
  58. newName += name + "_";
  59. if (!string.IsNullOrEmpty(newName))
  60. {
  61. Debug.LogWarning(string.Format("File contains layer with invalid name for generating asset. {0} is renamed to {1}", name, newName));
  62. return newName;
  63. }
  64. return name;
  65. }
  66. static int GetStringHash(string str)
  67. {
  68. MD5 md5Hasher = MD5.Create();
  69. var hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(str));
  70. return BitConverter.ToInt32(hashed, 0);
  71. }
  72. }
  73. class GameObjectCreationFactory : UniqueNameGenerator
  74. {
  75. public GameObjectCreationFactory(IList<string> names)
  76. {
  77. if (names != null)
  78. {
  79. foreach (var name in names)
  80. GetUniqueName(name);
  81. }
  82. }
  83. public GameObject CreateGameObject(string name, params System.Type[] components)
  84. {
  85. var newName = GetUniqueName(name);
  86. return new GameObject(newName, components);
  87. }
  88. }
  89. internal static class ImportUtilities
  90. {
  91. public static string SaveToPng(NativeArray<Color32> buffer, int width, int height)
  92. {
  93. if (!buffer.IsCreated ||
  94. buffer.Length == 0 ||
  95. width == 0 ||
  96. height == 0)
  97. return "No .png generated.";
  98. var texture2D = new Texture2D(width, height);
  99. texture2D.SetPixels32(buffer.ToArray());
  100. var png = texture2D.EncodeToPNG();
  101. var path = Application.dataPath + $"/tex_{System.Guid.NewGuid().ToString()}.png";
  102. var fileStream = System.IO.File.Create(path);
  103. fileStream.Write(png);
  104. fileStream.Close();
  105. UnityEngine.Object.DestroyImmediate(texture2D);
  106. return path;
  107. }
  108. public static void ValidatePSDLayerId(IEnumerable<PSDLayer> oldPsdLayer, IEnumerable<BitmapLayer> layers, UniqueNameGenerator uniqueNameGenerator)
  109. {
  110. // first check if all layers are unique. If not, we use back the previous layer id based on name match
  111. var uniqueIdSet = new HashSet<int>();
  112. var useOldID = false;
  113. foreach(var layer in layers)
  114. {
  115. if (uniqueIdSet.Contains(layer.LayerID))
  116. {
  117. useOldID = true;
  118. break;
  119. }
  120. uniqueIdSet.Add(layer.LayerID);
  121. }
  122. for (var i = 0; i < layers.Count(); ++i)
  123. {
  124. var childBitmapLayer = layers.ElementAt(i);
  125. // fix case 1291323
  126. if (useOldID)
  127. {
  128. var oldLayers = oldPsdLayer.Where(x => x.name == childBitmapLayer.Name);
  129. if (oldLayers.Count() == 0)
  130. oldLayers = oldPsdLayer.Where(x => x.layerID == childBitmapLayer.Name.GetHashCode());
  131. // pick one that is not already on the list
  132. foreach (var ol in oldLayers)
  133. {
  134. if (!uniqueNameGenerator.ContainHash(ol.layerID))
  135. {
  136. childBitmapLayer.LayerID = ol.layerID;
  137. break;
  138. }
  139. }
  140. }
  141. if (uniqueNameGenerator.ContainHash(childBitmapLayer.LayerID))
  142. {
  143. var importWarning = string.Format("Layer {0}: LayerId is not unique. Mapping will be done by Layer's name.", childBitmapLayer.Name);
  144. var layerName = uniqueNameGenerator.GetUniqueName(childBitmapLayer.Name);
  145. if (layerName != childBitmapLayer.Name)
  146. importWarning += "\nLayer names are not unique. Please ensure they are unique to for SpriteRect to be mapped back correctly.";
  147. childBitmapLayer.LayerID = layerName.GetHashCode();
  148. Debug.LogWarning(importWarning);
  149. }
  150. else
  151. uniqueNameGenerator.AddHash(childBitmapLayer.LayerID);
  152. if (childBitmapLayer.ChildLayer != null)
  153. {
  154. ValidatePSDLayerId(oldPsdLayer, childBitmapLayer.ChildLayer, uniqueNameGenerator);
  155. }
  156. }
  157. }
  158. public static void TranslatePivotPoint(Vector2 pivot, Rect rect, out SpriteAlignment alignment, out Vector2 customPivot)
  159. {
  160. customPivot = pivot;
  161. if (new Vector2(rect.xMin, rect.yMax) == pivot)
  162. alignment = SpriteAlignment.TopLeft;
  163. else if(new Vector2(rect.center.x, rect.yMax) == pivot)
  164. alignment = SpriteAlignment.TopCenter;
  165. else if(new Vector2(rect.xMax, rect.yMax) == pivot)
  166. alignment = SpriteAlignment.TopRight;
  167. else if(new Vector2(rect.xMin, rect.center.y) == pivot)
  168. alignment = SpriteAlignment.LeftCenter;
  169. else if(new Vector2(rect.center.x, rect.center.y) == pivot)
  170. alignment = SpriteAlignment.Center;
  171. else if(new Vector2(rect.xMax, rect.center.y) == pivot)
  172. alignment = SpriteAlignment.RightCenter;
  173. else if(new Vector2(rect.xMin, rect.yMin) == pivot)
  174. alignment = SpriteAlignment.BottomLeft;
  175. else if(new Vector2(rect.center.x, rect.yMin) == pivot)
  176. alignment = SpriteAlignment.BottomCenter;
  177. else if(new Vector2(rect.xMax, rect.yMin) == pivot)
  178. alignment = SpriteAlignment.BottomRight;
  179. else
  180. alignment = SpriteAlignment.Custom;
  181. }
  182. public static Vector2 GetPivotPoint(Rect rect, SpriteAlignment alignment, Vector2 customPivot)
  183. {
  184. switch (alignment)
  185. {
  186. case SpriteAlignment.TopLeft:
  187. return new Vector2(rect.xMin, rect.yMax);
  188. case SpriteAlignment.TopCenter:
  189. return new Vector2(rect.center.x, rect.yMax);
  190. case SpriteAlignment.TopRight:
  191. return new Vector2(rect.xMax, rect.yMax);
  192. case SpriteAlignment.LeftCenter:
  193. return new Vector2(rect.xMin, rect.center.y);
  194. case SpriteAlignment.Center:
  195. return new Vector2(rect.center.x, rect.center.y);
  196. case SpriteAlignment.RightCenter:
  197. return new Vector2(rect.xMax, rect.center.y);
  198. case SpriteAlignment.BottomLeft:
  199. return new Vector2(rect.xMin, rect.yMin);
  200. case SpriteAlignment.BottomCenter:
  201. return new Vector2(rect.center.x, rect.yMin);
  202. case SpriteAlignment.BottomRight:
  203. return new Vector2(rect.xMax, rect.yMin);
  204. case SpriteAlignment.Custom:
  205. return new Vector2(customPivot.x * rect.width, customPivot.y * rect.height);
  206. }
  207. return Vector2.zero;
  208. }
  209. public static string GetUniqueSpriteName(string name, UniqueNameGenerator generator, bool keepDupilcateSpriteName)
  210. {
  211. if (keepDupilcateSpriteName)
  212. return name;
  213. return generator.GetUniqueName(name);
  214. }
  215. public static bool VisibleInHierarchy(List<PSDLayer> psdGroup, int index)
  216. {
  217. var psdLayer = psdGroup[index];
  218. var parentVisible = true;
  219. if (psdLayer.parentIndex >= 0)
  220. parentVisible = VisibleInHierarchy(psdGroup, psdLayer.parentIndex);
  221. return parentVisible && psdLayer.isVisible;
  222. }
  223. public static bool SpriteIsMainFromSpriteLib(List<SpriteCategory> categories, string spriteId, out string categoryName)
  224. {
  225. categoryName = "";
  226. if (categories != null)
  227. {
  228. foreach (var category in categories)
  229. {
  230. var index = category.labels.FindIndex(x => x.spriteId == spriteId);
  231. if (index == 0)
  232. {
  233. categoryName = category.name;
  234. return true;
  235. }
  236. if (index > 0)
  237. return false;
  238. }
  239. }
  240. return true;
  241. }
  242. }
  243. }