com.wallstop-studios.unity-helpers 2.0.0-rc76.4 → 2.0.0-rc76.6

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.
Files changed (31) hide show
  1. package/Editor/CustomDrawers/WShowIfPropertyDrawer.cs +1 -0
  2. package/Editor/CustomEditors/PersistentDirectoryGUI.cs +4 -1
  3. package/Editor/CustomEditors/PolygonCollider2DOptimizerEditor.cs +40 -0
  4. package/Editor/CustomEditors/PolygonCollider2DOptimizerEditor.cs.meta +3 -0
  5. package/Editor/CustomEditors/SourceFolderEntryDrawer.cs +43 -31
  6. package/{Runtime/Core/Extension → Editor/Extensions}/SerializedPropertyExtensions.cs +1 -1
  7. package/Editor/Sprites/ScriptableSpriteAtlasEditor.cs +9 -1
  8. package/Editor/Sprites/SpriteSheetAnimationCreator.cs +1218 -0
  9. package/Editor/Sprites/SpriteSheetAnimationCreator.cs.meta +3 -0
  10. package/Runtime/Core/DataStructure/CyclicBuffer.cs +29 -19
  11. package/Runtime/Core/Extension/DictionaryExtensions.cs +30 -10
  12. package/Runtime/Core/Extension/IEnumerableExtensions.cs +12 -10
  13. package/Runtime/Core/Extension/IListExtensions.cs +6 -0
  14. package/Runtime/Core/Extension/UnityExtensions.cs +68 -0
  15. package/Runtime/Core/Helper/Helpers.cs +12 -0
  16. package/Runtime/Core/Helper/LineHelper.cs +194 -0
  17. package/Runtime/Core/Helper/LineHelper.cs.meta +3 -0
  18. package/Runtime/Tags/CollisionSenses.cs +91 -0
  19. package/Runtime/Tags/CollisionSenses.cs.meta +3 -0
  20. package/Runtime/Utils/ChildSpawner.cs +100 -0
  21. package/Runtime/Utils/ChildSpawner.cs.meta +3 -0
  22. package/Runtime/Utils/CollisionProxy.cs +48 -0
  23. package/Runtime/Utils/CollisionProxy.cs.meta +3 -0
  24. package/Runtime/Utils/PolygonCollider2DOptimizer.cs +83 -0
  25. package/Runtime/Utils/PolygonCollider2DOptimizer.cs.meta +3 -0
  26. package/Runtime/Utils/SerializedStringComparer.cs +107 -0
  27. package/Runtime/Utils/SerializedStringComparer.cs.meta +3 -0
  28. package/Runtime/Utils/UnityObjectNameComparer.cs +46 -1
  29. package/Tests/Runtime/DataStructures/CyclicBufferTests.cs +2 -2
  30. package/package.json +3 -1
  31. /package/{Runtime/Core/Extension → Editor/Extensions}/SerializedPropertyExtensions.cs.meta +0 -0
@@ -0,0 +1,1218 @@
1
+ namespace WallstopStudios.UnityHelpers.Editor.Sprites
2
+ {
3
+ #if UNITY_EDITOR
4
+ using System;
5
+ using System.Collections.Generic;
6
+ using System.Diagnostics;
7
+ using System.IO;
8
+ using System.Linq;
9
+ using System.Runtime.Serialization;
10
+ using Core.Extension;
11
+ using Core.Helper;
12
+ using UnityEditor;
13
+ using UnityEditor.UIElements;
14
+ using UnityEngine;
15
+ using UnityEngine.UIElements;
16
+ using Debug = UnityEngine.Debug;
17
+ using Object = UnityEngine.Object;
18
+
19
+ public sealed class SpriteSheetAnimationCreator : EditorWindow
20
+ {
21
+ private const float ThumbnailSize = 64f;
22
+
23
+ private Texture2D _selectedSpriteSheet;
24
+ private readonly List<Sprite> _availableSprites = new();
25
+
26
+ private readonly List<AnimationDefinition> _animationDefinitions = new();
27
+
28
+ private ObjectField _spriteSheetField;
29
+ private Button _refreshSpritesButton;
30
+ private Button _loadSpritesButton;
31
+ private ScrollView _spriteThumbnailsScrollView;
32
+ private VisualElement _spriteThumbnailsContainer;
33
+ private ListView _animationDefinitionsListView;
34
+ private Button _addAnimationDefinitionButton;
35
+ private Button _generateAnimationsButton;
36
+
37
+ private VisualElement _previewContainer;
38
+ private Image _previewImage;
39
+ private Label _previewFrameLabel;
40
+ private Button _playPreviewButton;
41
+ private Button _stopPreviewButton;
42
+ private Button _prevFrameButton;
43
+ private Button _nextFrameButton;
44
+ private Slider _previewScrubber;
45
+
46
+ private bool _isPreviewing;
47
+ private int _currentPreviewAnimDefIndex = -1;
48
+ private int _currentPreviewSpriteIndex;
49
+ private AnimationDefinition _currentPreviewDefinition;
50
+ private readonly EditorApplication.CallbackFunction _editorUpdateCallback;
51
+ private readonly Stopwatch _timer = Stopwatch.StartNew();
52
+
53
+ private TimeSpan? _lastTick;
54
+
55
+ [MenuItem("Tools/Wallstop Studios/Unity Helpers/Sprite Sheet Animation Creator")]
56
+ public static void ShowWindow()
57
+ {
58
+ SpriteSheetAnimationCreator window = GetWindow<SpriteSheetAnimationCreator>();
59
+ window.titleContent = new GUIContent("Sprite Animation Creator");
60
+ window.minSize = new Vector2(600, 700);
61
+ }
62
+
63
+ [Serializable]
64
+ [DataContract]
65
+ public sealed class AnimationDefinition
66
+ {
67
+ public string Name = "New Animation";
68
+ public int StartSpriteIndex;
69
+ public int EndSpriteIndex;
70
+ public float DefaultFrameRate = 12f;
71
+ public AnimationCurve FrameRateCurve = AnimationCurve.Constant(0, 1, 12f);
72
+ public List<Sprite> SpritesToAnimate = new();
73
+
74
+ public TextField nameField;
75
+ public IntegerField startIndexField;
76
+ public IntegerField endIndexField;
77
+ public FloatField defaultFrameRateField;
78
+ public CurveField frameRateCurveField;
79
+ public Label spriteCountLabel;
80
+ public Button previewButton;
81
+ public Button removeButton;
82
+ }
83
+
84
+ public SpriteSheetAnimationCreator()
85
+ {
86
+ _editorUpdateCallback = OnEditorUpdate;
87
+ }
88
+
89
+ public void CreateGUI()
90
+ {
91
+ VisualElement root = rootVisualElement;
92
+ root.style.paddingLeft = 10;
93
+ root.style.paddingRight = 10;
94
+ root.style.paddingTop = 10;
95
+ root.style.paddingBottom = 10;
96
+
97
+ VisualElement topSection = new()
98
+ {
99
+ style = { flexDirection = FlexDirection.Row, marginBottom = 10 },
100
+ };
101
+ _spriteSheetField = new ObjectField("Sprite Sheet")
102
+ {
103
+ objectType = typeof(Texture2D),
104
+ allowSceneObjects = false,
105
+ style =
106
+ {
107
+ flexGrow = 1,
108
+ flexShrink = 0,
109
+ minHeight = 20,
110
+ },
111
+ };
112
+ _spriteSheetField.RegisterValueChangedCallback(OnSpriteSheetSelected);
113
+ topSection.Add(_spriteSheetField);
114
+
115
+ _loadSpritesButton = new Button(() =>
116
+ {
117
+ string filePath = string.Empty;
118
+ if (_spriteSheetField.value != null)
119
+ {
120
+ filePath = AssetDatabase.GetAssetPath(_spriteSheetField.value);
121
+ }
122
+
123
+ if (string.IsNullOrWhiteSpace(filePath))
124
+ {
125
+ filePath = Application.dataPath;
126
+ }
127
+
128
+ string selectedPath = EditorUtility.OpenFilePanel(
129
+ "Select Sprite Sheet",
130
+ filePath,
131
+ "png,jpg,gif,bmp,psd"
132
+ );
133
+ if (string.IsNullOrWhiteSpace(selectedPath))
134
+ {
135
+ return;
136
+ }
137
+
138
+ string relativePath = DirectoryHelper.AbsoluteToUnityRelativePath(selectedPath);
139
+ if (!string.IsNullOrWhiteSpace(relativePath))
140
+ {
141
+ Texture2D loadedTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(
142
+ relativePath
143
+ );
144
+ if (loadedTexture != null)
145
+ {
146
+ _spriteSheetField.value = loadedTexture;
147
+ }
148
+ }
149
+ })
150
+ {
151
+ text = "Load Sprites",
152
+ style = { marginLeft = 5, minHeight = 20 },
153
+ };
154
+ topSection.Add(_loadSpritesButton);
155
+
156
+ _refreshSpritesButton = new Button(LoadAndDisplaySprites)
157
+ {
158
+ text = "Refresh Sprites",
159
+ style = { marginLeft = 5, minHeight = 20 },
160
+ };
161
+ topSection.Add(_refreshSpritesButton);
162
+ root.Add(topSection);
163
+
164
+ Label thumbnailsLabel = new("Available Sprites:")
165
+ {
166
+ style =
167
+ {
168
+ unityFontStyleAndWeight = FontStyle.Bold,
169
+ marginTop = 5,
170
+ marginBottom = 5,
171
+ },
172
+ };
173
+ root.Add(thumbnailsLabel);
174
+ _spriteThumbnailsScrollView = new ScrollView(ScrollViewMode.Horizontal)
175
+ {
176
+ style =
177
+ {
178
+ height = ThumbnailSize + 20,
179
+ minHeight = ThumbnailSize + 20,
180
+ borderTopWidth = 1,
181
+ borderBottomWidth = 1,
182
+ borderLeftWidth = 1,
183
+ borderRightWidth = 1,
184
+ borderBottomColor = Color.gray,
185
+ borderTopColor = Color.gray,
186
+ borderLeftColor = Color.gray,
187
+ borderRightColor = Color.gray,
188
+ paddingLeft = 5,
189
+ paddingRight = 5,
190
+ paddingTop = 5,
191
+ paddingBottom = 5,
192
+ marginBottom = 10,
193
+ },
194
+ };
195
+ _spriteThumbnailsContainer = new VisualElement
196
+ {
197
+ style = { flexDirection = FlexDirection.Row },
198
+ };
199
+ _spriteThumbnailsScrollView.Add(_spriteThumbnailsContainer);
200
+ root.Add(_spriteThumbnailsScrollView);
201
+
202
+ Label animDefsLabel = new("Animation Definitions:")
203
+ {
204
+ style =
205
+ {
206
+ unityFontStyleAndWeight = FontStyle.Bold,
207
+ marginTop = 10,
208
+ marginBottom = 5,
209
+ },
210
+ };
211
+ root.Add(animDefsLabel);
212
+
213
+ _animationDefinitionsListView = new ListView(
214
+ _animationDefinitions,
215
+ 130,
216
+ MakeAnimationDefinitionItem,
217
+ BindAnimationDefinitionItem
218
+ )
219
+ {
220
+ selectionType = SelectionType.None,
221
+ style = { flexGrow = 1, minHeight = 200 },
222
+ };
223
+ root.Add(_animationDefinitionsListView);
224
+
225
+ _addAnimationDefinitionButton = new Button(AddAnimationDefinition)
226
+ {
227
+ text = "Add Animation Definition",
228
+ style = { marginTop = 5 },
229
+ };
230
+ root.Add(_addAnimationDefinitionButton);
231
+
232
+ Label previewSectionLabel = new("Animation Preview:")
233
+ {
234
+ style =
235
+ {
236
+ unityFontStyleAndWeight = FontStyle.Bold,
237
+ marginTop = 15,
238
+ marginBottom = 5,
239
+ },
240
+ };
241
+ root.Add(previewSectionLabel);
242
+
243
+ _previewContainer = new VisualElement
244
+ {
245
+ style =
246
+ {
247
+ flexDirection = FlexDirection.Column,
248
+ alignItems = Align.Center,
249
+ borderTopWidth = 1,
250
+ borderBottomWidth = 1,
251
+ borderLeftWidth = 1,
252
+ borderRightWidth = 1,
253
+ borderBottomColor = Color.gray,
254
+ borderTopColor = Color.gray,
255
+ borderLeftColor = Color.gray,
256
+ borderRightColor = Color.gray,
257
+ paddingBottom = 10,
258
+ paddingTop = 10,
259
+ minHeight = 150,
260
+ },
261
+ };
262
+ _previewImage = new Image
263
+ {
264
+ scaleMode = ScaleMode.ScaleToFit,
265
+ style =
266
+ {
267
+ width = 128,
268
+ height = 128,
269
+ marginBottom = 10,
270
+ backgroundColor = new StyleColor(new Color(0.2f, 0.2f, 0.2f)),
271
+ },
272
+ };
273
+ _previewContainer.Add(_previewImage);
274
+
275
+ _previewFrameLabel = new Label("Frame: -/- | FPS: -")
276
+ {
277
+ style = { alignSelf = Align.Center, marginBottom = 5 },
278
+ };
279
+ _previewContainer.Add(_previewFrameLabel);
280
+
281
+ _previewScrubber = new Slider(0, 1)
282
+ {
283
+ style =
284
+ {
285
+ minWidth = 200,
286
+ marginBottom = 5,
287
+ visibility = Visibility.Hidden,
288
+ },
289
+ };
290
+ _previewScrubber.RegisterValueChangedCallback(evt =>
291
+ {
292
+ if (
293
+ _currentPreviewDefinition != null
294
+ && 0 < _currentPreviewDefinition.SpritesToAnimate.Count
295
+ )
296
+ {
297
+ int frame = Mathf.FloorToInt(
298
+ evt.newValue * (_currentPreviewDefinition.SpritesToAnimate.Count - 1)
299
+ );
300
+ SetPreviewFrame(frame);
301
+ }
302
+ });
303
+ _previewContainer.Add(_previewScrubber);
304
+
305
+ VisualElement previewControls = new()
306
+ {
307
+ style = { flexDirection = FlexDirection.Row, justifyContent = Justify.Center },
308
+ };
309
+ _prevFrameButton = new Button(() => AdjustPreviewFrame(-1))
310
+ {
311
+ text = "◀",
312
+ style = { minWidth = 40 },
313
+ };
314
+ _playPreviewButton = new Button(PlayCurrentPreview)
315
+ {
316
+ text = "▶ Play",
317
+ style = { minWidth = 70 },
318
+ };
319
+ _stopPreviewButton = new Button(StopCurrentPreview)
320
+ {
321
+ text = "◼ Stop",
322
+ style = { minWidth = 70, display = DisplayStyle.None },
323
+ };
324
+ _nextFrameButton = new Button(() => AdjustPreviewFrame(1))
325
+ {
326
+ text = "▶",
327
+ style = { minWidth = 40 },
328
+ };
329
+ previewControls.Add(_prevFrameButton);
330
+ previewControls.Add(_playPreviewButton);
331
+ previewControls.Add(_stopPreviewButton);
332
+ previewControls.Add(_nextFrameButton);
333
+ _previewContainer.Add(previewControls);
334
+ root.Add(_previewContainer);
335
+
336
+ _generateAnimationsButton = new Button(GenerateAnimations)
337
+ {
338
+ text = "Generate Animation Files",
339
+ style = { marginTop = 15, height = 30 },
340
+ };
341
+ root.Add(_generateAnimationsButton);
342
+
343
+ if (_selectedSpriteSheet != null)
344
+ {
345
+ _spriteSheetField.SetValueWithoutNotify(_selectedSpriteSheet);
346
+ LoadAndDisplaySprites();
347
+ }
348
+ _animationDefinitionsListView.Rebuild();
349
+ }
350
+
351
+ private void OnEnable()
352
+ {
353
+ EditorApplication.update += _editorUpdateCallback;
354
+
355
+ string data = SessionState.GetString(GetType().FullName, "");
356
+ if (!string.IsNullOrEmpty(data))
357
+ {
358
+ JsonUtility.FromJsonOverwrite(data, this);
359
+ }
360
+ if (_selectedSpriteSheet != null)
361
+ {
362
+ EditorApplication.delayCall += () =>
363
+ {
364
+ if (_spriteSheetField != null)
365
+ {
366
+ _spriteSheetField.value = _selectedSpriteSheet;
367
+ }
368
+ LoadAndDisplaySprites();
369
+ _animationDefinitionsListView.Rebuild();
370
+ };
371
+ }
372
+ }
373
+
374
+ private void OnDisable()
375
+ {
376
+ EditorApplication.update -= _editorUpdateCallback;
377
+ StopCurrentPreview();
378
+
379
+ string data = JsonUtility.ToJson(this);
380
+ SessionState.SetString(GetType().FullName, data);
381
+ }
382
+
383
+ private void OnEditorUpdate()
384
+ {
385
+ if (
386
+ !_isPreviewing
387
+ || _currentPreviewDefinition is not { SpritesToAnimate: { Count: > 0 } }
388
+ )
389
+ {
390
+ return;
391
+ }
392
+
393
+ _lastTick ??= _timer.Elapsed;
394
+ float targetFps = 0;
395
+ if (1 < _currentPreviewDefinition.SpritesToAnimate.Count)
396
+ {
397
+ _currentPreviewDefinition.FrameRateCurve.Evaluate(
398
+ _currentPreviewSpriteIndex
399
+ / (_currentPreviewDefinition.SpritesToAnimate.Count - 1f)
400
+ );
401
+ }
402
+
403
+ if (targetFps <= 0)
404
+ {
405
+ targetFps = _currentPreviewDefinition.DefaultFrameRate;
406
+ }
407
+
408
+ if (targetFps <= 0)
409
+ {
410
+ targetFps = 1;
411
+ }
412
+
413
+ TimeSpan elapsed = _timer.Elapsed;
414
+ TimeSpan deltaTime = TimeSpan.FromMilliseconds(1000 / targetFps);
415
+ if (_lastTick + deltaTime > elapsed)
416
+ {
417
+ return;
418
+ }
419
+
420
+ _lastTick += deltaTime;
421
+ int nextFrame = _currentPreviewSpriteIndex.WrappedIncrement(
422
+ _currentPreviewDefinition.SpritesToAnimate.Count
423
+ );
424
+ SetPreviewFrame(nextFrame);
425
+ }
426
+
427
+ private void OnSpriteSheetSelected(ChangeEvent<Object> evt)
428
+ {
429
+ _selectedSpriteSheet = evt.newValue as Texture2D;
430
+ _animationDefinitions.Clear();
431
+ AddAnimationDefinition();
432
+ _animationDefinitionsListView.Rebuild();
433
+ LoadAndDisplaySprites();
434
+ StopCurrentPreview();
435
+ _previewImage.sprite = null;
436
+ _previewImage.style.backgroundImage = null;
437
+ _previewFrameLabel.text = "Frame: -/- | FPS: -";
438
+ }
439
+
440
+ private void LoadAndDisplaySprites()
441
+ {
442
+ if (_selectedSpriteSheet == null)
443
+ {
444
+ EditorUtility.DisplayDialog("Error", "No sprite sheet selected.", "OK");
445
+ _availableSprites.Clear();
446
+ UpdateSpriteThumbnails();
447
+ return;
448
+ }
449
+
450
+ string path = AssetDatabase.GetAssetPath(_selectedSpriteSheet);
451
+ if (string.IsNullOrEmpty(path))
452
+ {
453
+ EditorUtility.DisplayDialog("Error", "Selected texture is not an asset.", "OK");
454
+ return;
455
+ }
456
+
457
+ TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
458
+ if (importer == null)
459
+ {
460
+ EditorUtility.DisplayDialog(
461
+ "Error",
462
+ "Could not get TextureImporter for the selected texture.",
463
+ "OK"
464
+ );
465
+ return;
466
+ }
467
+
468
+ bool importSettingsChanged = false;
469
+ if (importer.textureType != TextureImporterType.Sprite)
470
+ {
471
+ importer.textureType = TextureImporterType.Sprite;
472
+ importSettingsChanged = true;
473
+ }
474
+ if (importer.spriteImportMode != SpriteImportMode.Multiple)
475
+ {
476
+ bool fix = EditorUtility.DisplayDialog(
477
+ "Sprite Mode",
478
+ "The selected texture is not in 'Sprite Mode: Multiple'. This is required to extract individual sprites.\n\nAttempt to change it automatically?",
479
+ "Yes, Change It",
480
+ "No, I'll Do It"
481
+ );
482
+ if (fix)
483
+ {
484
+ importer.spriteImportMode = SpriteImportMode.Multiple;
485
+
486
+ importSettingsChanged = true;
487
+ }
488
+ else
489
+ {
490
+ _availableSprites.Clear();
491
+ UpdateSpriteThumbnails();
492
+ return;
493
+ }
494
+ }
495
+
496
+ if (importSettingsChanged)
497
+ {
498
+ EditorUtility.SetDirty(importer);
499
+ importer.SaveAndReimport();
500
+ AssetDatabase.Refresh();
501
+ }
502
+
503
+ _availableSprites.Clear();
504
+ Object[] assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
505
+ foreach (Object asset in assets)
506
+ {
507
+ if (asset is Sprite sprite && sprite != null)
508
+ {
509
+ _availableSprites.Add(sprite);
510
+ }
511
+ }
512
+
513
+ _availableSprites.SortByName();
514
+
515
+ if (_availableSprites.Count == 0)
516
+ {
517
+ EditorUtility.DisplayDialog(
518
+ "No Sprites",
519
+ "No sprites found in the selected Texture. Ensure it's sliced correctly in the Sprite Editor.",
520
+ "OK"
521
+ );
522
+ }
523
+
524
+ UpdateSpriteThumbnails();
525
+ UpdateAllAnimationDefinitionSprites();
526
+ _animationDefinitionsListView.Rebuild();
527
+ }
528
+
529
+ private void UpdateSpriteThumbnails()
530
+ {
531
+ _spriteThumbnailsContainer.Clear();
532
+ if (_availableSprites.Count == 0)
533
+ {
534
+ _spriteThumbnailsContainer.Add(new Label("No sprites loaded or sheet not sliced."));
535
+ return;
536
+ }
537
+
538
+ for (int i = 0; i < _availableSprites.Count; ++i)
539
+ {
540
+ Sprite sprite = _availableSprites[i];
541
+ VisualElement thumbContainer = new()
542
+ {
543
+ style = { alignItems = Align.Center, marginRight = 5 },
544
+ };
545
+ Image img = new()
546
+ {
547
+ sprite = sprite,
548
+ scaleMode = ScaleMode.ScaleToFit,
549
+ style = { width = ThumbnailSize, height = ThumbnailSize },
550
+ };
551
+ thumbContainer.Add(img);
552
+ thumbContainer.Add(new Label($"{i}") { style = { fontSize = 9 } });
553
+ _spriteThumbnailsContainer.Add(thumbContainer);
554
+ }
555
+ }
556
+
557
+ private void UpdateAllAnimationDefinitionSprites()
558
+ {
559
+ foreach (AnimationDefinition def in _animationDefinitions)
560
+ {
561
+ UpdateSpritesForDefinition(def);
562
+ }
563
+ }
564
+
565
+ private VisualElement MakeAnimationDefinitionItem()
566
+ {
567
+ VisualElement container = new()
568
+ {
569
+ style =
570
+ {
571
+ flexDirection = FlexDirection.Column,
572
+ borderBottomWidth = 1,
573
+ borderBottomColor = Color.gray,
574
+ paddingBottom = 10,
575
+ paddingTop = 5,
576
+ },
577
+ };
578
+ VisualElement firstRow = new()
579
+ {
580
+ style =
581
+ {
582
+ flexDirection = FlexDirection.Row,
583
+ alignItems = Align.Center,
584
+ marginBottom = 3,
585
+ },
586
+ };
587
+ VisualElement secondRow = new()
588
+ {
589
+ style =
590
+ {
591
+ flexDirection = FlexDirection.Row,
592
+ alignItems = Align.Center,
593
+ marginBottom = 3,
594
+ },
595
+ };
596
+ VisualElement thirdRow = new()
597
+ {
598
+ style = { flexDirection = FlexDirection.Row, alignItems = Align.Center },
599
+ };
600
+
601
+ TextField nameField = new("Name:")
602
+ {
603
+ style =
604
+ {
605
+ flexGrow = 1,
606
+ flexShrink = 1,
607
+ marginRight = 5,
608
+ },
609
+ };
610
+ Label spriteCountLabel = new("Sprites: 0")
611
+ {
612
+ style = { minWidth = 80, marginRight = 5 },
613
+ };
614
+ Button removeButton = new() { text = "Remove", style = { minWidth = 60 } };
615
+
616
+ firstRow.Add(nameField);
617
+ firstRow.Add(spriteCountLabel);
618
+ firstRow.Add(removeButton);
619
+
620
+ IntegerField startField = new("Start Idx:")
621
+ {
622
+ style =
623
+ {
624
+ flexGrow = 1,
625
+ flexShrink = 1,
626
+ marginRight = 5,
627
+ },
628
+ tooltip = "Index of the first sprite (from 'Available Sprites' above, 0-based).",
629
+ };
630
+ IntegerField endField = new("End Idx:")
631
+ {
632
+ style =
633
+ {
634
+ flexGrow = 1,
635
+ flexShrink = 1,
636
+ marginRight = 5,
637
+ },
638
+ tooltip = "Index of the last sprite (inclusive).",
639
+ };
640
+ Button previewButton = new() { text = "Preview This", style = { minWidth = 100 } };
641
+
642
+ secondRow.Add(startField);
643
+ secondRow.Add(endField);
644
+ secondRow.Add(previewButton);
645
+
646
+ FloatField fpsField = new("Default FPS:")
647
+ {
648
+ style =
649
+ {
650
+ flexGrow = 1,
651
+ flexShrink = 1,
652
+ marginRight = 5,
653
+ },
654
+ };
655
+ CurveField curveField = new("FPS Curve:") { style = { flexGrow = 1, flexShrink = 1 } };
656
+
657
+ thirdRow.Add(fpsField);
658
+ thirdRow.Add(curveField);
659
+
660
+ container.Add(firstRow);
661
+ container.Add(secondRow);
662
+ container.Add(thirdRow);
663
+
664
+ container.userData = new AnimationDefUITags
665
+ {
666
+ nameField = nameField,
667
+ startIndexField = startField,
668
+ endIndexField = endField,
669
+ defaultFrameRateField = fpsField,
670
+ frameRateCurveField = curveField,
671
+ spriteCountLabel = spriteCountLabel,
672
+ previewButton = previewButton,
673
+ removeButton = removeButton,
674
+ };
675
+ return container;
676
+ }
677
+
678
+ private class AnimationDefUITags
679
+ {
680
+ public TextField nameField;
681
+ public IntegerField startIndexField;
682
+ public IntegerField endIndexField;
683
+ public FloatField defaultFrameRateField;
684
+ public CurveField frameRateCurveField;
685
+ public Label spriteCountLabel;
686
+ public Button previewButton;
687
+ public Button removeButton;
688
+ }
689
+
690
+ private void BindAnimationDefinitionItem(VisualElement element, int index)
691
+ {
692
+ AnimationDefinition def = _animationDefinitions[index];
693
+ if (element.userData is not AnimationDefUITags tags)
694
+ {
695
+ this.LogError(
696
+ $"Element UserData was not AnimationDefUITags, found: {element.userData?.GetType()}."
697
+ );
698
+ return;
699
+ }
700
+
701
+ def.nameField?.UnregisterValueChangedCallback(
702
+ def.nameField.userData as EventCallback<ChangeEvent<string>>
703
+ );
704
+
705
+ def.startIndexField?.UnregisterValueChangedCallback(
706
+ def.startIndexField.userData as EventCallback<ChangeEvent<int>>
707
+ );
708
+
709
+ def.endIndexField?.UnregisterValueChangedCallback(
710
+ def.endIndexField.userData as EventCallback<ChangeEvent<int>>
711
+ );
712
+
713
+ def.defaultFrameRateField?.UnregisterValueChangedCallback(
714
+ def.defaultFrameRateField.userData as EventCallback<ChangeEvent<float>>
715
+ );
716
+
717
+ def.frameRateCurveField?.UnregisterValueChangedCallback(
718
+ def.frameRateCurveField.userData as EventCallback<ChangeEvent<AnimationCurve>>
719
+ );
720
+
721
+ if (def.removeButton != null)
722
+ {
723
+ def.removeButton.clicked -= (Action)def.removeButton.userData;
724
+ }
725
+
726
+ if (def.previewButton != null)
727
+ {
728
+ def.previewButton.clicked -= (Action)def.previewButton.userData;
729
+ }
730
+
731
+ def.nameField = tags.nameField;
732
+ def.startIndexField = tags.startIndexField;
733
+ def.endIndexField = tags.endIndexField;
734
+ def.defaultFrameRateField = tags.defaultFrameRateField;
735
+ def.frameRateCurveField = tags.frameRateCurveField;
736
+ def.spriteCountLabel = tags.spriteCountLabel;
737
+ def.removeButton = tags.removeButton;
738
+ def.previewButton = tags.previewButton;
739
+
740
+ def.nameField.SetValueWithoutNotify(def.Name);
741
+ EventCallback<ChangeEvent<string>> nameChangeCallback = evt =>
742
+ {
743
+ def.Name = evt.newValue;
744
+ };
745
+ def.nameField.RegisterValueChangedCallback(nameChangeCallback);
746
+ def.nameField.userData = nameChangeCallback;
747
+
748
+ def.startIndexField.SetValueWithoutNotify(def.StartSpriteIndex);
749
+ EventCallback<ChangeEvent<int>> startChangeCallback = evt =>
750
+ {
751
+ def.StartSpriteIndex = Mathf.Clamp(
752
+ evt.newValue,
753
+ 0,
754
+ _availableSprites.Count > 0 ? _availableSprites.Count - 1 : 0
755
+ );
756
+ if (def.StartSpriteIndex > def.EndSpriteIndex && 0 < _availableSprites.Count)
757
+ {
758
+ def.EndSpriteIndex = def.StartSpriteIndex;
759
+ }
760
+
761
+ def.startIndexField.SetValueWithoutNotify(def.StartSpriteIndex);
762
+ UpdateSpritesForDefinition(def);
763
+ if (_currentPreviewAnimDefIndex == index)
764
+ {
765
+ StartOrUpdateCurrentPreview(def);
766
+ }
767
+ };
768
+ def.startIndexField.RegisterValueChangedCallback(startChangeCallback);
769
+ def.startIndexField.userData = startChangeCallback;
770
+
771
+ def.endIndexField.SetValueWithoutNotify(def.EndSpriteIndex);
772
+ EventCallback<ChangeEvent<int>> endChangeCallback = evt =>
773
+ {
774
+ def.EndSpriteIndex = Mathf.Clamp(
775
+ evt.newValue,
776
+ 0,
777
+ _availableSprites.Count > 0 ? _availableSprites.Count - 1 : 0
778
+ );
779
+ if (def.EndSpriteIndex < def.StartSpriteIndex && 0 < _availableSprites.Count)
780
+ {
781
+ def.StartSpriteIndex = def.EndSpriteIndex;
782
+ }
783
+
784
+ def.endIndexField.SetValueWithoutNotify(def.EndSpriteIndex);
785
+ UpdateSpritesForDefinition(def);
786
+ if (_currentPreviewAnimDefIndex == index)
787
+ {
788
+ StartOrUpdateCurrentPreview(def);
789
+ }
790
+ };
791
+ def.endIndexField.RegisterValueChangedCallback(endChangeCallback);
792
+ def.endIndexField.userData = endChangeCallback;
793
+
794
+ def.defaultFrameRateField.SetValueWithoutNotify(def.DefaultFrameRate);
795
+ EventCallback<ChangeEvent<float>> fpsChangeCallback = evt =>
796
+ {
797
+ def.DefaultFrameRate = Mathf.Max(0.1f, evt.newValue);
798
+ def.defaultFrameRateField.SetValueWithoutNotify(def.DefaultFrameRate);
799
+
800
+ if (IsCurveConstant(def.FrameRateCurve))
801
+ {
802
+ def.FrameRateCurve = AnimationCurve.Constant(0, 1, def.DefaultFrameRate);
803
+ def.frameRateCurveField.SetValueWithoutNotify(def.FrameRateCurve);
804
+ }
805
+ if (_currentPreviewAnimDefIndex == index)
806
+ {
807
+ StartOrUpdateCurrentPreview(def);
808
+ }
809
+ };
810
+ def.defaultFrameRateField.RegisterValueChangedCallback(fpsChangeCallback);
811
+ def.defaultFrameRateField.userData = fpsChangeCallback;
812
+
813
+ def.frameRateCurveField.SetValueWithoutNotify(def.FrameRateCurve);
814
+ EventCallback<ChangeEvent<AnimationCurve>> curveChangeCallback = evt =>
815
+ {
816
+ def.FrameRateCurve = evt.newValue;
817
+ if (_currentPreviewAnimDefIndex == index)
818
+ {
819
+ StartOrUpdateCurrentPreview(def);
820
+ }
821
+ };
822
+ def.frameRateCurveField.RegisterValueChangedCallback(curveChangeCallback);
823
+ def.frameRateCurveField.userData = curveChangeCallback;
824
+
825
+ Action removeAction = () => RemoveAnimationDefinition(index);
826
+ def.removeButton.clicked += removeAction;
827
+ def.removeButton.userData = removeAction;
828
+
829
+ Action previewAction = () =>
830
+ {
831
+ _currentPreviewAnimDefIndex = index;
832
+ StartOrUpdateCurrentPreview(def);
833
+ };
834
+ def.previewButton.clicked += previewAction;
835
+ def.previewButton.userData = previewAction;
836
+
837
+ UpdateSpritesForDefinition(def);
838
+ }
839
+
840
+ private static bool IsCurveConstant(AnimationCurve curve)
841
+ {
842
+ if (curve == null || curve.keys.Length < 2)
843
+ {
844
+ return true;
845
+ }
846
+
847
+ float firstValue = curve.keys[0].value;
848
+ for (int i = 1; i < curve.keys.Length; ++i)
849
+ {
850
+ if (!Mathf.Approximately(curve.keys[i].value, firstValue))
851
+ {
852
+ return false;
853
+ }
854
+ }
855
+ return true;
856
+ }
857
+
858
+ private void UpdateSpritesForDefinition(AnimationDefinition def)
859
+ {
860
+ def.SpritesToAnimate.Clear();
861
+ if (
862
+ 0 < _availableSprites.Count
863
+ && def.StartSpriteIndex <= def.EndSpriteIndex
864
+ && def.StartSpriteIndex < _availableSprites.Count
865
+ && def.EndSpriteIndex < _availableSprites.Count
866
+ && def.StartSpriteIndex >= 0
867
+ && def.EndSpriteIndex >= 0
868
+ )
869
+ {
870
+ for (int i = def.StartSpriteIndex; i <= def.EndSpriteIndex; ++i)
871
+ {
872
+ def.SpritesToAnimate.Add(_availableSprites[i]);
873
+ }
874
+ }
875
+ if (def.spriteCountLabel != null)
876
+ {
877
+ def.spriteCountLabel.text = $"Sprites: {def.SpritesToAnimate.Count}";
878
+ }
879
+ }
880
+
881
+ private void AddAnimationDefinition()
882
+ {
883
+ AnimationDefinition newDef = new();
884
+ if (_selectedSpriteSheet != null)
885
+ {
886
+ newDef.Name = $"{_selectedSpriteSheet.name}_Anim_{_animationDefinitions.Count}";
887
+ }
888
+ if (0 < _availableSprites.Count)
889
+ {
890
+ newDef.EndSpriteIndex = _availableSprites.Count - 1;
891
+ }
892
+ newDef.FrameRateCurve = AnimationCurve.Constant(0, 1, newDef.DefaultFrameRate);
893
+ _animationDefinitions.Add(newDef);
894
+ UpdateSpritesForDefinition(newDef);
895
+ _animationDefinitionsListView.Rebuild();
896
+ }
897
+
898
+ private void RemoveAnimationDefinition(int index)
899
+ {
900
+ if (index >= 0 && index < _animationDefinitions.Count)
901
+ {
902
+ if (_currentPreviewAnimDefIndex == index)
903
+ {
904
+ StopCurrentPreview();
905
+ }
906
+
907
+ _animationDefinitions.RemoveAt(index);
908
+ _animationDefinitionsListView.Rebuild();
909
+ if (_currentPreviewAnimDefIndex > index)
910
+ {
911
+ _currentPreviewAnimDefIndex--;
912
+ }
913
+ }
914
+ }
915
+
916
+ private void StartOrUpdateCurrentPreview(AnimationDefinition def)
917
+ {
918
+ _currentPreviewDefinition = def;
919
+ if (def == null || def.SpritesToAnimate.Count == 0)
920
+ {
921
+ StopCurrentPreview();
922
+ _previewImage.sprite = null;
923
+ _previewImage.style.backgroundImage = null;
924
+ _previewFrameLabel.text = "Frame: -/- | FPS: -";
925
+ _previewScrubber.style.visibility = Visibility.Hidden;
926
+ return;
927
+ }
928
+
929
+ _previewScrubber.style.visibility = Visibility.Visible;
930
+ _previewScrubber.highValue = def.SpritesToAnimate.Count > 1 ? 1f : 0f;
931
+ _previewScrubber.SetValueWithoutNotify(0);
932
+
933
+ SetPreviewFrame(0);
934
+ }
935
+
936
+ private void PlayCurrentPreview()
937
+ {
938
+ if (
939
+ _currentPreviewDefinition == null
940
+ || _currentPreviewDefinition.SpritesToAnimate.Count == 0
941
+ )
942
+ {
943
+ EditorUtility.DisplayDialog(
944
+ "Preview Error",
945
+ "No animation definition selected or definition has no sprites. Click 'Preview This' on an animation definition first.",
946
+ "OK"
947
+ );
948
+ return;
949
+ }
950
+
951
+ _isPreviewing = true;
952
+ _playPreviewButton.style.display = DisplayStyle.None;
953
+ _stopPreviewButton.style.display = DisplayStyle.Flex;
954
+ }
955
+
956
+ private void StopCurrentPreview()
957
+ {
958
+ _isPreviewing = false;
959
+ _lastTick = null;
960
+ _playPreviewButton.style.display = DisplayStyle.Flex;
961
+ _stopPreviewButton.style.display = DisplayStyle.None;
962
+ }
963
+
964
+ private void SetPreviewFrame(int frameIndex)
965
+ {
966
+ if (_currentPreviewDefinition is not { SpritesToAnimate: { Count: > 0 } })
967
+ {
968
+ return;
969
+ }
970
+
971
+ _currentPreviewSpriteIndex = Mathf.Clamp(
972
+ frameIndex,
973
+ 0,
974
+ _currentPreviewDefinition.SpritesToAnimate.Count - 1
975
+ );
976
+
977
+ Sprite spriteToShow = _currentPreviewDefinition.SpritesToAnimate[
978
+ _currentPreviewSpriteIndex
979
+ ];
980
+ _previewImage.sprite = spriteToShow;
981
+ _previewImage.MarkDirtyRepaint();
982
+
983
+ float currentCurveTime = 0f;
984
+ if (_currentPreviewDefinition.SpritesToAnimate.Count > 1)
985
+ {
986
+ currentCurveTime =
987
+ (float)_currentPreviewSpriteIndex
988
+ / (_currentPreviewDefinition.SpritesToAnimate.Count - 1);
989
+ }
990
+
991
+ float fpsAtCurrent = _currentPreviewDefinition.FrameRateCurve.Evaluate(
992
+ currentCurveTime
993
+ * _currentPreviewDefinition.FrameRateCurve.keys.LastOrDefault().time
994
+ );
995
+ if (fpsAtCurrent <= 0)
996
+ {
997
+ fpsAtCurrent = _currentPreviewDefinition.DefaultFrameRate;
998
+ }
999
+
1000
+ _previewFrameLabel.text =
1001
+ $"Frame: {_currentPreviewSpriteIndex + 1}/{_currentPreviewDefinition.SpritesToAnimate.Count} | FPS: {fpsAtCurrent:F1}";
1002
+
1003
+ if (_currentPreviewDefinition.SpritesToAnimate.Count > 1)
1004
+ {
1005
+ _previewScrubber.SetValueWithoutNotify(
1006
+ (float)_currentPreviewSpriteIndex
1007
+ / (_currentPreviewDefinition.SpritesToAnimate.Count - 1)
1008
+ );
1009
+ }
1010
+ else
1011
+ {
1012
+ _previewScrubber.SetValueWithoutNotify(0);
1013
+ }
1014
+ }
1015
+
1016
+ private void AdjustPreviewFrame(int direction)
1017
+ {
1018
+ if (_currentPreviewDefinition is not { SpritesToAnimate: { Count: > 0 } })
1019
+ {
1020
+ return;
1021
+ }
1022
+
1023
+ StopCurrentPreview();
1024
+
1025
+ int newFrame = _currentPreviewSpriteIndex + direction;
1026
+
1027
+ int count = _currentPreviewDefinition.SpritesToAnimate.Count;
1028
+ if (newFrame < 0)
1029
+ {
1030
+ newFrame = count - 1;
1031
+ }
1032
+
1033
+ if (newFrame >= count)
1034
+ {
1035
+ newFrame = 0;
1036
+ }
1037
+
1038
+ SetPreviewFrame(newFrame);
1039
+ }
1040
+
1041
+ private void GenerateAnimations()
1042
+ {
1043
+ if (_selectedSpriteSheet == null)
1044
+ {
1045
+ EditorUtility.DisplayDialog("Error", "No sprite sheet loaded.", "OK");
1046
+ return;
1047
+ }
1048
+ if (_animationDefinitions.Count == 0)
1049
+ {
1050
+ EditorUtility.DisplayDialog("Error", "No animation definitions created.", "OK");
1051
+ return;
1052
+ }
1053
+
1054
+ string sheetPath = AssetDatabase.GetAssetPath(_selectedSpriteSheet);
1055
+ string directory = Path.GetDirectoryName(sheetPath);
1056
+ string animationsFolder = EditorUtility.OpenFolderPanel(
1057
+ "Select Output Directory",
1058
+ directory,
1059
+ string.Empty
1060
+ );
1061
+ if (string.IsNullOrWhiteSpace(animationsFolder))
1062
+ {
1063
+ return;
1064
+ }
1065
+
1066
+ if (!Directory.Exists(animationsFolder))
1067
+ {
1068
+ Directory.CreateDirectory(animationsFolder);
1069
+ }
1070
+
1071
+ int createdCount = 0;
1072
+ foreach (AnimationDefinition def in _animationDefinitions)
1073
+ {
1074
+ if (def.SpritesToAnimate.Count == 0)
1075
+ {
1076
+ Debug.LogWarning($"Skipping animation '{def.Name}' as it has no sprites.");
1077
+ continue;
1078
+ }
1079
+
1080
+ AnimationClip clip = new() { frameRate = 60 };
1081
+
1082
+ EditorCurveBinding spriteBinding = new()
1083
+ {
1084
+ type = typeof(SpriteRenderer),
1085
+ path = "",
1086
+ propertyName = "m_Sprite",
1087
+ };
1088
+
1089
+ ObjectReferenceKeyframe[] keyframes = new ObjectReferenceKeyframe[
1090
+ def.SpritesToAnimate.Count
1091
+ ];
1092
+ float currentTime = 0f;
1093
+ AnimationCurve curve = def.FrameRateCurve;
1094
+ if (curve == null || curve.keys.Length == 0)
1095
+ {
1096
+ Debug.LogWarning(
1097
+ $"Animation '{def.Name}' has an invalid FrameRateCurve. Falling back to DefaultFrameRate."
1098
+ );
1099
+ curve = AnimationCurve.Constant(0, 1, def.DefaultFrameRate);
1100
+ }
1101
+
1102
+ if (curve.keys.Length == 0)
1103
+ {
1104
+ curve.AddKey(0, def.DefaultFrameRate);
1105
+ }
1106
+
1107
+ float curveDuration = curve.keys.LastOrDefault().time;
1108
+ if (curveDuration <= 0)
1109
+ {
1110
+ curveDuration = 1f;
1111
+ }
1112
+
1113
+ for (int i = 0; i < def.SpritesToAnimate.Count; ++i)
1114
+ {
1115
+ keyframes[i] = new ObjectReferenceKeyframe
1116
+ {
1117
+ time = currentTime,
1118
+ value = def.SpritesToAnimate[i],
1119
+ };
1120
+
1121
+ if (i < def.SpritesToAnimate.Count - 1)
1122
+ {
1123
+ float normalizedTimeForCurve =
1124
+ def.SpritesToAnimate.Count > 1
1125
+ ? (float)i / (def.SpritesToAnimate.Count - 1)
1126
+ : 0;
1127
+ float timeForCurveEval = normalizedTimeForCurve * curveDuration;
1128
+
1129
+ float fps = curve.Evaluate(timeForCurveEval);
1130
+ if (fps <= 0)
1131
+ {
1132
+ fps = def.DefaultFrameRate;
1133
+ }
1134
+
1135
+ if (fps <= 0)
1136
+ {
1137
+ fps = 1;
1138
+ }
1139
+
1140
+ currentTime += 1.0f / fps;
1141
+ }
1142
+ }
1143
+
1144
+ AnimationUtility.SetObjectReferenceCurve(clip, spriteBinding, keyframes);
1145
+
1146
+ AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(clip);
1147
+ settings.loopTime = true;
1148
+ AnimationUtility.SetAnimationClipSettings(clip, settings);
1149
+
1150
+ string animName = string.IsNullOrEmpty(def.Name) ? "UnnamedAnim" : def.Name;
1151
+
1152
+ foreach (char character in Path.GetInvalidFileNameChars())
1153
+ {
1154
+ animName = animName.Replace(character, '_');
1155
+ }
1156
+ string assetPath = Path.Combine(animationsFolder, $"{animName}.anim");
1157
+ assetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
1158
+
1159
+ AssetDatabase.CreateAsset(clip, assetPath);
1160
+ createdCount++;
1161
+ }
1162
+
1163
+ if (createdCount > 0)
1164
+ {
1165
+ AssetDatabase.SaveAssets();
1166
+ AssetDatabase.Refresh();
1167
+ EditorUtility.DisplayDialog(
1168
+ "Success",
1169
+ $"{createdCount} animation(s) created in:\n{animationsFolder}",
1170
+ "OK"
1171
+ );
1172
+ EditorGUIUtility.PingObject(
1173
+ AssetDatabase.LoadAssetAtPath<Object>(animationsFolder)
1174
+ );
1175
+ }
1176
+ else
1177
+ {
1178
+ EditorUtility.DisplayDialog(
1179
+ "Finished",
1180
+ "No valid animations were generated.",
1181
+ "OK"
1182
+ );
1183
+ }
1184
+ }
1185
+
1186
+ private static void OnRootDragUpdated(DragUpdatedEvent evt)
1187
+ {
1188
+ if (DragAndDrop.objectReferences.Any(obj => obj is Texture2D))
1189
+ {
1190
+ DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
1191
+ }
1192
+ }
1193
+
1194
+ private void OnRootDragPerform(DragPerformEvent evt)
1195
+ {
1196
+ Texture2D draggedTexture =
1197
+ DragAndDrop.objectReferences.FirstOrDefault(obj => obj is Texture2D) as Texture2D;
1198
+ if (draggedTexture != null)
1199
+ {
1200
+ _spriteSheetField.value = draggedTexture;
1201
+ DragAndDrop.AcceptDrag();
1202
+ }
1203
+ }
1204
+
1205
+ public void OnBecameVisible()
1206
+ {
1207
+ rootVisualElement.RegisterCallback<DragUpdatedEvent>(OnRootDragUpdated);
1208
+ rootVisualElement.RegisterCallback<DragPerformEvent>(OnRootDragPerform);
1209
+ }
1210
+
1211
+ public void OnBecameInvisible()
1212
+ {
1213
+ rootVisualElement.UnregisterCallback<DragUpdatedEvent>(OnRootDragUpdated);
1214
+ rootVisualElement.UnregisterCallback<DragPerformEvent>(OnRootDragPerform);
1215
+ }
1216
+ }
1217
+ #endif
1218
+ }