SpriteLibraryAsset.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine.Assertions;
  6. namespace UnityEngine.Experimental.U2D.Animation
  7. {
  8. internal interface INameHash
  9. {
  10. string name { get; set; }
  11. int hash { get; }
  12. }
  13. [Serializable]
  14. internal class Categorylabel : INameHash
  15. {
  16. [SerializeField]
  17. string m_Name;
  18. [SerializeField]
  19. [HideInInspector]
  20. int m_Hash;
  21. [SerializeField]
  22. Sprite m_Sprite;
  23. public string name
  24. {
  25. get { return m_Name; }
  26. set
  27. {
  28. m_Name = value;
  29. m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
  30. }
  31. }
  32. public int hash { get { return m_Hash; } }
  33. public Sprite sprite {get { return m_Sprite; } set { m_Sprite = value; }}
  34. public void UpdateHash()
  35. {
  36. m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
  37. }
  38. }
  39. [Serializable]
  40. internal class SpriteLibCategory : INameHash
  41. {
  42. [SerializeField]
  43. string m_Name;
  44. [SerializeField]
  45. int m_Hash;
  46. [SerializeField]
  47. List<Categorylabel> m_CategoryList;
  48. public string name
  49. {
  50. get { return m_Name; }
  51. set
  52. {
  53. m_Name = value;
  54. m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
  55. }
  56. }
  57. public int hash { get { return m_Hash; } }
  58. public List<Categorylabel> categoryList
  59. {
  60. get { return m_CategoryList; }
  61. set { m_CategoryList = value; }
  62. }
  63. public void UpdateHash()
  64. {
  65. m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
  66. foreach (var s in m_CategoryList)
  67. s.UpdateHash();
  68. }
  69. internal void ValidateLabels()
  70. {
  71. SpriteLibraryAsset.RenameDuplicate(m_CategoryList,
  72. (originalName, newName)
  73. =>
  74. {
  75. Debug.LogWarning(string.Format("Label {0} renamed to {1} due to hash clash", originalName, newName));
  76. });
  77. }
  78. }
  79. /// <summary>
  80. /// A custom Asset that stores Sprites grouping
  81. /// </summary>
  82. /// <Description>
  83. /// Sprites are grouped under a given category as categories. Each category and label needs to have
  84. /// a name specified so that it can be queried.
  85. /// </Description>
  86. [CreateAssetMenu(order = 9, menuName = "2D/Sprite Library Asset")]
  87. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/SLAsset.html")]
  88. public class SpriteLibraryAsset : ScriptableObject
  89. {
  90. [SerializeField]
  91. private List<SpriteLibCategory> m_Labels = new List<SpriteLibCategory>();
  92. internal List<SpriteLibCategory> categories
  93. {
  94. get
  95. {
  96. return m_Labels;
  97. }
  98. set
  99. {
  100. m_Labels = value;
  101. ValidateCategories();
  102. }
  103. }
  104. internal Sprite GetSprite(int categoryHash, int labelHash)
  105. {
  106. var category = m_Labels.FirstOrDefault(x => x.hash == categoryHash);
  107. if (category != null)
  108. {
  109. var spritelabel = category.categoryList.FirstOrDefault(x => x.hash == labelHash);
  110. if (spritelabel != null)
  111. {
  112. return spritelabel.sprite;
  113. }
  114. }
  115. return null;
  116. }
  117. internal Sprite GetSprite(int categoryHash, int labelHash, out bool validEntry)
  118. {
  119. SpriteLibCategory category = null;
  120. for (int i = 0; i < m_Labels.Count; ++i)
  121. {
  122. if (m_Labels[i].hash == categoryHash)
  123. {
  124. category = m_Labels[i];
  125. break;
  126. }
  127. }
  128. if (category != null)
  129. {
  130. Categorylabel spritelabel = null;
  131. for (int i = 0; i < category.categoryList.Count; ++i)
  132. {
  133. if (category.categoryList[i].hash == labelHash)
  134. {
  135. spritelabel = category.categoryList[i];
  136. break;
  137. }
  138. }
  139. if (spritelabel != null)
  140. {
  141. validEntry = true;
  142. return spritelabel.sprite;
  143. }
  144. }
  145. validEntry = false;
  146. return null;
  147. }
  148. /// <summary>
  149. /// Returns the Sprite registered in the Asset given the Category and Label value
  150. /// </summary>
  151. /// <param name="category">Category string value</param>
  152. /// <param name="label">Label string value</param>
  153. /// <returns></returns>
  154. public Sprite GetSprite(string category, string label)
  155. {
  156. var categoryHash = SpriteLibraryAsset.GetStringHash(category);
  157. var labelHash = SpriteLibraryAsset.GetStringHash(label);
  158. return GetSprite(categoryHash, labelHash);
  159. }
  160. /// <summary>
  161. /// Return all the Category names of the Sprite Library Asset that is associated.
  162. /// </summary>
  163. /// <returns>A Enumerable string value representing the name</returns>
  164. public IEnumerable<string> GetCategoryNames()
  165. {
  166. return m_Labels.Select(x => x.name);
  167. }
  168. /// <summary>
  169. /// (Obsolete) Returns the labels' name for the given name
  170. /// </summary>
  171. /// <param name="category">Category name</param>
  172. /// <returns>A Enumerable string representing labels' name</returns>
  173. [Obsolete("GetCategorylabelNames has been deprecated. Please use GetCategoryLabelNames (UnityUpgradable) -> GetCategoryLabelNames(*)")]
  174. public IEnumerable<string> GetCategorylabelNames(string category)
  175. {
  176. return GetCategoryLabelNames(category);
  177. }
  178. /// <summary>
  179. /// Returns the labels' name for the given name
  180. /// </summary>
  181. /// <param name="category">Category name</param>
  182. /// <returns>A Enumerable string representing labels' name</returns>
  183. public IEnumerable<string> GetCategoryLabelNames(string category)
  184. {
  185. var label = m_Labels.FirstOrDefault(x => x.name == category);
  186. return label == null ? new string[0] : label.categoryList.Select(x => x.name);
  187. }
  188. internal string GetCategoryNameFromHash(int hash)
  189. {
  190. var label = m_Labels.FirstOrDefault(x => x.hash == hash);
  191. return label == null ? "" : label.name;
  192. }
  193. /// <summary>
  194. /// Add or replace and existing Sprite into the given Category and Label
  195. /// </summary>
  196. /// <param name="sprite">Sprite to add</param>
  197. /// <param name="category">Category to add the Sprite to</param>
  198. /// <param name="label">Label of the Category to add the Sprite to</param>
  199. public void AddCategoryLabel(Sprite sprite, string category, string label)
  200. {
  201. category = category.Trim();
  202. label = label.Trim();
  203. if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(label))
  204. {
  205. Debug.LogError("Cannot add label with empty or null Category or label string");
  206. }
  207. var catHash = SpriteLibraryAsset.GetStringHash(category);
  208. Categorylabel categorylabel = null;
  209. SpriteLibCategory libCategory = null;
  210. libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash);
  211. if (libCategory != null)
  212. {
  213. Assert.AreEqual(libCategory.name, category, "Category string hash clashes with another existing Category. Please use another string");
  214. var labelHash = SpriteLibraryAsset.GetStringHash(label);
  215. categorylabel = libCategory.categoryList.FirstOrDefault(y => y.hash == labelHash);
  216. if (categorylabel != null)
  217. {
  218. Assert.AreEqual(categorylabel.name, label, "Label string hash clashes with another existing label. Please use another string");
  219. categorylabel.sprite = sprite;
  220. }
  221. else
  222. {
  223. categorylabel = new Categorylabel()
  224. {
  225. name = label,
  226. sprite = sprite
  227. };
  228. libCategory.categoryList.Add(categorylabel);
  229. }
  230. }
  231. else
  232. {
  233. var slc = new SpriteLibCategory()
  234. {
  235. categoryList = new List<Categorylabel>()
  236. {
  237. new Categorylabel()
  238. {
  239. name = label,
  240. sprite = sprite
  241. }
  242. },
  243. name = category
  244. };
  245. m_Labels.Add(slc);
  246. }
  247. #if UNITY_EDITOR
  248. EditorUtility.SetDirty(this);
  249. #endif
  250. }
  251. /// <summary>
  252. /// Remove a Label from a given Category
  253. /// </summary>
  254. /// <param name="category">Category to remove from</param>
  255. /// <param name="label">Label to remove</param>
  256. /// <param name="deleteCategory">Indicate to remove the Category if it is empty</param>
  257. public void RemoveCategoryLabel(string category, string label, bool deleteCategory)
  258. {
  259. var catHash = SpriteLibraryAsset.GetStringHash(category);
  260. SpriteLibCategory libCategory = null;
  261. libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash);
  262. if (libCategory != null)
  263. {
  264. var labelHash = SpriteLibraryAsset.GetStringHash(label);
  265. libCategory.categoryList.RemoveAll(x => x.hash == labelHash);
  266. if (deleteCategory && libCategory.categoryList.Count == 0)
  267. m_Labels.RemoveAll(x => x.hash == libCategory.hash);
  268. #if UNITY_EDITOR
  269. EditorUtility.SetDirty(this);
  270. #endif
  271. }
  272. }
  273. internal string GetLabelNameFromHash(int categoryHas, int labelHash)
  274. {
  275. var labels = m_Labels.FirstOrDefault(x => x.hash == categoryHas);
  276. if (labels != null)
  277. {
  278. var label = labels.categoryList.FirstOrDefault(x => x.hash == labelHash);
  279. return label == null ? "" : label.name;
  280. }
  281. return "";
  282. }
  283. internal void UpdateHashes()
  284. {
  285. foreach (var e in m_Labels)
  286. e.UpdateHash();
  287. #if UNITY_EDITOR
  288. UnityEditor.EditorUtility.SetDirty(this);
  289. #endif
  290. }
  291. internal void ValidateCategories()
  292. {
  293. RenameDuplicate(m_Labels, (originalName, newName)
  294. =>
  295. {
  296. Debug.LogWarning(string.Format("Category {0} renamed to {1} due to hash clash", originalName, newName));
  297. });
  298. for (int i = 0; i < m_Labels.Count; ++i)
  299. {
  300. // Verify categories have no hash clash
  301. var category = m_Labels[i];
  302. // Verify labels have no clash
  303. category.ValidateLabels();
  304. }
  305. }
  306. internal static void RenameDuplicate(IEnumerable<INameHash> nameHashList, Action<string, string> onRename)
  307. {
  308. const int k_IncrementMax = 1000;
  309. for (int i = 0; i < nameHashList.Count(); ++i)
  310. {
  311. // Verify categories have no hash clash
  312. var category = nameHashList.ElementAt(i);
  313. var categoriesClash = nameHashList.Where(x => (x.hash == category.hash || x.name == category.name) && x != category);
  314. int increment = 0;
  315. for (int j = 0; j < categoriesClash.Count(); ++j)
  316. {
  317. var categoryClash = categoriesClash.ElementAt(j);
  318. while (increment < k_IncrementMax)
  319. {
  320. var name = categoryClash.name;
  321. name = string.Format("{0}_{1}", name, increment);
  322. var nameHash = SpriteLibraryAsset.GetStringHash(name);
  323. var exist = nameHashList.FirstOrDefault(x => (x.hash == nameHash || x.name == name) && x != categoryClash);
  324. if (exist == null)
  325. {
  326. onRename(categoryClash.name, name);
  327. categoryClash.name = name;
  328. break;
  329. }
  330. ++increment;
  331. }
  332. }
  333. }
  334. }
  335. // Allow delegate override for test
  336. internal static Func<string, int> GetStringHash = Default_GetStringHash;
  337. internal static int Default_GetStringHash(string value)
  338. {
  339. #if DEBUG_GETSTRINGHASH_CLASH
  340. if (value == "abc" || value == "123")
  341. value = "abc";
  342. #endif
  343. var hash = Animator.StringToHash(value);
  344. var bytes = BitConverter.GetBytes(hash);
  345. var exponentialBit = BitConverter.IsLittleEndian ? 3 : 1;
  346. if (bytes[exponentialBit] == 0xFF)
  347. bytes[exponentialBit] -= 1;
  348. return BitConverter.ToInt32(bytes, 0);
  349. }
  350. }
  351. }