SpriteResolver.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. namespace UnityEngine.Experimental.U2D.Animation
  3. {
  4. /// <summary>
  5. /// Updates a SpriteRenderer's Sprite reference on the Category and Label value it is set
  6. /// </summary>
  7. /// <Description>
  8. /// By setting the SpriteResolver's Category and Label value, it will request for a Sprite from
  9. /// a SpriteLibrary Component the Sprite that is registered for the Category and Label.
  10. /// If a SpriteRenderer is present in the same GameObject, the SpriteResolver will update the
  11. /// SpriteRenderer's Sprite reference to the corresponding Sprite.
  12. /// </Description>
  13. [ExecuteInEditMode]
  14. [DisallowMultipleComponent]
  15. [AddComponentMenu("2D Animation/Sprite Resolver (Experimental)")]
  16. [DefaultExecutionOrder(-2)]
  17. [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@latest/index.html?subfolder=/manual/SLAsset.html%23sprite-resolver-component")]
  18. public class SpriteResolver : MonoBehaviour
  19. {
  20. // These are for animation
  21. [SerializeField]
  22. private float m_CategoryHash;
  23. [SerializeField]
  24. private float m_labelHash;
  25. // For comparing hash values
  26. private int m_CategoryHashInt;
  27. private int m_LabelHashInt;
  28. // For OnUpdate during animation playback
  29. private int m_PreviousCategoryHash;
  30. private int m_PreviouslabelHash;
  31. #if UNITY_EDITOR
  32. bool m_SpriteLibChanged;
  33. #endif
  34. void OnEnable()
  35. {
  36. m_CategoryHashInt = ConvertFloatToInt(m_CategoryHash);
  37. m_PreviousCategoryHash = m_CategoryHashInt;
  38. m_LabelHashInt = ConvertFloatToInt(m_labelHash);
  39. m_PreviouslabelHash = m_LabelHashInt;
  40. ResolveSpriteToSpriteRenderer();
  41. }
  42. SpriteRenderer spriteRenderer
  43. {
  44. get { return GetComponent<SpriteRenderer>(); }
  45. }
  46. /// <summary>
  47. /// Set the Category and label to use
  48. /// </summary>
  49. /// <param name="category">Category to use</param>
  50. /// <param name="label">Label to use</param>
  51. public void SetCategoryAndLabel(string category, string label)
  52. {
  53. categoryHashInt = SpriteLibraryAsset.GetStringHash(category);
  54. m_PreviousCategoryHash = categoryHashInt;
  55. labelHashInt = SpriteLibraryAsset.GetStringHash(label);
  56. m_PreviouslabelHash = categoryHashInt;
  57. ResolveSpriteToSpriteRenderer();
  58. }
  59. /// <summary>
  60. /// Get the Category set for the SpriteResolver
  61. /// </summary>
  62. /// <returns>The Category's name</returns>
  63. public string GetCategory()
  64. {
  65. var returnString = "";
  66. var sl = spriteLibrary;
  67. if (sl)
  68. returnString = sl.GetCategoryNameFromHash(categoryHashInt);
  69. return returnString;
  70. }
  71. /// <summary>
  72. /// Get the Label set for the SpriteResolver
  73. /// </summary>
  74. /// <returns>The Label's name</returns>
  75. public string GetLabel()
  76. {
  77. var returnString = "";
  78. var sl = spriteLibrary;
  79. if (sl)
  80. returnString = sl.GetLabelNameFromHash(categoryHashInt, labelHashInt);
  81. return returnString;
  82. }
  83. /// <summary>
  84. /// Property to get the SpriteLibrary the SpriteResolver is resolving from
  85. /// </summary>
  86. public SpriteLibrary spriteLibrary => gameObject.GetComponentInParent<SpriteLibrary>(true);
  87. void LateUpdate()
  88. {
  89. m_CategoryHashInt = ConvertFloatToInt(m_CategoryHash);
  90. m_LabelHashInt = ConvertFloatToInt(m_labelHash);
  91. if (m_LabelHashInt != m_PreviouslabelHash || m_CategoryHashInt != m_PreviousCategoryHash)
  92. {
  93. m_PreviousCategoryHash = m_CategoryHashInt;
  94. m_PreviouslabelHash = m_LabelHashInt;
  95. ResolveSpriteToSpriteRenderer();
  96. }
  97. }
  98. internal Sprite GetSprite(out bool validEntry)
  99. {
  100. var lib = spriteLibrary;
  101. if (lib != null)
  102. {
  103. return lib.GetSprite(m_CategoryHashInt, m_LabelHashInt, out validEntry);
  104. }
  105. validEntry = false;
  106. return null;
  107. }
  108. /// <summary>
  109. /// Set the Sprite in SpriteResolver to the SpriteRenderer component that is in the same GameObject.
  110. /// </summary>
  111. public void ResolveSpriteToSpriteRenderer()
  112. {
  113. m_PreviousCategoryHash = m_CategoryHashInt;
  114. m_PreviouslabelHash = m_LabelHashInt;
  115. bool validEntry;
  116. var sprite = GetSprite(out validEntry);
  117. var sr = spriteRenderer;
  118. if (sr != null && (sprite != null || validEntry))
  119. sr.sprite = sprite;
  120. }
  121. void OnTransformParentChanged()
  122. {
  123. ResolveSpriteToSpriteRenderer();
  124. #if UNITY_EDITOR
  125. spriteLibChanged = true;
  126. #endif
  127. }
  128. int categoryHashInt
  129. {
  130. get { return m_CategoryHashInt; }
  131. set
  132. {
  133. m_CategoryHashInt = value;
  134. m_CategoryHash = ConvertIntToFloat(m_CategoryHashInt);
  135. }
  136. }
  137. int labelHashInt
  138. {
  139. get { return m_LabelHashInt; }
  140. set
  141. {
  142. m_LabelHashInt = value;
  143. m_labelHash = ConvertIntToFloat(m_LabelHashInt);
  144. }
  145. }
  146. internal unsafe static int ConvertFloatToInt(float f)
  147. {
  148. float* fp = &f;
  149. int* i = (int*)fp;
  150. return *i;
  151. }
  152. internal unsafe static float ConvertIntToFloat(int f)
  153. {
  154. int* fp = &f;
  155. float* i = (float*)fp;
  156. return *i;
  157. }
  158. #if UNITY_EDITOR
  159. internal bool spriteLibChanged
  160. {
  161. get {return m_SpriteLibChanged;}
  162. set { m_SpriteLibChanged = value; }
  163. }
  164. #endif
  165. }
  166. }