SpriteRect.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace UnityEditor
  5. {
  6. /// <summary>Abstract class that is used by systems to encapsulate Sprite data representation. Currently this is used by Sprite Editor Window.</summary>
  7. [Serializable]
  8. public class SpriteRect
  9. {
  10. [SerializeField]
  11. string m_Name;
  12. [SerializeField]
  13. string m_OriginalName;
  14. [SerializeField]
  15. Vector2 m_Pivot;
  16. [SerializeField]
  17. SpriteAlignment m_Alignment;
  18. [SerializeField]
  19. Vector4 m_Border;
  20. [SerializeField]
  21. Rect m_Rect;
  22. [SerializeField]
  23. string m_SpriteID;
  24. [SerializeField]
  25. internal long m_InternalID;
  26. internal bool m_RegisterInternalID;
  27. GUID m_GUID;
  28. // <summary>The name of the Sprite data.</summary>
  29. public string name
  30. {
  31. get { return m_Name; }
  32. set { m_Name = value; }
  33. }
  34. // <summary>Vector2value representing the pivot for the Sprite data.</summary>
  35. public Vector2 pivot
  36. {
  37. get { return m_Pivot; }
  38. set { m_Pivot = value; }
  39. }
  40. /// <summary>SpriteAlignment that represents the pivot value for the Sprite data.</summary>
  41. public SpriteAlignment alignment
  42. {
  43. get { return m_Alignment; }
  44. set { m_Alignment = value; }
  45. }
  46. /// <summary>Returns a Vector4 that represents the border of the Sprite data.</summary>
  47. public Vector4 border
  48. {
  49. get { return m_Border; }
  50. set { m_Border = value; }
  51. }
  52. // <summary>Rect value that represents the position and size of the Sprite data.</summary>
  53. public Rect rect
  54. {
  55. get { return m_Rect; }
  56. set { m_Rect = value; }
  57. }
  58. internal string originalName
  59. {
  60. get
  61. {
  62. if (m_OriginalName == null)
  63. {
  64. m_OriginalName = name;
  65. }
  66. return m_OriginalName;
  67. }
  68. set { m_OriginalName = value; }
  69. }
  70. // <summary>GUID to uniquely identify the SpriteRect data. This will be populated to Sprite.spriteID to identify the SpriteRect used to generate the Sprite.</summary>
  71. public GUID spriteID
  72. {
  73. get
  74. {
  75. ValidateGUID();
  76. return m_GUID;
  77. }
  78. set
  79. {
  80. m_GUID = value;
  81. m_SpriteID = m_GUID.ToString();
  82. ValidateGUID();
  83. }
  84. }
  85. private void ValidateGUID()
  86. {
  87. if (m_GUID.Empty())
  88. {
  89. // We can't use ISerializationCallbackReceiver because we will hit into Script serialization errors
  90. m_GUID = new GUID(m_SpriteID);
  91. if (m_GUID.Empty())
  92. {
  93. m_GUID = GUID.Generate();
  94. m_SpriteID = m_GUID.ToString();
  95. }
  96. }
  97. }
  98. /// <summary>Helper method to get SpriteRect.spriteID from a SerializedProperty.</summary>
  99. /// <param name="sp">The SerializedProperty to acquire from.</param>
  100. /// <returns>GUID for the SpriteRect.</returns>
  101. public static GUID GetSpriteIDFromSerializedProperty(SerializedProperty sp)
  102. {
  103. return new GUID(sp.FindPropertyRelative("m_SpriteID").stringValue);
  104. }
  105. internal long internalID
  106. {
  107. get
  108. {
  109. return m_InternalID;
  110. }
  111. set
  112. {
  113. m_InternalID = value;
  114. }
  115. }
  116. }
  117. internal class SpriteRectCache : ScriptableObject
  118. {
  119. [SerializeField]
  120. private List<SpriteRect> m_Rects;
  121. public int Count
  122. {
  123. get { return m_Rects != null ? m_Rects.Count : 0; }
  124. }
  125. public SpriteRect RectAt(int i)
  126. {
  127. return i >= Count || i < 0 ? null : m_Rects[i];
  128. }
  129. public void AddRect(SpriteRect r)
  130. {
  131. if (m_Rects != null)
  132. m_Rects.Add(r);
  133. }
  134. public void RemoveRect(SpriteRect r)
  135. {
  136. if (m_Rects != null)
  137. m_Rects.RemoveAll(x => x.spriteID == r.spriteID);
  138. }
  139. public void ClearAll()
  140. {
  141. if (m_Rects != null)
  142. m_Rects.Clear();
  143. }
  144. public int GetIndex(SpriteRect spriteRect)
  145. {
  146. if (m_Rects != null && spriteRect != null)
  147. return m_Rects.FindIndex(p => p.spriteID == spriteRect.spriteID);
  148. return -1;
  149. }
  150. public bool Contains(SpriteRect spriteRect)
  151. {
  152. if (m_Rects != null && spriteRect != null)
  153. return m_Rects.Find(x => x.spriteID == spriteRect.spriteID) != null;
  154. return false;
  155. }
  156. void OnEnable()
  157. {
  158. if (m_Rects == null)
  159. m_Rects = new List<SpriteRect>();
  160. }
  161. }
  162. }