SpriteEditorData.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. internal SpriteDataExt(SerializedObject so)
  19. {
  20. var ti = so.targetObject as TextureImporter;
  21. name = Path.GetFileNameWithoutExtension(ti.assetPath);
  22. alignment = (SpriteAlignment)so.FindProperty("m_Alignment").intValue;
  23. border = ti.spriteBorder;
  24. pivot = SpriteEditorUtility.GetPivotValue(alignment, ti.spritePivot);
  25. tessellationDetail = so.FindProperty("m_SpriteTessellationDetail").floatValue;
  26. int width = 0, height = 0;
  27. ti.GetWidthAndHeight(ref width, ref height);
  28. rect = new Rect(0, 0, width, height);
  29. var guidSP = so.FindProperty("m_SpriteSheet.m_SpriteID");
  30. spriteID = new GUID(guidSP.stringValue);
  31. internalID = so.FindProperty("m_SpriteSheet.m_InternalID").longValue;
  32. }
  33. internal SpriteDataExt(SerializedProperty sp)
  34. {
  35. rect = sp.FindPropertyRelative("m_Rect").rectValue;
  36. border = sp.FindPropertyRelative("m_Border").vector4Value;
  37. name = sp.FindPropertyRelative("m_Name").stringValue;
  38. alignment = (SpriteAlignment)sp.FindPropertyRelative("m_Alignment").intValue;
  39. pivot = SpriteEditorUtility.GetPivotValue(alignment, sp.FindPropertyRelative("m_Pivot").vector2Value);
  40. tessellationDetail = sp.FindPropertyRelative("m_TessellationDetail").floatValue;
  41. spriteID = new GUID(sp.FindPropertyRelative("m_SpriteID").stringValue);
  42. internalID = sp.FindPropertyRelative("m_InternalID").longValue;
  43. }
  44. internal SpriteDataExt(SpriteRect sr)
  45. {
  46. originalName = sr.originalName;
  47. name = sr.name;
  48. border = sr.border;
  49. tessellationDetail = 0;
  50. rect = sr.rect;
  51. spriteID = sr.spriteID;
  52. internalID = sr.internalID;
  53. alignment = sr.alignment;
  54. pivot = sr.pivot;
  55. spriteOutline = new List<Vector2[]>();
  56. vertices = new List<Vertex2DMetaData>();
  57. indices = new List<int>();
  58. edges = new List<Vector2Int>();
  59. spritePhysicsOutline = new List<Vector2[]>();
  60. spriteBone = new List<SpriteBone>();
  61. }
  62. public void Apply(SerializedObject so)
  63. {
  64. so.FindProperty("m_Alignment").intValue = (int)alignment;
  65. so.FindProperty("m_SpriteBorder").vector4Value = border;
  66. so.FindProperty("m_SpritePivot").vector2Value = pivot;
  67. so.FindProperty("m_SpriteTessellationDetail").floatValue = tessellationDetail;
  68. so.FindProperty("m_SpriteSheet.m_SpriteID").stringValue = spriteID.ToString();
  69. so.FindProperty("m_SpriteSheet.m_InternalID").longValue = internalID;
  70. var sp = so.FindProperty("m_SpriteSheet");
  71. if (spriteBone != null)
  72. SpriteBoneDataTransfer.Apply(sp, spriteBone);
  73. if (spriteOutline != null)
  74. SpriteOutlineDataTransfer.Apply(sp, spriteOutline);
  75. if (spritePhysicsOutline != null)
  76. SpritePhysicsOutlineDataTransfer.Apply(sp, spritePhysicsOutline);
  77. if (vertices != null)
  78. SpriteMeshDataTransfer.Apply(sp, vertices, indices, edges);
  79. }
  80. public void Apply(SerializedProperty sp)
  81. {
  82. sp.FindPropertyRelative("m_Rect").rectValue = rect;
  83. sp.FindPropertyRelative("m_Name").stringValue = name;
  84. sp.FindPropertyRelative("m_Border").vector4Value = border;
  85. sp.FindPropertyRelative("m_Alignment").intValue = (int)alignment;
  86. sp.FindPropertyRelative("m_Pivot").vector2Value = pivot;
  87. sp.FindPropertyRelative("m_TessellationDetail").floatValue = tessellationDetail;
  88. sp.FindPropertyRelative("m_SpriteID").stringValue = spriteID.ToString();
  89. sp.FindPropertyRelative("m_InternalID").longValue = internalID;
  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 CopyFromSpriteRect(SpriteRect spriteRect)
  100. {
  101. alignment = spriteRect.alignment;
  102. border = spriteRect.border;
  103. name = spriteRect.name;
  104. pivot = spriteRect.pivot;
  105. rect = spriteRect.rect;
  106. spriteID = spriteRect.spriteID;
  107. internalID = spriteRect.internalID;
  108. }
  109. }
  110. }