Skip to content

Commit e71df3e

Browse files
authored
Merge pull request #64 from sr4dev/feature/fix_parallel_import_issue
Fix Parallel import issue.
2 parents 3a0ea87 + ee11e0c commit e71df3e

4 files changed

Lines changed: 54 additions & 51 deletions

File tree

Assets/SpriteAssist/Editor/Inspector/SpriteProcessor.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -466,27 +466,27 @@ public void OnInspectorGUI(bool disableBaseGUI)
466466
EditorGUILayout.Space();
467467

468468
using (new EditorGUILayout.HorizontalScope())
469-
using (new EditorGUI.DisabledScope(!_isDataChanged))
470-
{
471-
GUILayout.FlexibleSpace();
472-
473-
if (GUILayout.Button("Revert", GUILayout.Width(50)))
469+
using (new EditorGUI.DisabledScope(!_isDataChanged))
474470
{
475-
Revert();
476-
}
471+
GUILayout.FlexibleSpace();
477472

478-
if (GUILayout.Button("Apply", GUILayout.Width(50)))
479-
{
480-
if (_meshPrefab != _mainImportData.MeshPrefab)
473+
if (GUILayout.Button("Revert", GUILayout.Width(50)))
481474
{
482-
Apply(true, _meshPrefab == null, false, (GameObject)_meshPrefab);
475+
Revert();
483476
}
484-
else
477+
478+
if (GUILayout.Button("Apply", GUILayout.Width(50)))
485479
{
486-
Apply();
480+
if (_meshPrefab != _mainImportData.MeshPrefab)
481+
{
482+
Apply(true, _meshPrefab == null, false, (GameObject)_meshPrefab);
483+
}
484+
else
485+
{
486+
Apply();
487+
}
487488
}
488489
}
489-
}
490490

491491
if (!_mainImportData.IsTightMesh)
492492
{
@@ -513,7 +513,7 @@ public bool OnPreviewGUI(Rect rect, Sprite baseSprite, Sprite dummySprite, Textu
513513

514514
//for multiple preview
515515
bool hasMultipleTargets = Selection.objects.Length > 1;
516-
516+
517517
if (_isPreviewChanged || _preview.Rect != rect || hasMultipleTargets || isForce)
518518
{
519519
_preview.Update(rect, baseSprite, dummySprite, textureInfo, _configData);
@@ -579,8 +579,12 @@ private void Apply(bool withMeshPrefabProcess = false, bool hasMeshPrefab = fals
579579

580580
Undo.RegisterCompleteObjectUndo(_targets, "SpriteAssist Texture");
581581

582+
_isDataChanged = false;
583+
_meshPrefab = _mainImportData.MeshPrefab;
582584
_originalUserData = JsonUtility.ToJson(_configData);
583585

586+
var oldSelection = Selection.objects;
587+
584588
foreach (var selectedTarget in _targets)
585589
{
586590
if (SpriteImportData.TryGetSpriteImportData(selectedTarget, out var importData))
@@ -620,10 +624,9 @@ private void Apply(bool withMeshPrefabProcess = false, bool hasMeshPrefab = fals
620624

621625
AssetDatabase.SaveAssets();
622626

623-
_isDataChanged = false;
624-
_meshPrefab = _mainImportData.MeshPrefab;
627+
Selection.objects = oldSelection;
625628
}
626-
629+
627630
private void SetMeshPrefabContainer(SpriteImportData importData, bool removeOldMeshPrefab, GameObject attachedMeshPrefab)
628631
{
629632
importData.RemoveExternalPrefab(removeOldMeshPrefab);

Assets/SpriteAssist/Editor/MeshCreator/OpaqueEdgeGridMeshCreator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ private Mesh GetCombinedMesh(Sprite baseSprite, Sprite dummySprite, TextureInfo
4242
{
4343
dummySprite.GetVertexAndTriangle2D(data, out var vertices, out var triangles, MeshRenderType.OpaqueWithoutTightGrid);
4444
if (applyPixelPerUnitScale) vertices = MeshUtil.GetScaledVertices(vertices, textureInfo, isClamped: true);
45-
Mesh opaqueMesh = MeshUtil.Create(vertices.ToVector3(), triangles.ToInt(), textureInfo, false);
45+
Mesh opaqueMesh = MeshUtil.Update(null, vertices.ToVector3(), triangles.ToInt(), textureInfo, false);
4646

4747
dummySprite.GetVertexAndTriangle2D(data, out var verticesGrid, out var trianglesGrid, MeshRenderType.TightGrid);
4848
if (applyPixelPerUnitScale) verticesGrid = MeshUtil.GetScaledVertices(verticesGrid, textureInfo, isClamped: true);
49-
Mesh gridMesh = MeshUtil.Create(verticesGrid.ToVector3(), trianglesGrid.ToInt(), textureInfo, false);
49+
Mesh gridMesh = MeshUtil.Update(null, verticesGrid.ToVector3(), trianglesGrid.ToInt(), textureInfo, false);
5050

5151
var combinedMesh = new Mesh();
5252

@@ -59,4 +59,4 @@ private Mesh GetCombinedMesh(Sprite baseSprite, Sprite dummySprite, TextureInfo
5959
return combinedMesh;
6060
}
6161
}
62-
}
62+
}

Assets/SpriteAssist/Editor/Util/MeshUtil.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static int[] ToInt(this ushort[] triangles2D)
4848
{
4949
return Array.ConvertAll(triangles2D, i => (int)i);
5050
}
51-
51+
5252
public static ushort[] ToUShort(this int[] triangles3D)
5353
{
5454
return Array.ConvertAll(triangles3D, i => (ushort)i);
@@ -63,7 +63,7 @@ public static string GetAreaInfo(Vector2[] vertices2D, ushort[] triangles2D, Tex
6363
Vector2 a = vertices2D[triangles2D[i]];
6464
Vector2 b = vertices2D[triangles2D[i + 1]];
6565
Vector2 c = vertices2D[triangles2D[i + 2]];
66-
area += (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y));
66+
area += a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y);
6767
}
6868

6969
area *= 0.5f * textureInfo.pixelPerUnit * textureInfo.pixelPerUnit;
@@ -73,14 +73,7 @@ public static string GetAreaInfo(Vector2[] vertices2D, ushort[] triangles2D, Tex
7373
return $"{vertices2D.Length} verts, {triangles2D.Length / 3} tris, {meshAreaRatio:F2}% overdraw";
7474
}
7575

76-
public static Mesh Create(Vector3[] v, int[] t, TextureInfo textureInfo, bool splitVertices)
77-
{
78-
Mesh mesh = new Mesh();
79-
Update(mesh, v, t, textureInfo, splitVertices);
80-
return mesh;
81-
}
82-
83-
public static void Update(Mesh mesh, Vector3[] v, int[] t, TextureInfo textureInfo, bool splitVertices)
76+
public static Mesh Update(Mesh mesh, Vector3[] v, int[] t, TextureInfo textureInfo, bool splitVertices)
8477
{
8578
Vector2[] uv = new Vector2[v.Length];
8679

@@ -91,6 +84,11 @@ public static void Update(Mesh mesh, Vector3[] v, int[] t, TextureInfo textureIn
9184
uv[i].y /= textureInfo.rect.size.y;
9285
}
9386

87+
if (mesh == null)
88+
{
89+
mesh = new Mesh();
90+
}
91+
9492
mesh.Clear();
9593
mesh.SetVertices(v);
9694
mesh.SetUVs(0, uv);
@@ -102,6 +100,8 @@ public static void Update(Mesh mesh, Vector3[] v, int[] t, TextureInfo textureIn
102100
{
103101
mesh.SplitVertices();
104102
}
103+
104+
return mesh;
105105
}
106106

107107
//reference 'https://github.com/sr4dev/Unity-SpriteAssist/issues/38 by MateuszRe'
@@ -129,7 +129,7 @@ public static void SplitVertices(this Mesh mesh)
129129
{
130130
var triangles = mesh.GetTriangles(s);
131131
int tCnt = triangles.Length;
132-
132+
133133
for (int t = 0; t < tCnt; t++)
134134
{
135135
int index = triangles[t];
@@ -138,8 +138,10 @@ public static void SplitVertices(this Mesh mesh)
138138
vertices.Add(vertices[index]);
139139
if (hasUvs)
140140
uvs.Add(uvs[index]);
141+
141142
if (hasColors)
142143
colors.Add(colors[index]);
144+
143145
if (hasTangents)
144146
tangents.Add(tangents[index]);
145147

@@ -158,8 +160,10 @@ public static void SplitVertices(this Mesh mesh)
158160

159161
if (hasUvs)
160162
mesh.SetUVs(0, uvs);
163+
161164
if (hasColors)
162165
mesh.SetColors(colors);
166+
163167
if (hasTangents)
164168
mesh.SetTangents(tangents);
165169

@@ -228,9 +232,9 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
228232
{
229233
tempIndex = tempList[j];
230234
if (Vector3.SqrMagnitude(vertBuffer[tempIndex] - vertices[i]) < threshold
231-
&& (!hasUVs || uvs[i] == uvsBuffer[tempIndex])
232-
&& (!hasColors || colors[i] == colorBuffer[tempIndex])
233-
&& (ignoreNormals || normals[i] == normalBuffer[tempIndex]))
235+
&& (!hasUVs || uvs[i] == uvsBuffer[tempIndex])
236+
&& (!hasColors || colors[i] == colorBuffer[tempIndex])
237+
&& (ignoreNormals || normals[i] == normalBuffer[tempIndex]))
234238
{
235239
indexBuffer.Add(tempList[j]);
236240
goto skip;
@@ -240,16 +244,18 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
240244
vertBuffer.Add(vertices[i]);
241245
if (hasColors)
242246
colorBuffer.Add(colors[i]);
247+
243248
if (hasUVs)
244249
uvsBuffer.Add(uvs[i]);
250+
245251
if (!ignoreNormals)
246252
normalBuffer.Add(normals[i]);
247253

248254
tempList.Add(newSize);
249255
indexBuffer.Add(newSize);
250256
newSize++;
251257

252-
skip:;
258+
skip: ;
253259
}
254260

255261
for (int i = 0; i < triangles.Length; i++)
@@ -263,6 +269,7 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
263269

264270
if (hasColors)
265271
mesh.SetColors(colorBuffer);
272+
266273
if (hasUVs)
267274
mesh.SetUVs(0, uvsBuffer);
268275

@@ -273,6 +280,5 @@ public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucke
273280

274281
if (printResults) Debug.Log("Reduced " + mesh.name + " from " + vertices.Length + " to " + vertBuffer.Count);
275282
}
276-
277283
}
278-
}
284+
}

Assets/SpriteAssist/Editor/Util/PrefabUtil.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Object = UnityEngine.Object;
66
#if UNITY_2021_2_OR_NEWER
77
using UnityEditor.SceneManagement;
8+
89
#else
910
using UnityEditor.Experimental.SceneManagement;
1011
#endif
@@ -48,8 +49,7 @@ public static GameObject CreateMeshPrefab(TextureInfo textureInfo, bool hasSubOb
4849

4950
string countText = count > 1 ? $"_{count}" : string.Empty;
5051
path = Path.Combine(currentDirectory, $"{textureFileName}{countText}.prefab");
51-
}
52-
while (File.Exists(path));
52+
} while (File.Exists(path));
5353

5454
if (!string.IsNullOrEmpty(relativePath))
5555
{
@@ -146,12 +146,8 @@ public static void AddComponentsAssets(Sprite sprite, GameObject prefab, Vector3
146146
}
147147

148148
//create new mesh
149-
Mesh mesh = new Mesh()
150-
{
151-
name = renderType,
152-
};
153-
154-
MeshUtil.Update(mesh, v, t, textureInfo, spriteConfigData.isCorrectNormal);
149+
Mesh mesh = MeshUtil.Update(null, v, t, textureInfo, spriteConfigData.isCorrectNormal);
150+
mesh.name = renderType;
155151
meshFilter.mesh = mesh;
156152

157153
//create new material
@@ -170,9 +166,7 @@ public static void AddComponentsAssets(Sprite sprite, GameObject prefab, Vector3
170166
public static void UpdateMeshFiltersMesh(GameObject prefab, Vector3[] v, int[] t, TextureInfo textureInfo, bool splitVertices)
171167
{
172168
MeshFilter meshFilter = prefab.GetComponent<MeshFilter>();
173-
Mesh mesh = meshFilter.sharedMesh;
174-
MeshUtil.Update(mesh, v, t, textureInfo, splitVertices);
175-
meshFilter.sharedMesh = mesh;
169+
meshFilter.sharedMesh = MeshUtil.Update(meshFilter.sharedMesh, v, t, textureInfo, splitVertices);
176170
}
177171

178172
public static void CleanUpSubAssets(GameObject prefab)
@@ -211,7 +205,7 @@ public static bool TryGetMutableInstanceInHierarchy(Object target, out GameObjec
211205
public static bool TryGetSpriteRendererWithSprite(GameObject gameObject, out SpriteRenderer spriteRenderer)
212206
{
213207
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
214-
208+
215209
if (spriteRenderer != null && spriteRenderer.sprite != null)
216210
{
217211
return true;
@@ -249,4 +243,4 @@ public static bool TryRename(string spriteAssetPath, GameObject meshPrefab)
249243
return false;
250244
}
251245
}
252-
}
246+
}

0 commit comments

Comments
 (0)