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.

SpriteEditorData.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine.U2D;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Sprites
  6. {
  7. internal class SpriteDataExt : SpriteRect
  8. {
  9. public float tessellationDetail = 0;
  10. // The following lists are to be left un-initialized.
  11. // If they never loaded or assign explicitly, we avoid writing empty list to metadata.
  12. public List<Vector2[]> spriteOutline;
  13. public List<Vertex2DMetaData> vertices;
  14. public List<int> indices;
  15. public List<Vector2Int> edges;
  16. public List<Vector2[]> spritePhysicsOutline;
  17. public List<SpriteBone> spriteBone;
  18. long m_InternalID;
  19. internal SpriteDataExt(SerializedObject so)
  20. {
  21. var ti = so.targetObject as TextureImporter;
  22. name = Path.GetFileNameWithoutExtension(ti.assetPath);
  23. alignment = (SpriteAlignment)so.FindProperty("m_Alignment").intValue;
  24. border = ti.spriteBorder;
  25. pivot = SpriteEditorUtility.GetPivotValue(alignment, ti.spritePivot);
  26. tessellationDetail = so.FindProperty("m_SpriteTessellationDetail").floatValue;
  27. int width = 0, height = 0;
  28. ti.GetWidthAndHeight(ref width, ref height);
  29. rect = new Rect(0, 0, width, height);
  30. var guidSP = so.FindProperty("m_SpriteSheet.m_SpriteID");
  31. spriteID = new GUID(guidSP.stringValue);
  32. m_InternalID = so.FindProperty("m_SpriteSheet.m_InternalID").longValue;
  33. }
  34. internal SpriteDataExt(SerializedProperty sp)
  35. {
  36. rect = sp.FindPropertyRelative("m_Rect").rectValue;
  37. border = sp.FindPropertyRelative("m_Border").vector4Value;
  38. name = sp.FindPropertyRelative("m_Name").stringValue;
  39. alignment = (SpriteAlignment)sp.FindPropertyRelative("m_Alignment").intValue;
  40. pivot = SpriteEditorUtility.GetPivotValue(alignment, sp.FindPropertyRelative("m_Pivot").vector2Value);
  41. tessellationDetail = sp.FindPropertyRelative("m_TessellationDetail").floatValue;
  42. spriteID = new GUID(sp.FindPropertyRelative("m_SpriteID").stringValue);
  43. m_InternalID = sp.FindPropertyRelative("m_InternalID").longValue;
  44. }
  45. internal SpriteDataExt(SpriteDataExt sr)
  46. {
  47. originalName = sr.originalName;
  48. name = sr.name;
  49. border = sr.border;
  50. tessellationDetail = 0;
  51. rect = sr.rect;
  52. spriteID = sr.spriteID;
  53. m_InternalID = sr.internalID;
  54. alignment = sr.alignment;
  55. pivot = sr.pivot;
  56. spriteOutline = new List<Vector2[]>();
  57. vertices = new List<Vertex2DMetaData>();
  58. indices = new List<int>();
  59. edges = new List<Vector2Int>();
  60. spritePhysicsOutline = new List<Vector2[]>();
  61. spriteBone = new List<SpriteBone>();
  62. }
  63. internal SpriteDataExt(SpriteRect sr)
  64. {
  65. originalName = sr.originalName;
  66. name = sr.name;
  67. border = sr.border;
  68. tessellationDetail = 0;
  69. rect = sr.rect;
  70. spriteID = sr.spriteID;
  71. m_InternalID = sr.spriteID.GetHashCode();
  72. alignment = sr.alignment;
  73. pivot = sr.pivot;
  74. spriteOutline = new List<Vector2[]>();
  75. vertices = new List<Vertex2DMetaData>();
  76. indices = new List<int>();
  77. edges = new List<Vector2Int>();
  78. spritePhysicsOutline = new List<Vector2[]>();
  79. spriteBone = new List<SpriteBone>();
  80. }
  81. public void Apply(SerializedObject so)
  82. {
  83. so.FindProperty("m_Alignment").intValue = (int)alignment;
  84. so.FindProperty("m_SpriteBorder").vector4Value = border;
  85. so.FindProperty("m_SpritePivot").vector2Value = pivot;
  86. so.FindProperty("m_SpriteTessellationDetail").floatValue = tessellationDetail;
  87. so.FindProperty("m_SpriteSheet.m_SpriteID").stringValue = spriteID.ToString();
  88. so.FindProperty("m_SpriteSheet.m_InternalID").longValue = m_InternalID;
  89. var sp = so.FindProperty("m_SpriteSheet");
  90. if (spriteBone != null)
  91. SpriteBoneDataTransfer.Apply(sp, spriteBone);
  92. if (spriteOutline != null)
  93. SpriteOutlineDataTransfer.Apply(sp, spriteOutline);
  94. if (spritePhysicsOutline != null)
  95. SpritePhysicsOutlineDataTransfer.Apply(sp, spritePhysicsOutline);
  96. if (vertices != null)
  97. SpriteMeshDataTransfer.Apply(sp, vertices, indices, edges);
  98. }
  99. public void Apply(SerializedProperty sp)
  100. {
  101. sp.FindPropertyRelative("m_Rect").rectValue = rect;
  102. sp.FindPropertyRelative("m_Name").stringValue = name;
  103. sp.FindPropertyRelative("m_Border").vector4Value = border;
  104. sp.FindPropertyRelative("m_Alignment").intValue = (int)alignment;
  105. sp.FindPropertyRelative("m_Pivot").vector2Value = pivot;
  106. sp.FindPropertyRelative("m_TessellationDetail").floatValue = tessellationDetail;
  107. sp.FindPropertyRelative("m_SpriteID").stringValue = spriteID.ToString();
  108. sp.FindPropertyRelative("m_InternalID").longValue = m_InternalID;
  109. if (spriteBone != null)
  110. SpriteBoneDataTransfer.Apply(sp, spriteBone);
  111. if (spriteOutline != null)
  112. SpriteOutlineDataTransfer.Apply(sp, spriteOutline);
  113. if (spritePhysicsOutline != null)
  114. SpritePhysicsOutlineDataTransfer.Apply(sp, spritePhysicsOutline);
  115. if (vertices != null)
  116. SpriteMeshDataTransfer.Apply(sp, vertices, indices, edges);
  117. }
  118. public void CopyFromSpriteRect(SpriteRect spriteRect)
  119. {
  120. alignment = spriteRect.alignment;
  121. border = spriteRect.border;
  122. name = spriteRect.name;
  123. pivot = spriteRect.pivot;
  124. rect = spriteRect.rect;
  125. spriteID = spriteRect.spriteID;
  126. }
  127. public long internalID
  128. {
  129. get
  130. {
  131. if (m_InternalID == 0)
  132. m_InternalID = spriteID.GetHashCode();
  133. return m_InternalID;
  134. }
  135. set => m_InternalID = value;
  136. }
  137. }
  138. internal class SpriteNameFileIdPairExt : SpriteNameFileIdPair
  139. {
  140. private const string k_NameField = "first";
  141. private const string k_FileIdField = "second";
  142. long m_InternalId;
  143. public SpriteNameFileIdPairExt(string name, GUID guid, long internalId)
  144. : base(name, guid)
  145. {
  146. m_InternalId = internalId;
  147. }
  148. public long internalID
  149. {
  150. get
  151. {
  152. if (m_InternalId == 0L)
  153. m_InternalId = GetFileGUID().GetHashCode();
  154. return m_InternalId;
  155. }
  156. set => m_InternalId = value;
  157. }
  158. public static SpriteNameFileIdPairExt GetValue(SerializedProperty sp)
  159. {
  160. var name = sp.FindPropertyRelative(k_NameField).stringValue;
  161. var id = sp.FindPropertyRelative(k_FileIdField).longValue;
  162. return new SpriteNameFileIdPairExt(name, GUID.Generate(), id);
  163. }
  164. public void Apply(SerializedProperty sp)
  165. {
  166. sp.FindPropertyRelative(k_NameField).stringValue = name;
  167. sp.FindPropertyRelative(k_FileIdField).longValue = internalID;
  168. }
  169. }
  170. }