暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpriteEditorData.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. customData = so.FindProperty("m_SpriteSheet.m_CustomData").stringValue;
  34. }
  35. internal SpriteDataExt(SerializedProperty sp)
  36. {
  37. rect = sp.FindPropertyRelative("m_Rect").rectValue;
  38. border = sp.FindPropertyRelative("m_Border").vector4Value;
  39. name = sp.FindPropertyRelative("m_Name").stringValue;
  40. alignment = (SpriteAlignment)sp.FindPropertyRelative("m_Alignment").intValue;
  41. pivot = SpriteEditorUtility.GetPivotValue(alignment, sp.FindPropertyRelative("m_Pivot").vector2Value);
  42. tessellationDetail = sp.FindPropertyRelative("m_TessellationDetail").floatValue;
  43. spriteID = new GUID(sp.FindPropertyRelative("m_SpriteID").stringValue);
  44. m_InternalID = sp.FindPropertyRelative("m_InternalID").longValue;
  45. customData = sp.FindPropertyRelative("m_CustomData").stringValue;
  46. }
  47. internal SpriteDataExt(SpriteDataExt sr)
  48. {
  49. originalName = sr.originalName;
  50. name = sr.name;
  51. border = sr.border;
  52. tessellationDetail = 0;
  53. rect = sr.rect;
  54. spriteID = sr.spriteID;
  55. m_InternalID = sr.internalID;
  56. alignment = sr.alignment;
  57. pivot = sr.pivot;
  58. spriteOutline = new List<Vector2[]>();
  59. vertices = new List<Vertex2DMetaData>();
  60. indices = new List<int>();
  61. edges = new List<Vector2Int>();
  62. spritePhysicsOutline = new List<Vector2[]>();
  63. spriteBone = new List<SpriteBone>();
  64. customData = sr.customData;
  65. }
  66. internal SpriteDataExt(SpriteRect sr)
  67. {
  68. originalName = sr.originalName;
  69. name = sr.name;
  70. border = sr.border;
  71. tessellationDetail = 0;
  72. rect = sr.rect;
  73. spriteID = sr.spriteID;
  74. m_InternalID = sr.spriteID.GetHashCode();
  75. alignment = sr.alignment;
  76. pivot = sr.pivot;
  77. customData = sr.customData;
  78. spriteOutline = new List<Vector2[]>();
  79. vertices = new List<Vertex2DMetaData>();
  80. indices = new List<int>();
  81. edges = new List<Vector2Int>();
  82. spritePhysicsOutline = new List<Vector2[]>();
  83. spriteBone = new List<SpriteBone>();
  84. }
  85. public void Apply(SerializedObject so)
  86. {
  87. so.FindProperty("m_Alignment").intValue = (int)alignment;
  88. so.FindProperty("m_SpriteBorder").vector4Value = border;
  89. so.FindProperty("m_SpritePivot").vector2Value = pivot;
  90. so.FindProperty("m_SpriteTessellationDetail").floatValue = tessellationDetail;
  91. so.FindProperty("m_SpriteSheet.m_SpriteID").stringValue = spriteID.ToString();
  92. so.FindProperty("m_SpriteSheet.m_InternalID").longValue = m_InternalID;
  93. so.FindProperty("m_SpriteSheet.m_CustomData").stringValue = customData;
  94. var sp = so.FindProperty("m_SpriteSheet");
  95. if (spriteBone != null)
  96. SpriteBoneDataTransfer.Apply(sp, spriteBone);
  97. if (spriteOutline != null)
  98. SpriteOutlineDataTransfer.Apply(sp, spriteOutline);
  99. if (spritePhysicsOutline != null)
  100. SpritePhysicsOutlineDataTransfer.Apply(sp, spritePhysicsOutline);
  101. if (vertices != null)
  102. SpriteMeshDataTransfer.Apply(sp, vertices, indices, edges);
  103. }
  104. public void Apply(SerializedProperty sp)
  105. {
  106. sp.FindPropertyRelative("m_Rect").rectValue = rect;
  107. sp.FindPropertyRelative("m_Name").stringValue = name;
  108. sp.FindPropertyRelative("m_Border").vector4Value = border;
  109. sp.FindPropertyRelative("m_Alignment").intValue = (int)alignment;
  110. sp.FindPropertyRelative("m_Pivot").vector2Value = pivot;
  111. sp.FindPropertyRelative("m_TessellationDetail").floatValue = tessellationDetail;
  112. sp.FindPropertyRelative("m_SpriteID").stringValue = spriteID.ToString();
  113. sp.FindPropertyRelative("m_InternalID").longValue = m_InternalID;
  114. sp.FindPropertyRelative("m_CustomData").stringValue = customData;
  115. if (spriteBone != null)
  116. SpriteBoneDataTransfer.Apply(sp, spriteBone);
  117. if (spriteOutline != null)
  118. SpriteOutlineDataTransfer.Apply(sp, spriteOutline);
  119. if (spritePhysicsOutline != null)
  120. SpritePhysicsOutlineDataTransfer.Apply(sp, spritePhysicsOutline);
  121. if (vertices != null)
  122. SpriteMeshDataTransfer.Apply(sp, vertices, indices, edges);
  123. }
  124. public void CopyFromSpriteRect(SpriteRect spriteRect)
  125. {
  126. alignment = spriteRect.alignment;
  127. border = spriteRect.border;
  128. name = spriteRect.name;
  129. pivot = spriteRect.pivot;
  130. rect = spriteRect.rect;
  131. spriteID = spriteRect.spriteID;
  132. customData = spriteRect.customData;
  133. }
  134. public long internalID
  135. {
  136. get
  137. {
  138. if (m_InternalID == 0)
  139. m_InternalID = spriteID.GetHashCode();
  140. return m_InternalID;
  141. }
  142. set => m_InternalID = value;
  143. }
  144. }
  145. internal class SpriteNameFileIdPairExt : SpriteNameFileIdPair
  146. {
  147. private const string k_NameField = "first";
  148. private const string k_FileIdField = "second";
  149. long m_InternalId;
  150. public SpriteNameFileIdPairExt(string name, GUID guid, long internalId)
  151. : base(name, guid)
  152. {
  153. m_InternalId = internalId;
  154. }
  155. public long internalID
  156. {
  157. get
  158. {
  159. if (m_InternalId == 0L)
  160. m_InternalId = GetFileGUID().GetHashCode();
  161. return m_InternalId;
  162. }
  163. set => m_InternalId = value;
  164. }
  165. public static SpriteNameFileIdPairExt GetValue(SerializedProperty sp)
  166. {
  167. var name = sp.FindPropertyRelative(k_NameField).stringValue;
  168. var id = sp.FindPropertyRelative(k_FileIdField).longValue;
  169. return new SpriteNameFileIdPairExt(name, GUID.Generate(), id);
  170. }
  171. public void Apply(SerializedProperty sp)
  172. {
  173. sp.FindPropertyRelative(k_NameField).stringValue = name;
  174. sp.FindPropertyRelative(k_FileIdField).longValue = internalID;
  175. }
  176. }
  177. }