com.wallstop-studios.unity-helpers 2.0.0-rc73.6 → 2.0.0-rc73.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,7 +140,7 @@
140
140
  return;
141
141
  }
142
142
 
143
- absolutePath = absolutePath.Replace("\\", "/");
143
+ absolutePath = absolutePath.SanitizePath();
144
144
  if (absolutePath.StartsWith(Application.dataPath, StringComparison.OrdinalIgnoreCase))
145
145
  {
146
146
  string relativePath =
@@ -595,7 +595,7 @@
595
595
 
596
596
  string firstFramePath = AssetDatabase.GetAssetPath(validFrames[0]);
597
597
  string assetPath =
598
- Path.GetDirectoryName(firstFramePath)?.Replace("\\", "/") ?? "Assets";
598
+ Path.GetDirectoryName(firstFramePath).SanitizePath() ?? "Assets";
599
599
  if (!assetPath.EndsWith("/"))
600
600
  {
601
601
  assetPath += "/";
@@ -775,8 +775,7 @@
775
775
  }
776
776
 
777
777
  string assetPath = AssetDatabase.GetAssetPath(sprite);
778
- string directoryPath =
779
- Path.GetDirectoryName(assetPath)?.Replace("\\", "/") ?? "";
778
+ string directoryPath = Path.GetDirectoryName(assetPath).SanitizePath() ?? "";
780
779
  string frameName = sprite.name;
781
780
 
782
781
  int splitIndex = frameName.LastIndexOf('_');
@@ -336,6 +336,14 @@
336
336
  _ = updatedImporters.Remove(textureImporter);
337
337
  }
338
338
  }
339
+ }
340
+ finally
341
+ {
342
+ if (applyChanges)
343
+ {
344
+ AssetDatabase.StopAssetEditing();
345
+ }
346
+ EditorUtility.ClearProgressBar();
339
347
 
340
348
  if (applyChanges)
341
349
  {
@@ -356,14 +364,6 @@
356
364
  }
357
365
  }
358
366
  }
359
- finally
360
- {
361
- if (applyChanges)
362
- {
363
- AssetDatabase.StopAssetEditing();
364
- }
365
- EditorUtility.ClearProgressBar();
366
- }
367
367
  return changedCount;
368
368
  }
369
369
  }
@@ -213,11 +213,6 @@
213
213
  newImporters.Add(newImporter);
214
214
  }
215
215
  }
216
-
217
- foreach (TextureImporter newImporter in newImporters)
218
- {
219
- newImporter.SaveAndReimport();
220
- }
221
216
  }
222
217
  finally
223
218
  {
@@ -276,8 +271,10 @@
276
271
  return null;
277
272
  }
278
273
 
279
- TextureImporter importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
280
- if (importer is not { textureType: TextureImporterType.Sprite })
274
+ if (
275
+ AssetImporter.GetAtPath(assetPath)
276
+ is not TextureImporter { textureType: TextureImporterType.Sprite } importer
277
+ )
281
278
  {
282
279
  return null;
283
280
  }
@@ -285,7 +282,6 @@
285
282
  TextureImporterSettings originalSettings = new();
286
283
  importer.ReadTextureSettings(originalSettings);
287
284
 
288
- bool originalReadableState = importer.isReadable;
289
285
  if (!importer.isReadable)
290
286
  {
291
287
  importer.isReadable = true;
@@ -298,142 +294,128 @@
298
294
  return null;
299
295
  }
300
296
 
301
- TextureImporter resultImporter;
302
- try
303
- {
304
- Color32[] pixels = tex.GetPixels32();
305
-
306
- int width = tex.width;
307
- int height = tex.height;
308
- int minX = width;
309
- int minY = height;
310
- int maxX = 0;
311
- int maxY = 0;
312
- bool hasVisible = false;
313
-
314
- object lockObject = new();
315
-
316
- Parallel.For(
317
- 0,
318
- width * height,
319
- () => (minX: width, minY: height, maxX: 0, maxY: 0, hasVisible: false),
320
- (index, _, localState) =>
321
- {
322
- int x = index % width;
323
- int y = index / width;
297
+ Color32[] pixels = tex.GetPixels32();
298
+ int width = tex.width;
299
+ int height = tex.height;
300
+ int minX = width;
301
+ int minY = height;
302
+ int maxX = 0;
303
+ int maxY = 0;
304
+ bool hasVisible = false;
305
+
306
+ object lockObject = new();
307
+
308
+ Parallel.For(
309
+ 0,
310
+ width * height,
311
+ () => (minX: width, minY: height, maxX: 0, maxY: 0, hasVisible: false),
312
+ (index, _, localState) =>
313
+ {
314
+ int x = index % width;
315
+ int y = index / width;
324
316
 
325
- float a = pixels[index].a / 255f;
326
- if (a > AlphaThreshold)
327
- {
328
- localState.hasVisible = true;
329
- localState.minX = Mathf.Min(localState.minX, x);
330
- localState.minY = Mathf.Min(localState.minY, y);
331
- localState.maxX = Mathf.Max(localState.maxX, x);
332
- localState.maxY = Mathf.Max(localState.maxY, y);
333
- }
334
- return localState;
335
- },
336
- finalLocalState =>
317
+ float a = pixels[index].a / 255f;
318
+ if (a > AlphaThreshold)
337
319
  {
338
- if (finalLocalState.hasVisible)
339
- {
340
- lock (lockObject)
341
- {
342
- hasVisible = true;
343
- minX = Mathf.Min(minX, finalLocalState.minX);
344
- minY = Mathf.Min(minY, finalLocalState.minY);
345
- maxX = Mathf.Max(maxX, finalLocalState.maxX);
346
- maxY = Mathf.Max(maxY, finalLocalState.maxY);
347
- }
348
- }
320
+ localState.hasVisible = true;
321
+ localState.minX = Mathf.Min(localState.minX, x);
322
+ localState.minY = Mathf.Min(localState.minY, y);
323
+ localState.maxX = Mathf.Max(localState.maxX, x);
324
+ localState.maxY = Mathf.Max(localState.maxY, y);
349
325
  }
350
- );
351
-
352
- if (!hasVisible)
326
+ return localState;
327
+ },
328
+ finalLocalState =>
353
329
  {
354
- return null;
355
- }
356
-
357
- int cropWidth = maxX - minX + 1;
358
- int cropHeight = maxY - minY + 1;
359
-
360
- if (_onlyNecessary && cropWidth == width && cropHeight == height)
361
- {
362
- return null;
363
- }
364
-
365
- Texture2D cropped = new(cropWidth, cropHeight, TextureFormat.RGBA32, false);
366
- Color32[] croppedPixels = new Color32[cropWidth * cropHeight];
367
-
368
- Parallel.For(
369
- 0,
370
- cropHeight,
371
- y =>
330
+ if (finalLocalState.hasVisible)
372
331
  {
373
- int sourceYOffset = (y + minY) * width;
374
- int destYOffset = y * cropWidth;
375
- for (int x = 0; x < cropWidth; ++x)
332
+ lock (lockObject)
376
333
  {
377
- croppedPixels[destYOffset + x] = pixels[sourceYOffset + x + minX];
334
+ hasVisible = true;
335
+ minX = Mathf.Min(minX, finalLocalState.minX);
336
+ minY = Mathf.Min(minY, finalLocalState.minY);
337
+ maxX = Mathf.Max(maxX, finalLocalState.maxX);
338
+ maxY = Mathf.Max(maxY, finalLocalState.maxY);
378
339
  }
379
340
  }
380
- );
381
-
382
- cropped.SetPixels32(croppedPixels);
383
- cropped.Apply();
384
-
385
- string newPath = Path.Combine(
386
- assetDirectory,
387
- CroppedPrefix + Path.GetFileName(assetPath)
388
- );
389
- File.WriteAllBytes(newPath, cropped.EncodeToPNG());
390
- DestroyImmediate(cropped);
391
- AssetDatabase.ImportAsset(newPath);
392
- TextureImporter newImporter = AssetImporter.GetAtPath(newPath) as TextureImporter;
393
- if (newImporter == null)
394
- {
395
- return null;
396
341
  }
342
+ );
397
343
 
398
- newImporter.textureType = importer.textureType;
399
- newImporter.spriteImportMode = importer.spriteImportMode;
400
- newImporter.filterMode = importer.filterMode;
401
- newImporter.textureCompression = importer.textureCompression;
402
- newImporter.wrapMode = importer.wrapMode;
403
- newImporter.mipmapEnabled = importer.mipmapEnabled;
404
- newImporter.spritePixelsPerUnit = importer.spritePixelsPerUnit;
405
-
406
- TextureImporterSettings newSettings = new();
407
- newImporter.ReadTextureSettings(newSettings);
408
- newSettings.spriteExtrude = originalSettings.spriteExtrude;
409
-
410
- Vector2 origPivot = GetSpritePivot(importer);
411
- Vector2 origCenter = new(width * origPivot.x, height * origPivot.y);
412
- Vector2 newPivotPixels = origCenter - new Vector2(minX, minY);
413
- Vector2 newPivotNorm = new(
414
- cropWidth > 0 ? newPivotPixels.x / cropWidth : 0.5f,
415
- cropHeight > 0 ? newPivotPixels.y / cropHeight : 0.5f
416
- );
417
-
418
- newImporter.spriteImportMode = SpriteImportMode.Single;
419
- newImporter.spritePivot = newPivotNorm;
420
- newSettings.spritePivot = newPivotNorm;
421
- newSettings.spriteAlignment = (int)SpriteAlignment.Custom;
344
+ if (!hasVisible)
345
+ {
346
+ return null;
347
+ }
422
348
 
423
- newImporter.SetTextureSettings(newSettings);
424
- newImporter.isReadable = false;
349
+ int cropWidth = maxX - minX + 1;
350
+ int cropHeight = maxY - minY + 1;
425
351
 
426
- resultImporter = newImporter;
427
- }
428
- finally
352
+ if (_onlyNecessary && cropWidth == width && cropHeight == height)
429
353
  {
430
- if (importer != null && importer.isReadable != originalReadableState)
354
+ return null;
355
+ }
356
+
357
+ Texture2D cropped = new(cropWidth, cropHeight, TextureFormat.RGBA32, false);
358
+ Color32[] croppedPixels = new Color32[cropWidth * cropHeight];
359
+
360
+ Parallel.For(
361
+ 0,
362
+ cropHeight,
363
+ y =>
431
364
  {
432
- importer.isReadable = originalReadableState;
433
- importer.SaveAndReimport();
365
+ int sourceYOffset = (y + minY) * width;
366
+ int destYOffset = y * cropWidth;
367
+ for (int x = 0; x < cropWidth; ++x)
368
+ {
369
+ croppedPixels[destYOffset + x] = pixels[sourceYOffset + x + minX];
370
+ }
434
371
  }
372
+ );
373
+
374
+ cropped.SetPixels32(croppedPixels);
375
+ cropped.Apply();
376
+
377
+ string newPath = Path.Combine(
378
+ assetDirectory,
379
+ CroppedPrefix + Path.GetFileName(assetPath)
380
+ );
381
+ File.WriteAllBytes(newPath, cropped.EncodeToPNG());
382
+ DestroyImmediate(cropped);
383
+ AssetDatabase.ImportAsset(newPath);
384
+ TextureImporter newImporter = AssetImporter.GetAtPath(newPath) as TextureImporter;
385
+ if (newImporter == null)
386
+ {
387
+ return null;
435
388
  }
436
389
 
390
+ newImporter.textureType = importer.textureType;
391
+ newImporter.spriteImportMode = importer.spriteImportMode;
392
+ newImporter.filterMode = importer.filterMode;
393
+ newImporter.textureCompression = importer.textureCompression;
394
+ newImporter.wrapMode = importer.wrapMode;
395
+ newImporter.mipmapEnabled = importer.mipmapEnabled;
396
+ newImporter.spritePixelsPerUnit = importer.spritePixelsPerUnit;
397
+
398
+ TextureImporterSettings newSettings = new();
399
+ importer.ReadTextureSettings(newSettings);
400
+
401
+ Vector2 origPivot = GetSpritePivot(importer);
402
+ Vector2 origCenter = new(width * origPivot.x, height * origPivot.y);
403
+ Vector2 newPivotPixels = origCenter - new Vector2(minX, minY);
404
+ Vector2 newPivotNorm = new(
405
+ cropWidth > 0 ? newPivotPixels.x / cropWidth : 0.5f,
406
+ cropHeight > 0 ? newPivotPixels.y / cropHeight : 0.5f
407
+ );
408
+
409
+ newImporter.spriteImportMode = SpriteImportMode.Single;
410
+ newImporter.spritePivot = newPivotNorm;
411
+ newSettings.spritePivot = newPivotNorm;
412
+ newSettings.spriteAlignment = (int)SpriteAlignment.Custom;
413
+
414
+ newImporter.SetTextureSettings(newSettings);
415
+ newImporter.isReadable = true;
416
+ newImporter.SaveAndReimport();
417
+
418
+ TextureImporter resultImporter = newImporter;
437
419
  return resultImporter;
438
420
  }
439
421
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc73.6",
3
+ "version": "2.0.0-rc73.7",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -42,3 +42,4 @@
42
42
 
43
43
 
44
44
 
45
+