No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpriteRect.cs 5.0KB

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