com.wallstop-studios.unity-helpers 2.0.0-rc76.7 → 2.0.0-rc76.9

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.
@@ -12,10 +12,6 @@
12
12
 
13
13
  public sealed class SpriteLabelProcessor : AssetPostprocessor
14
14
  {
15
- private static readonly Dictionary<string, string[]> CachedLabels = new(
16
- StringComparer.OrdinalIgnoreCase
17
- );
18
-
19
15
  private static void OnPostprocessAllAssets(
20
16
  string[] importedAssets,
21
17
  string[] deletedAssets,
@@ -23,7 +19,7 @@
23
19
  string[] movedFromAssetPaths
24
20
  )
25
21
  {
26
- bool anyChanged = !CachedLabels.Any();
22
+ bool anyChanged = !Helpers.CachedLabels.Any();
27
23
  InitializeCacheIfNeeded();
28
24
 
29
25
  foreach (string path in importedAssets)
@@ -51,7 +47,7 @@
51
47
 
52
48
  string[] newLabels = AssetDatabase.GetLabels(mainObj);
53
49
  if (
54
- !CachedLabels.TryGetValue(path, out string[] oldLabels)
50
+ !Helpers.CachedLabels.TryGetValue(path, out string[] oldLabels)
55
51
  || !AreEqual(oldLabels, newLabels)
56
52
  )
57
53
  {
@@ -62,14 +58,14 @@
62
58
  string[] updated = new string[newLabels.Length];
63
59
  Array.Copy(newLabels, updated, newLabels.Length);
64
60
  anyChanged = true;
65
- CachedLabels[path] = updated;
61
+ Helpers.CachedLabels[path] = updated;
66
62
  }
67
63
  }
68
64
 
69
65
  if (anyChanged)
70
66
  {
71
- Helpers.AllSpriteLabels = CachedLabels
72
- .Values.SelectMany(x => x)
67
+ Helpers.AllSpriteLabels = Helpers
68
+ .CachedLabels.Values.SelectMany(x => x)
73
69
  .Distinct()
74
70
  .Ordered()
75
71
  .ToArray();
@@ -78,23 +74,7 @@
78
74
 
79
75
  private static void InitializeCacheIfNeeded()
80
76
  {
81
- if (CachedLabels.Count > 0)
82
- {
83
- return;
84
- }
85
-
86
- string[] guids = AssetDatabase.FindAssets("t:Sprite");
87
- foreach (string guid in guids)
88
- {
89
- string path = AssetDatabase.GUIDToAssetPath(guid);
90
- Object asset = AssetDatabase.LoadMainAssetAtPath(path);
91
- if (asset == null)
92
- {
93
- continue;
94
- }
95
-
96
- CachedLabels[path] = AssetDatabase.GetLabels(asset);
97
- }
77
+ _ = Helpers.GetAllSpriteLabelNames();
98
78
  }
99
79
 
100
80
  private static bool AreEqual(string[] a, string[] b)
@@ -2,15 +2,23 @@
2
2
  {
3
3
  #if UNITY_EDITOR
4
4
  using System;
5
+ using System.Collections.Generic;
5
6
  using System.Reflection;
6
7
  using Extensions;
7
8
  using UnityEditor;
8
9
  using UnityEngine;
9
10
  using Core.Attributes;
11
+ using Core.Extension;
12
+ using Core.Helper;
10
13
 
11
14
  [CustomPropertyDrawer(typeof(WShowIfAttribute))]
12
15
  public sealed class WShowIfPropertyDrawer : PropertyDrawer
13
16
  {
17
+ private static readonly Dictionary<
18
+ Type,
19
+ Dictionary<string, Func<object, object>>
20
+ > CachedFields = new();
21
+
14
22
  public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
15
23
  {
16
24
  return !ShouldShow(property) ? 0f : EditorGUI.GetPropertyHeight(property, label, true);
@@ -43,13 +51,38 @@
43
51
 
44
52
  // This might not be a unity object, so fall back to reflection
45
53
  object enclosingObject = property.GetEnclosingObject(out _);
46
- FieldInfo conditionField = enclosingObject
47
- ?.GetType()
48
- .GetField(
54
+ if (enclosingObject == null)
55
+ {
56
+ return true;
57
+ }
58
+
59
+ Type type = enclosingObject.GetType();
60
+ Dictionary<string, Func<object, object>> cachedFields = CachedFields.GetOrAdd(type);
61
+ if (
62
+ !cachedFields.TryGetValue(
63
+ showIf.conditionField,
64
+ out Func<object, object> accessor
65
+ )
66
+ )
67
+ {
68
+ FieldInfo field = type.GetField(
49
69
  showIf.conditionField,
50
70
  BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
51
71
  );
52
- object fieldValue = conditionField?.GetValue(enclosingObject);
72
+ if (field == null)
73
+ {
74
+ Debug.LogError(
75
+ $"Failed to find conditional field {showIf.conditionField} on {type.Name}!"
76
+ );
77
+ accessor = _ => null;
78
+ }
79
+ else
80
+ {
81
+ accessor = ReflectionHelpers.GetFieldGetter(field);
82
+ }
83
+ cachedFields[showIf.conditionField] = accessor;
84
+ }
85
+ object fieldValue = accessor(enclosingObject);
53
86
  if (fieldValue is bool maybeCondition)
54
87
  {
55
88
  return showIf.inverse ? !maybeCondition : maybeCondition;
@@ -6,6 +6,7 @@
6
6
  using UnityEngine;
7
7
  using UnityEditor;
8
8
  using System.Collections.Generic;
9
+ using Core.Extension;
9
10
  using Core.Helper;
10
11
 
11
12
  [CustomPropertyDrawer(typeof(SourceFolderEntry))]
@@ -324,8 +325,8 @@
324
325
  nameof(SourceFolderEntry.selectionMode)
325
326
  );
326
327
  SpriteSelectionMode modeValue = (SpriteSelectionMode)modeProp.intValue;
327
- bool useRegex = (modeValue & SpriteSelectionMode.Regex) != 0;
328
- bool useLabels = (modeValue & SpriteSelectionMode.Labels) != 0;
328
+ bool useRegex = modeValue.HasFlagNoAlloc(SpriteSelectionMode.Regex);
329
+ bool useLabels = modeValue.HasFlagNoAlloc(SpriteSelectionMode.Labels);
329
330
 
330
331
  if (useRegex)
331
332
  {
@@ -46,11 +46,11 @@
46
46
  if (string.Equals(fieldName, "Array", StringComparison.Ordinal))
47
47
  {
48
48
  // Move to "data[i]", no need to length-check, we're guarded above
49
-
50
49
  ++i;
50
+ fieldName = pathParts[i];
51
51
  if (
52
52
  !int.TryParse(
53
- pathParts[i]
53
+ fieldName
54
54
  .Replace("data[", string.Empty, StringComparison.Ordinal)
55
55
  .Replace("]", string.Empty, StringComparison.Ordinal),
56
56
  out int index
@@ -61,15 +61,20 @@
61
61
  fieldInfo = null;
62
62
  return null;
63
63
  }
64
+
64
65
  obj = GetElementAtIndex(obj, index);
65
66
  type = obj?.GetType();
67
+ UpdateField(fieldName, ref fieldInfo);
68
+
69
+ if (i == pathParts.Length - 2)
70
+ {
71
+ fieldName = pathParts[i + 1];
72
+ UpdateField(fieldName, ref fieldInfo);
73
+ }
66
74
  continue;
67
75
  }
68
76
 
69
- fieldInfo = type?.GetField(
70
- fieldName,
71
- BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
72
- );
77
+ UpdateField(fieldName, ref fieldInfo);
73
78
  if (fieldInfo == null)
74
79
  {
75
80
  return null;
@@ -83,7 +88,24 @@
83
88
  }
84
89
  }
85
90
 
91
+ if (fieldInfo == null)
92
+ {
93
+ UpdateField(property.name, ref fieldInfo);
94
+ }
95
+
86
96
  return obj;
97
+
98
+ void UpdateField(string fieldName, ref FieldInfo field)
99
+ {
100
+ FieldInfo newField = type?.GetField(
101
+ fieldName,
102
+ BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance
103
+ );
104
+ if (newField != null)
105
+ {
106
+ field = newField;
107
+ }
108
+ }
87
109
  }
88
110
 
89
111
  /// <summary>
@@ -87,14 +87,6 @@
87
87
  [Tooltip("Asset labels to include in the folders.")]
88
88
  [StringInList(typeof(Helpers), nameof(Helpers.GetAllSpriteLabelNames))]
89
89
  public List<string> labels = new();
90
-
91
- public SourceFolderEntry() { }
92
-
93
- public SourceFolderEntry(string path)
94
- {
95
- folderPath = path;
96
- regexes = new List<string>();
97
- }
98
90
  }
99
91
 
100
92
  [CreateAssetMenu(
@@ -45,11 +45,6 @@
45
45
  LoadAtlasConfigs();
46
46
  }
47
47
 
48
- private void OnFocus()
49
- {
50
- LoadAtlasConfigs();
51
- }
52
-
53
48
  private void OnProjectChange()
54
49
  {
55
50
  LoadAtlasConfigs();
@@ -68,6 +63,10 @@
68
63
  foreach (string guid in guids)
69
64
  {
70
65
  string path = AssetDatabase.GUIDToAssetPath(guid);
66
+ if (string.IsNullOrWhiteSpace(path))
67
+ {
68
+ continue;
69
+ }
71
70
  ScriptableSpriteAtlas config = AssetDatabase.LoadAssetAtPath<ScriptableSpriteAtlas>(
72
71
  path
73
72
  );
@@ -78,9 +77,9 @@
78
77
  {
79
78
  _scanResultsCache[config] = cachedResult;
80
79
  }
81
- else if (!_scanResultsCache.ContainsKey(config))
80
+ else
82
81
  {
83
- _scanResultsCache[config] = new ScanResult();
82
+ _scanResultsCache.TryAdd(config, _ => new ScanResult());
84
83
  }
85
84
  _serializedConfigs.TryAdd(config, newConfig => new SerializedObject(newConfig));
86
85
  _foldoutStates.TryAdd(config, true);
@@ -502,6 +501,7 @@
502
501
  }
503
502
  }
504
503
 
504
+ bool allMatch = matchesAllRegexesInEntry;
505
505
  bool matchesAllTagsInEntry = true;
506
506
  if (
507
507
  entry.selectionMode.HasFlagNoAlloc(SpriteSelectionMode.Labels)
@@ -538,28 +538,27 @@
538
538
  }
539
539
  }
540
540
  }
541
- }
542
541
 
543
- bool allMatch;
544
- switch (entry.regexAndTagLogic)
545
- {
546
- case SpriteSelectionBooleanLogic.And:
547
- {
548
- allMatch = matchesAllRegexesInEntry && matchesAllTagsInEntry;
549
- break;
550
- }
551
- case SpriteSelectionBooleanLogic.Or:
542
+ switch (entry.regexAndTagLogic)
552
543
  {
553
- allMatch = matchesAllRegexesInEntry || matchesAllTagsInEntry;
554
- break;
555
- }
556
- default:
557
- {
558
- throw new InvalidEnumArgumentException(
559
- nameof(entry.regexAndTagLogic),
560
- (int)entry.regexAndTagLogic,
561
- typeof(SpriteSelectionBooleanLogic)
562
- );
544
+ case SpriteSelectionBooleanLogic.And:
545
+ {
546
+ allMatch = matchesAllRegexesInEntry && matchesAllTagsInEntry;
547
+ break;
548
+ }
549
+ case SpriteSelectionBooleanLogic.Or:
550
+ {
551
+ allMatch = matchesAllRegexesInEntry || matchesAllTagsInEntry;
552
+ break;
553
+ }
554
+ default:
555
+ {
556
+ throw new InvalidEnumArgumentException(
557
+ nameof(entry.regexAndTagLogic),
558
+ (int)entry.regexAndTagLogic,
559
+ typeof(SpriteSelectionBooleanLogic)
560
+ );
561
+ }
563
562
  }
564
563
  }
565
564
 
@@ -636,6 +635,8 @@
636
635
  if (addedCount > 0)
637
636
  {
638
637
  so.ApplyModifiedProperties();
638
+ config.spritesToPack.SortByName();
639
+ EditorUtility.SetDirty(config);
639
640
  this.Log($"'{config.name}': Added {addedCount} sprites.");
640
641
  }
641
642
  else
@@ -1179,28 +1179,35 @@ namespace WallstopStudios.UnityHelpers.Editor.Sprites
1179
1179
 
1180
1180
  private void AddAnimationDefinition()
1181
1181
  {
1182
- AnimationDefinition newDef = new();
1182
+ AnimationDefinition newDefinition = new();
1183
1183
  if (_selectedSpriteSheet != null)
1184
1184
  {
1185
- newDef.Name = $"{_selectedSpriteSheet.name}_Anim_{_animationDefinitions.Count}";
1185
+ newDefinition.Name =
1186
+ $"{_selectedSpriteSheet.name}_Anim_{_animationDefinitions.Count}";
1186
1187
  }
1187
1188
  if (0 < _availableSprites.Count)
1188
1189
  {
1189
1190
  if (0 < _animationDefinitions.Count)
1190
1191
  {
1191
1192
  int nextStartIndex = _animationDefinitions[^1].EndSpriteIndex + 1;
1192
- if (_availableSprites.Count - 1 <= nextStartIndex)
1193
+ if (_availableSprites.Count - 1 < nextStartIndex)
1193
1194
  {
1194
1195
  nextStartIndex = 0;
1195
1196
  }
1196
- newDef.StartSpriteIndex = nextStartIndex;
1197
+ newDefinition.StartSpriteIndex = nextStartIndex;
1197
1198
  }
1198
1199
 
1199
- newDef.EndSpriteIndex = _availableSprites.Count - 1;
1200
+ newDefinition.EndSpriteIndex = _availableSprites.Count - 1;
1200
1201
  }
1201
- newDef.FrameRateCurve = AnimationCurve.Constant(0, 1, newDef.DefaultFrameRate);
1202
- _animationDefinitions.Add(newDef);
1203
- UpdateSpritesForDefinition(newDef);
1202
+ newDefinition.FrameRateCurve = AnimationCurve.Constant(
1203
+ 0,
1204
+ 1,
1205
+ newDefinition.DefaultFrameRate
1206
+ );
1207
+ _animationDefinitions.Add(newDefinition);
1208
+ _currentPreviewAnimDefIndex = _animationDefinitions.Count - 1;
1209
+ StartOrUpdateCurrentPreview(newDefinition);
1210
+ UpdateSpritesForDefinition(newDefinition);
1204
1211
  _animationDefinitionsListView.Rebuild();
1205
1212
  }
1206
1213
 
@@ -24,6 +24,10 @@
24
24
  StringComparer.Ordinal
25
25
  );
26
26
 
27
+ internal static readonly Dictionary<string, string[]> CachedLabels = new(
28
+ StringComparer.OrdinalIgnoreCase
29
+ );
30
+
27
31
  internal static string[] AllSpriteLabels;
28
32
 
29
33
  public static string[] GetAllSpriteLabelNames()
@@ -45,7 +49,9 @@
45
49
  continue;
46
50
  }
47
51
 
48
- allLabels.UnionWith(AssetDatabase.GetLabels(asset));
52
+ string[] labels = AssetDatabase.GetLabels(asset);
53
+ CachedLabels[path] = labels;
54
+ allLabels.UnionWith(labels);
49
55
  }
50
56
 
51
57
  AllSpriteLabels = allLabels.ToArray();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc76.7",
3
+ "version": "2.0.0-rc76.9",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -80,6 +80,8 @@
80
80
 
81
81
 
82
82
 
83
+
84
+
83
85
 
84
86
 
85
87