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