PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/VoteMario/Library/PackageCache/com.unity.2d.animation@6.0.7/Runtime/SpriteLib/SpriteResolver.cs

https://gitlab.com/Surabhi124/vote-mario
C# | 278 lines | 213 code | 30 blank | 35 comment | 34 complexity | 83dbfa17a27b6bbe4a14645a895ac2d0 MD5 | raw file
  1. using System;
  2. using UnityEngine.Animations;
  3. using UnityEngine.Scripting.APIUpdating;
  4. namespace UnityEngine.U2D.Animation
  5. {
  6. /// <summary>
  7. /// Updates a SpriteRenderer's Sprite reference on the Category and Label value it is set
  8. /// </summary>
  9. /// <Description>
  10. /// By setting the SpriteResolver's Category and Label value, it will request for a Sprite from
  11. /// a SpriteLibrary Component the Sprite that is registered for the Category and Label.
  12. /// If a SpriteRenderer is present in the same GameObject, the SpriteResolver will update the
  13. /// SpriteRenderer's Sprite reference to the corresponding Sprite.
  14. /// </Description>
  15. [ExecuteInEditMode]
  16. [DisallowMultipleComponent]
  17. [AddComponentMenu("2D Animation/Sprite Resolver")]
  18. [DefaultExecutionOrder(-2)]
  19. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/SLAsset.html%23sprite-resolver-component")]
  20. [MovedFrom("UnityEngine.Experimental.U2D.Animation")]
  21. public class SpriteResolver : MonoBehaviour, ISerializationCallbackReceiver
  22. {
  23. // SpriteKey is the new animation key.
  24. // We are keeping the old ones so that the animation clip doesn't braek
  25. // These are for animation
  26. [SerializeField]
  27. private float m_CategoryHash = 0;
  28. [SerializeField]
  29. private float m_labelHash = 0;
  30. [SerializeField]
  31. private float m_SpriteKey = 0;
  32. // For comparing hash values
  33. private int m_CategoryHashInt;
  34. private int m_LabelHashInt;
  35. private int m_SpriteKeyInt;
  36. // For OnUpdate during animation playback
  37. private int m_PreviousCategoryHash;
  38. private int m_PreviouslabelHash;
  39. private int m_PreviousSpriteKeyInt;
  40. #if UNITY_EDITOR
  41. bool m_SpriteLibChanged;
  42. public event Action onDeserializedCallback = () => { };
  43. #endif
  44. void Reset()
  45. {
  46. // If the Sprite referred to by the SpriteRenderer exist in the library,
  47. // we select the Sprite
  48. if(spriteRenderer)
  49. SetSprite(spriteRenderer.sprite);
  50. }
  51. void SetSprite(Sprite sprite)
  52. {
  53. var sl = spriteLibrary;
  54. if (sl != null && sprite != null)
  55. {
  56. foreach (var cat in sl.categoryNames)
  57. {
  58. var entries = sl.GetEntryNames(cat);
  59. foreach (var ent in entries)
  60. {
  61. if (sl.GetSprite(cat, ent) == sprite)
  62. {
  63. spriteKeyInt = SpriteLibrary.GetHashForCategoryAndEntry(cat, ent);
  64. return;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. void OnEnable()
  71. {
  72. m_CategoryHashInt = ConvertFloatToInt(m_CategoryHash);
  73. m_PreviousCategoryHash = m_CategoryHashInt;
  74. m_LabelHashInt = ConvertFloatToInt(m_labelHash);
  75. m_PreviouslabelHash = m_LabelHashInt;
  76. m_SpriteKeyInt = ConvertFloatToInt(m_SpriteKey);
  77. if (m_SpriteKeyInt == 0)
  78. {
  79. m_SpriteKey = ConvertCategoryLabelHashToSpriteKey(spriteLibrary, m_CategoryHashInt, m_LabelHashInt);
  80. m_SpriteKeyInt = ConvertFloatToInt(m_SpriteKey);
  81. }
  82. m_PreviousSpriteKeyInt = m_SpriteKeyInt;
  83. ResolveSpriteToSpriteRenderer();
  84. }
  85. SpriteRenderer spriteRenderer
  86. {
  87. get { return GetComponent<SpriteRenderer>(); }
  88. }
  89. /// <summary>
  90. /// Set the Category and label to use
  91. /// </summary>
  92. /// <param name="category">Category to use</param>
  93. /// <param name="label">Label to use</param>
  94. public void SetCategoryAndLabel(string category, string label)
  95. {
  96. spriteKeyInt = SpriteLibrary.GetHashForCategoryAndEntry(category, label);
  97. m_PreviousSpriteKeyInt = spriteKeyInt;
  98. ResolveSpriteToSpriteRenderer();
  99. }
  100. /// <summary>
  101. /// Get the Category set for the SpriteResolver
  102. /// </summary>
  103. /// <returns>The Category's name</returns>
  104. public string GetCategory()
  105. {
  106. var returnString = "";
  107. var sl = spriteLibrary;
  108. if (sl)
  109. {
  110. sl.GetCategoryAndEntryNameFromHash(spriteKeyInt, out returnString, out _);
  111. }
  112. return returnString;
  113. }
  114. /// <summary>
  115. /// Get the Label set for the SpriteResolver
  116. /// </summary>
  117. /// <returns>The Label's name</returns>
  118. public string GetLabel()
  119. {
  120. var returnString = "";
  121. var sl = spriteLibrary;
  122. if (sl)
  123. sl.GetCategoryAndEntryNameFromHash(spriteKeyInt, out _, out returnString);
  124. return returnString;
  125. }
  126. /// <summary>
  127. /// Property to get the SpriteLibrary the SpriteResolver is resolving from
  128. /// </summary>
  129. public SpriteLibrary spriteLibrary => gameObject.GetComponentInParent<SpriteLibrary>(true);
  130. void LateUpdate()
  131. {
  132. m_SpriteKeyInt = ConvertFloatToInt(m_SpriteKey);
  133. if (m_SpriteKeyInt != m_PreviousSpriteKeyInt)
  134. {
  135. m_PreviousSpriteKeyInt = m_SpriteKeyInt;
  136. ResolveSpriteToSpriteRenderer();
  137. }
  138. else
  139. {
  140. m_CategoryHashInt = ConvertFloatToInt(m_CategoryHash);
  141. m_LabelHashInt = ConvertFloatToInt(m_labelHash);
  142. if (m_LabelHashInt != m_PreviouslabelHash || m_CategoryHashInt != m_PreviousCategoryHash)
  143. {
  144. if (spriteLibrary != null)
  145. {
  146. m_PreviousCategoryHash = m_CategoryHashInt;
  147. m_PreviouslabelHash = m_LabelHashInt;
  148. m_SpriteKey = ConvertCategoryLabelHashToSpriteKey(spriteLibrary, m_CategoryHashInt, m_LabelHashInt);
  149. m_SpriteKeyInt = ConvertFloatToInt(m_SpriteKey);
  150. m_PreviousSpriteKeyInt = m_SpriteKeyInt;
  151. ResolveSpriteToSpriteRenderer();
  152. }
  153. }
  154. }
  155. }
  156. internal static float ConvertCategoryLabelHashToSpriteKey(SpriteLibrary library, int categoryHash, int labelHash)
  157. {
  158. if (library != null)
  159. {
  160. foreach(var category in library.categoryNames)
  161. {
  162. if (categoryHash == SpriteLibraryAsset.GetStringHash(category))
  163. {
  164. var entries = library.GetEntryNames(category);
  165. if (entries != null)
  166. {
  167. foreach (var entry in entries)
  168. {
  169. if (labelHash == SpriteLibraryAsset.GetStringHash(entry))
  170. {
  171. var spriteKey = SpriteLibrary.GetHashForCategoryAndEntry(category, entry);
  172. return ConvertIntToFloat(spriteKey);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. return 0;
  180. }
  181. internal Sprite GetSprite(out bool validEntry)
  182. {
  183. var lib = spriteLibrary;
  184. if (lib != null)
  185. {
  186. return lib.GetSpriteFromCategoryAndEntryHash(m_SpriteKeyInt, out validEntry);
  187. }
  188. validEntry = false;
  189. return null;
  190. }
  191. /// <summary>
  192. /// Set the Sprite in SpriteResolver to the SpriteRenderer component that is in the same GameObject.
  193. /// </summary>
  194. public void ResolveSpriteToSpriteRenderer()
  195. {
  196. m_PreviousSpriteKeyInt = m_SpriteKeyInt;
  197. bool validEntry;
  198. var sprite = GetSprite(out validEntry);
  199. var sr = spriteRenderer;
  200. if (sr != null && (sprite != null || validEntry))
  201. sr.sprite = sprite;
  202. }
  203. void OnTransformParentChanged()
  204. {
  205. ResolveSpriteToSpriteRenderer();
  206. #if UNITY_EDITOR
  207. spriteLibChanged = true;
  208. #endif
  209. }
  210. int spriteKeyInt
  211. {
  212. get { return m_SpriteKeyInt; }
  213. set
  214. {
  215. m_SpriteKeyInt = value;
  216. m_SpriteKey = ConvertIntToFloat(m_SpriteKeyInt);
  217. }
  218. }
  219. internal unsafe static int ConvertFloatToInt(float f)
  220. {
  221. float* fp = &f;
  222. int* i = (int*)fp;
  223. return *i;
  224. }
  225. internal unsafe static float ConvertIntToFloat(int f)
  226. {
  227. int* fp = &f;
  228. float* i = (float*)fp;
  229. return *i;
  230. }
  231. #if UNITY_EDITOR
  232. internal bool spriteLibChanged
  233. {
  234. get {return m_SpriteLibChanged;}
  235. set { m_SpriteLibChanged = value; }
  236. }
  237. #endif
  238. void ISerializationCallbackReceiver.OnBeforeSerialize()
  239. {
  240. }
  241. void ISerializationCallbackReceiver.OnAfterDeserialize()
  242. {
  243. #if UNITY_EDITOR
  244. onDeserializedCallback();
  245. #endif
  246. }
  247. }
  248. }