SpriteLibrary.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEngine.Experimental.U2D.Animation
  5. {
  6. /// <summary>
  7. /// Component that holds a Sprite Library Asset. The component is used by SpriteResolver Component to query for Sprite based on Category and Index
  8. /// </summary>
  9. [DisallowMultipleComponent]
  10. [AddComponentMenu("2D Animation/Sprite Library (Experimental)")]
  11. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/SLAsset.html%23sprite-library-component")]
  12. public class SpriteLibrary : MonoBehaviour
  13. {
  14. internal struct StringAndHash
  15. {
  16. public string name;
  17. public readonly int hash;
  18. public StringAndHash(string name)
  19. {
  20. this.name = name;
  21. hash = SpriteLibraryAsset.GetStringHash(name);
  22. }
  23. public StringAndHash(int hash)
  24. {
  25. name = "";
  26. this.hash = hash;
  27. }
  28. public static bool operator==(StringAndHash l, StringAndHash r)
  29. {
  30. return l.Equals(r);
  31. }
  32. public static bool operator!=(StringAndHash l, StringAndHash r)
  33. {
  34. return !l.Equals(r);
  35. }
  36. public override bool Equals(object obj)
  37. {
  38. if (obj == null || this.GetType() != obj.GetType())
  39. return false;
  40. return this.Equals((StringAndHash)obj);
  41. }
  42. private bool Equals(StringAndHash p)
  43. {
  44. // If run-time types are not exactly the same, return false.
  45. if (this.GetType() != p.GetType())
  46. return false;
  47. return (hash == p.hash) || (name == p.name);
  48. }
  49. public override int GetHashCode()
  50. {
  51. return hash;
  52. }
  53. }
  54. [SerializeField]
  55. private SpriteLibraryAsset m_SpriteLibraryAsset;
  56. Dictionary<StringAndHash, Dictionary<StringAndHash, Sprite>> m_Overrides = new Dictionary<StringAndHash, Dictionary<StringAndHash, Sprite>>();
  57. /// <summary>Get or Set the current SpriteLibraryAsset to use </summary>
  58. public SpriteLibraryAsset spriteLibraryAsset
  59. {
  60. set
  61. {
  62. if (m_SpriteLibraryAsset != value)
  63. {
  64. m_SpriteLibraryAsset = value;
  65. RefreshSpriteResolvers();
  66. }
  67. }
  68. get { return m_SpriteLibraryAsset; }
  69. }
  70. /// <summary>
  71. /// Return the Sprite that is registered for the given Category and Label for the SpriteLibrary
  72. /// </summary>
  73. /// <param name="category">Category name</param>
  74. /// <param name="label">Label name</param>
  75. /// <returns>Sprite associated to the name and index</returns>
  76. public Sprite GetSprite(string category, string label)
  77. {
  78. var categoryHash = SpriteLibraryAsset.GetStringHash(category);
  79. var labelHash = SpriteLibraryAsset.GetStringHash(label);
  80. return GetSprite(categoryHash, labelHash);
  81. }
  82. internal Sprite GetSprite(int categoryHash, int labelHash)
  83. {
  84. return GetSprite(categoryHash, labelHash, out _);
  85. }
  86. internal Sprite GetSprite(int categoryHash, int labelHash, out bool validEntry)
  87. {
  88. validEntry = false;
  89. var cat = new StringAndHash(categoryHash);
  90. var label = new StringAndHash(labelHash);
  91. if (m_Overrides.ContainsKey(cat) && m_Overrides[cat].ContainsKey(label))
  92. {
  93. validEntry = true;
  94. return m_Overrides[cat][label];
  95. }
  96. return m_SpriteLibraryAsset == null ? null : m_SpriteLibraryAsset.GetSprite(categoryHash, labelHash, out validEntry);
  97. }
  98. internal string GetCategoryNameFromHash(int categoryHash)
  99. {
  100. var key = m_Overrides.Keys.FirstOrDefault(x => x.hash == categoryHash);
  101. if (key != default)
  102. return key.name;
  103. return m_SpriteLibraryAsset == null ? "" : m_SpriteLibraryAsset.GetCategoryNameFromHash(categoryHash);
  104. }
  105. internal string GetLabelNameFromHash(int categoryHash, int labelHash)
  106. {
  107. var overrides = GetCategoryOverride(new StringAndHash(categoryHash), false);
  108. var label = overrides.Keys.FirstOrDefault(x => x.hash == labelHash);
  109. if (label != default)
  110. return label.name;
  111. return m_SpriteLibraryAsset == null ? "" : m_SpriteLibraryAsset.GetLabelNameFromHash(categoryHash, labelHash);
  112. }
  113. private Dictionary<StringAndHash, Sprite> GetCategoryOverride(string category, bool addToList)
  114. {
  115. return GetCategoryOverride(new StringAndHash(category), addToList);
  116. }
  117. private Dictionary<StringAndHash, Sprite> GetCategoryOverride(StringAndHash category, bool addToList)
  118. {
  119. Dictionary<StringAndHash, Sprite> label;
  120. if (m_Overrides.ContainsKey(category))
  121. label = m_Overrides[category];
  122. else
  123. label = new Dictionary<StringAndHash, Sprite>();
  124. if (addToList && !m_Overrides.ContainsKey(category))
  125. {
  126. if (string.IsNullOrEmpty(category.name))
  127. Debug.LogWarning("Adding override category with no name");
  128. m_Overrides.Add(category, label);
  129. }
  130. return label;
  131. }
  132. private void AddSpriteToOverride(Dictionary<StringAndHash, Sprite> overrides, StringAndHash label, Sprite sprite)
  133. {
  134. if (overrides.ContainsKey(label))
  135. overrides[label] = sprite;
  136. else
  137. overrides.Add(label, sprite);
  138. RefreshSpriteResolvers();
  139. }
  140. /// <summary>
  141. /// Add or replace an override when querying for the given Category and Label from a SpriteLibraryAsset
  142. /// </summary>
  143. /// <param name="spriteLib">Sprite Library Asset to query</param>
  144. /// <param name="category">Category name from the Sprite Library Asset to add override</param>
  145. /// <param name="label">Label name to add override</param>
  146. public void AddOverride(SpriteLibraryAsset spriteLib, string category, string label)
  147. {
  148. var sprite = spriteLib.GetSprite(category, label);
  149. var overridelabel = GetCategoryOverride(category, true);
  150. AddSpriteToOverride(overridelabel, new StringAndHash(label), sprite);
  151. }
  152. /// <summary>
  153. /// Add or replace an override when querying for the given Category. All the categories in the Category will be added.
  154. /// </summary>
  155. /// <param name="spriteLib">Sprite Library Asset to query</param>
  156. /// <param name="category">Category name from the Sprite Library Asset to add override</param>
  157. public void AddOverride(SpriteLibraryAsset spriteLib, string category)
  158. {
  159. var categoryHash = SpriteLibraryAsset.GetStringHash(category);
  160. var cat = spriteLib.categories.FirstOrDefault(x => x.hash == categoryHash);
  161. if (cat != null)
  162. {
  163. var label = GetCategoryOverride(category, true);
  164. for (int i = 0; i < cat.categoryList.Count; ++i)
  165. {
  166. AddSpriteToOverride(label, new StringAndHash(cat.categoryList[i].name), cat.categoryList[i].sprite);
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Add or replace an override when querying for the given Category and Label.
  172. /// </summary>
  173. /// <param name="sprite">Sprite to override to</param>
  174. /// <param name="category">Category name to override</param>
  175. /// <param name="label">Label name to override</param>
  176. public void AddOverride(Sprite sprite, string category, string label)
  177. {
  178. var overridelabel = GetCategoryOverride(category, true);
  179. AddSpriteToOverride(overridelabel, new StringAndHash(label), sprite);
  180. }
  181. /// <summary>
  182. /// Remove all Sprite Library override for a given category
  183. /// </summary>
  184. /// <param name="category">Category overrides to remove</param>
  185. public void RemoveOverride(string category)
  186. {
  187. var hash = new StringAndHash(SpriteLibraryAsset.GetStringHash(category));
  188. m_Overrides.Remove(hash);
  189. RefreshSpriteResolvers();
  190. }
  191. /// <summary>
  192. /// Remove Sprite Library override for a given category and label
  193. /// </summary>
  194. /// <param name="category">Category to remove</param>
  195. /// <param name="label">Label to remove</param>
  196. public void RemoveOverride(string category, string label)
  197. {
  198. var catlabel = GetCategoryOverride(category, false);
  199. if (catlabel != null)
  200. {
  201. catlabel.Remove(new StringAndHash(SpriteLibraryAsset.GetStringHash(label)));
  202. RefreshSpriteResolvers();
  203. }
  204. }
  205. /// <summary>
  206. /// Method to check if a Category and Label pair has an override
  207. /// </summary>
  208. /// <param name="category">Category name</param>
  209. /// <param name="label">Label name</param>
  210. /// <returns>True if override exist, false otherwise</returns>
  211. public bool HasOverride(string category, string label)
  212. {
  213. var catOverride = GetCategoryOverride(category, false);
  214. if (catOverride != null)
  215. return catOverride.ContainsKey(new StringAndHash(label));
  216. return false;
  217. }
  218. internal List<SpriteLibCategory> labels
  219. {
  220. get { return m_SpriteLibraryAsset != null ? m_SpriteLibraryAsset.categories : new List<SpriteLibCategory>(); }
  221. }
  222. /// <summary>
  223. /// Request SpriteResolver components that are in the same hierarchy to refresh
  224. /// </summary>
  225. public void RefreshSpriteResolvers()
  226. {
  227. var spriteResolvers = GetComponentsInChildren<SpriteResolver>();
  228. foreach (var sr in spriteResolvers)
  229. {
  230. sr.ResolveSpriteToSpriteRenderer();
  231. #if UNITY_EDITOR
  232. sr.spriteLibChanged = true;
  233. #endif
  234. }
  235. }
  236. }
  237. }