com.wallstop-studios.unity-helpers 2.0.0-rc41 → 2.0.0-rc43
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.
- package/Editor/AnimationCopier.cs +1 -1
- package/Editor/AnimationCreator.cs +1 -1
- package/Editor/{EnsureTextureSizeWizard.cs → FitTextureSizeWizard.cs} +47 -15
- package/Editor/PrefabCheckWizard.cs +30 -25
- package/Editor/SpriteSettingsApplier.cs +130 -26
- package/Editor/TextureSettingsApplier.cs +7 -0
- package/Editor/WShowIfPropertyDrawer.cs +63 -0
- package/Editor/WShowIfPropertyDrawer.cs.meta +3 -0
- package/Runtime/Core/Attributes/WShowIfAttribute.cs +16 -0
- package/Runtime/Core/Attributes/WShowIfAttribute.cs.meta +3 -0
- package/Runtime/Core/Extension/ColorExtensions.cs +378 -87
- package/Runtime/Core/Extension/SerializedPropertyExtensions.cs +157 -0
- package/Runtime/Core/Extension/SerializedPropertyExtensions.cs.meta +3 -0
- package/Runtime/Core/Helper/Objects.cs +4 -2
- package/Runtime/Core/Helper/Partials/TransformHelpers.cs +2 -6
- package/Runtime/Core/Helper/StringInList.cs +1 -1
- package/Runtime/Core/Threading/SingleThreadedThreadPool.cs +44 -35
- package/Runtime/Utils/Buffers.cs +2 -1
- package/package.json +1 -1
- /package/Editor/{EnsureTextureSizeWizard.cs.meta → FitTextureSizeWizard.cs.meta} +0 -0
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
public List<Object> animationSources = new();
|
|
26
26
|
public string text;
|
|
27
27
|
|
|
28
|
-
[MenuItem("Tools/Unity Helpers/Animation Creator")]
|
|
28
|
+
[MenuItem("Tools/Unity Helpers/Animation Creator", priority = -3)]
|
|
29
29
|
public static void CreateAnimation()
|
|
30
30
|
{
|
|
31
31
|
_ = DisplayWizard<AnimationCreator>("Animation Creator", "Create");
|
|
@@ -6,16 +6,24 @@
|
|
|
6
6
|
using UnityEditor;
|
|
7
7
|
using UnityEngine;
|
|
8
8
|
|
|
9
|
-
public
|
|
9
|
+
public enum FitMode
|
|
10
10
|
{
|
|
11
|
+
GrowAndShrink = 0,
|
|
12
|
+
GrowOnly = 1,
|
|
13
|
+
ShrinkOnly = 2,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public sealed class FitTextureSizeWizard : ScriptableWizard
|
|
17
|
+
{
|
|
18
|
+
public FitMode fitMode = FitMode.GrowAndShrink;
|
|
11
19
|
public List<Texture2D> textures = new();
|
|
12
20
|
|
|
13
21
|
public List<Object> textureSourcePaths = new();
|
|
14
22
|
|
|
15
|
-
[MenuItem("Tools/Unity Helpers/
|
|
23
|
+
[MenuItem("Tools/Unity Helpers/Fit Texture Size", priority = -1)]
|
|
16
24
|
public static void EnsureSizes()
|
|
17
25
|
{
|
|
18
|
-
_ = DisplayWizard<
|
|
26
|
+
_ = DisplayWizard<FitTextureSizeWizard>("Fit Texture Size", "Run");
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
private void OnWizardCreate()
|
|
@@ -68,36 +76,60 @@
|
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
int changedCount = 0;
|
|
71
|
-
foreach (Texture2D
|
|
79
|
+
foreach (Texture2D texture in textures)
|
|
72
80
|
{
|
|
73
|
-
Texture2D texture = inputTexture;
|
|
74
81
|
string assetPath = AssetDatabase.GetAssetPath(texture);
|
|
75
82
|
if (string.IsNullOrWhiteSpace(assetPath))
|
|
76
83
|
{
|
|
77
84
|
continue;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
|
-
TextureImporter
|
|
81
|
-
|
|
87
|
+
TextureImporter textureImporter =
|
|
88
|
+
AssetImporter.GetAtPath(assetPath) as TextureImporter;
|
|
89
|
+
if (textureImporter == null)
|
|
82
90
|
{
|
|
83
91
|
continue;
|
|
84
92
|
}
|
|
85
|
-
|
|
93
|
+
textureImporter.GetSourceTextureWidthAndHeight(out int width, out int height);
|
|
86
94
|
|
|
87
95
|
float size = Mathf.Max(width, height);
|
|
88
|
-
int textureSize =
|
|
96
|
+
int textureSize = textureImporter.maxTextureSize;
|
|
97
|
+
int originalTextureSize = textureSize;
|
|
89
98
|
bool changed = false;
|
|
90
|
-
|
|
99
|
+
if (fitMode is FitMode.GrowAndShrink or FitMode.GrowOnly)
|
|
91
100
|
{
|
|
92
|
-
|
|
93
|
-
|
|
101
|
+
while (textureSize < size)
|
|
102
|
+
{
|
|
103
|
+
changed = true;
|
|
104
|
+
textureSize <<= 1;
|
|
105
|
+
}
|
|
94
106
|
}
|
|
95
|
-
|
|
107
|
+
|
|
108
|
+
if (fitMode is FitMode.GrowAndShrink or FitMode.ShrinkOnly)
|
|
109
|
+
{
|
|
110
|
+
while (0 < textureSize && size <= (textureSize >> 1))
|
|
111
|
+
{
|
|
112
|
+
changed = true;
|
|
113
|
+
textureSize >>= 1;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
textureImporter.maxTextureSize = textureSize;
|
|
96
118
|
|
|
97
119
|
if (changed)
|
|
98
120
|
{
|
|
99
|
-
changedCount
|
|
100
|
-
|
|
121
|
+
++changedCount;
|
|
122
|
+
textureImporter.SaveAndReimport();
|
|
123
|
+
if (textureImporter.maxTextureSize != textureSize)
|
|
124
|
+
{
|
|
125
|
+
this.LogError(
|
|
126
|
+
$"Failed to update {texture.name}, need texture size {textureSize} but got {textureImporter.maxTextureSize}. Path: '{assetPath}'."
|
|
127
|
+
);
|
|
128
|
+
if (originalTextureSize != textureImporter.maxTextureSize)
|
|
129
|
+
{
|
|
130
|
+
--changedCount;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
101
133
|
}
|
|
102
134
|
}
|
|
103
135
|
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
)]
|
|
24
24
|
public List<Object> assetPaths = new();
|
|
25
25
|
|
|
26
|
-
[MenuItem("Tools/Unity Helpers/Prefab Check Wizard")]
|
|
26
|
+
[MenuItem("Tools/Unity Helpers/Prefab Check Wizard", priority = -1)]
|
|
27
27
|
public static void CreatePrefabCheckWizard()
|
|
28
28
|
{
|
|
29
29
|
_ = DisplayWizard<PrefabCheckWizard>("Prefab sanity check", "Run");
|
|
@@ -34,7 +34,10 @@
|
|
|
34
34
|
List<string> parsedAssetPaths;
|
|
35
35
|
if (assetPaths is { Count: > 0 })
|
|
36
36
|
{
|
|
37
|
-
parsedAssetPaths = assetPaths
|
|
37
|
+
parsedAssetPaths = assetPaths
|
|
38
|
+
.Where(Objects.NotNull)
|
|
39
|
+
.Select(AssetDatabase.GetAssetPath)
|
|
40
|
+
.ToList();
|
|
38
41
|
parsedAssetPaths.RemoveAll(string.IsNullOrEmpty);
|
|
39
42
|
if (parsedAssetPaths.Count <= 0)
|
|
40
43
|
{
|
|
@@ -102,29 +105,6 @@
|
|
|
102
105
|
)
|
|
103
106
|
)
|
|
104
107
|
{
|
|
105
|
-
bool LogIfNull(object thing, int? position = null)
|
|
106
|
-
{
|
|
107
|
-
if (thing == null || (thing is Object unityThing && !unityThing))
|
|
108
|
-
{
|
|
109
|
-
if (position == null)
|
|
110
|
-
{
|
|
111
|
-
component.LogError("Field {0} has a null element in it.", field.Name);
|
|
112
|
-
}
|
|
113
|
-
else
|
|
114
|
-
{
|
|
115
|
-
component.LogError(
|
|
116
|
-
"Field {0} has a null element at position {1}.",
|
|
117
|
-
field.Name,
|
|
118
|
-
position
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
108
|
object fieldValue = field.GetValue(component);
|
|
129
109
|
|
|
130
110
|
if (field.FieldType.IsArray)
|
|
@@ -158,6 +138,31 @@
|
|
|
158
138
|
{
|
|
159
139
|
_ = LogIfNull(thing, position++);
|
|
160
140
|
}
|
|
141
|
+
|
|
142
|
+
continue;
|
|
143
|
+
|
|
144
|
+
bool LogIfNull(object thing, int? position = null)
|
|
145
|
+
{
|
|
146
|
+
if (thing == null || (thing is Object unityThing && !unityThing))
|
|
147
|
+
{
|
|
148
|
+
if (position == null)
|
|
149
|
+
{
|
|
150
|
+
component.LogError("Field {0} has a null element in it.", field.Name);
|
|
151
|
+
}
|
|
152
|
+
else
|
|
153
|
+
{
|
|
154
|
+
component.LogError(
|
|
155
|
+
"Field {0} has a null element at position {1}.",
|
|
156
|
+
field.Name,
|
|
157
|
+
position
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
161
166
|
}
|
|
162
167
|
}
|
|
163
168
|
}
|
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
using System.Collections.Generic;
|
|
6
6
|
using System.IO;
|
|
7
7
|
using System.Linq;
|
|
8
|
+
using Core.Attributes;
|
|
8
9
|
using Core.Extension;
|
|
10
|
+
using Core.Helper;
|
|
9
11
|
using UnityEditor;
|
|
10
12
|
using UnityEngine;
|
|
11
13
|
using Object = UnityEngine.Object;
|
|
@@ -13,14 +15,53 @@
|
|
|
13
15
|
[Serializable]
|
|
14
16
|
public sealed class SpriteSettings
|
|
15
17
|
{
|
|
18
|
+
public bool applyPixelsPerUnit;
|
|
19
|
+
|
|
20
|
+
[WShowIf(nameof(applyPixelsPerUnit))]
|
|
16
21
|
public int pixelsPerUnit = 100;
|
|
22
|
+
|
|
23
|
+
public bool applyPivot;
|
|
24
|
+
|
|
25
|
+
[WShowIf(nameof(applyPivot))]
|
|
17
26
|
public Vector2 pivot = new(0.5f, 0.5f);
|
|
27
|
+
|
|
28
|
+
public bool applySpriteMode;
|
|
29
|
+
|
|
30
|
+
[WShowIf(nameof(applySpriteMode))]
|
|
18
31
|
public SpriteImportMode spriteMode = SpriteImportMode.Single;
|
|
19
|
-
|
|
32
|
+
|
|
33
|
+
public bool applyGenerateMipMaps;
|
|
34
|
+
|
|
35
|
+
[WShowIf(nameof(applyGenerateMipMaps))]
|
|
36
|
+
public bool generateMipMaps;
|
|
37
|
+
|
|
38
|
+
public bool applyAlphaIsTransparency;
|
|
39
|
+
|
|
40
|
+
[WShowIf(nameof(applyAlphaIsTransparency))]
|
|
41
|
+
public bool alphaIsTransparency = true;
|
|
42
|
+
|
|
43
|
+
public bool applyReadWriteEnabled;
|
|
44
|
+
|
|
45
|
+
[WShowIf(nameof(applyReadWriteEnabled))]
|
|
46
|
+
public bool readWriteEnabled = true;
|
|
47
|
+
|
|
48
|
+
public bool applyExtrudeEdges;
|
|
49
|
+
|
|
50
|
+
[WShowIf(nameof(applyExtrudeEdges))]
|
|
51
|
+
[Range(0, 32)]
|
|
52
|
+
public uint extrudeEdges = 1;
|
|
53
|
+
|
|
54
|
+
public bool applyWrapMode;
|
|
55
|
+
|
|
56
|
+
[WShowIf(nameof(applyWrapMode))]
|
|
20
57
|
public TextureWrapMode wrapMode = TextureWrapMode.Clamp;
|
|
21
|
-
|
|
58
|
+
|
|
59
|
+
public bool applyFilterMode;
|
|
60
|
+
|
|
61
|
+
[WShowIf(nameof(applyFilterMode))]
|
|
22
62
|
public FilterMode filterMode = FilterMode.Point;
|
|
23
|
-
|
|
63
|
+
|
|
64
|
+
public string name = string.Empty;
|
|
24
65
|
}
|
|
25
66
|
|
|
26
67
|
public sealed class SpriteSettingsApplier : ScriptableWizard
|
|
@@ -38,7 +79,7 @@
|
|
|
38
79
|
)]
|
|
39
80
|
public List<Object> directories = new();
|
|
40
81
|
|
|
41
|
-
[MenuItem("Tools/Unity Helpers/Sprite Settings Applier")]
|
|
82
|
+
[MenuItem("Tools/Unity Helpers/Sprite Settings Applier", priority = -2)]
|
|
42
83
|
public static void CreateAnimation()
|
|
43
84
|
{
|
|
44
85
|
_ = DisplayWizard<SpriteSettingsApplier>("Sprite Settings Directory Applier", "Set");
|
|
@@ -47,9 +88,13 @@
|
|
|
47
88
|
private void OnWizardCreate()
|
|
48
89
|
{
|
|
49
90
|
HashSet<string> uniqueDirectories = new();
|
|
50
|
-
foreach (
|
|
91
|
+
foreach (
|
|
92
|
+
string assetPath in directories
|
|
93
|
+
.Where(Objects.NotNull)
|
|
94
|
+
.Select(AssetDatabase.GetAssetPath)
|
|
95
|
+
.Where(assetPath => !string.IsNullOrWhiteSpace(assetPath))
|
|
96
|
+
)
|
|
51
97
|
{
|
|
52
|
-
string assetPath = AssetDatabase.GetAssetPath(directory);
|
|
53
98
|
if (Directory.Exists(assetPath))
|
|
54
99
|
{
|
|
55
100
|
_ = uniqueDirectories.Add(assetPath);
|
|
@@ -59,9 +104,8 @@
|
|
|
59
104
|
HashSet<string> processedSpritePaths = new();
|
|
60
105
|
Queue<string> directoriesToCheck = new(uniqueDirectories);
|
|
61
106
|
int spriteCount = 0;
|
|
62
|
-
while (
|
|
107
|
+
while (directoriesToCheck.TryDequeue(out string directoryPath))
|
|
63
108
|
{
|
|
64
|
-
string directoryPath = directoriesToCheck.Dequeue();
|
|
65
109
|
foreach (string fullFilePath in Directory.EnumerateFiles(directoryPath))
|
|
66
110
|
{
|
|
67
111
|
if (!spriteFileExtensions.Contains(Path.GetExtension(fullFilePath)))
|
|
@@ -102,14 +146,13 @@
|
|
|
102
146
|
}
|
|
103
147
|
}
|
|
104
148
|
|
|
105
|
-
foreach (
|
|
149
|
+
foreach (
|
|
150
|
+
string filePath in sprites
|
|
151
|
+
.Where(Objects.NotNull)
|
|
152
|
+
.Select(AssetDatabase.GetAssetPath)
|
|
153
|
+
.Where(Objects.NotNull)
|
|
154
|
+
)
|
|
106
155
|
{
|
|
107
|
-
if (sprite == null)
|
|
108
|
-
{
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
string filePath = AssetDatabase.GetAssetPath(sprite);
|
|
113
156
|
if (
|
|
114
157
|
processedSpritePaths.Add(Application.dataPath + filePath)
|
|
115
158
|
&& TryUpdateTextureSettings(filePath)
|
|
@@ -128,40 +171,101 @@
|
|
|
128
171
|
|
|
129
172
|
private bool TryUpdateTextureSettings(string filePath)
|
|
130
173
|
{
|
|
174
|
+
bool changed = false;
|
|
175
|
+
if (string.IsNullOrWhiteSpace(filePath))
|
|
176
|
+
{
|
|
177
|
+
return changed;
|
|
178
|
+
}
|
|
179
|
+
|
|
131
180
|
TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;
|
|
132
181
|
if (textureImporter == null)
|
|
133
182
|
{
|
|
134
|
-
return
|
|
183
|
+
return changed;
|
|
135
184
|
}
|
|
136
185
|
|
|
137
|
-
SpriteSettings spriteData = spriteSettings.
|
|
138
|
-
string.
|
|
186
|
+
SpriteSettings spriteData = spriteSettings.Find(settings =>
|
|
187
|
+
string.IsNullOrWhiteSpace(settings.name) || filePath.Contains(settings.name)
|
|
139
188
|
);
|
|
140
189
|
if (spriteData == null)
|
|
141
190
|
{
|
|
142
|
-
return
|
|
191
|
+
return changed;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (spriteData.applyPixelsPerUnit)
|
|
195
|
+
{
|
|
196
|
+
// ReSharper disable once CompareOfFloatsByEqualityOperator
|
|
197
|
+
changed |= textureImporter.spritePixelsPerUnit != spriteData.pixelsPerUnit;
|
|
198
|
+
textureImporter.spritePixelsPerUnit = spriteData.pixelsPerUnit;
|
|
143
199
|
}
|
|
144
200
|
|
|
145
|
-
|
|
146
|
-
|
|
201
|
+
if (spriteData.applyPivot)
|
|
202
|
+
{
|
|
203
|
+
changed |= textureImporter.spritePivot != spriteData.pivot;
|
|
204
|
+
textureImporter.spritePivot = spriteData.pivot;
|
|
205
|
+
}
|
|
147
206
|
|
|
207
|
+
if (spriteData.applyGenerateMipMaps)
|
|
208
|
+
{
|
|
209
|
+
changed |= textureImporter.mipmapEnabled != spriteData.generateMipMaps;
|
|
210
|
+
textureImporter.mipmapEnabled = spriteData.generateMipMaps;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
bool changedSettings = false;
|
|
148
214
|
TextureImporterSettings settings = new();
|
|
149
215
|
textureImporter.ReadTextureSettings(settings);
|
|
150
|
-
|
|
151
|
-
|
|
216
|
+
if (spriteData.applyPivot)
|
|
217
|
+
{
|
|
218
|
+
changedSettings |= settings.spriteAlignment != (int)SpriteAlignment.Custom;
|
|
219
|
+
settings.spriteAlignment = (int)SpriteAlignment.Custom;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (spriteData.applyAlphaIsTransparency)
|
|
223
|
+
{
|
|
224
|
+
changedSettings |= settings.alphaIsTransparency != spriteData.alphaIsTransparency;
|
|
225
|
+
settings.alphaIsTransparency = spriteData.alphaIsTransparency;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (spriteData.applyReadWriteEnabled)
|
|
229
|
+
{
|
|
230
|
+
changedSettings |= settings.readable != spriteData.readWriteEnabled;
|
|
231
|
+
settings.readable = spriteData.readWriteEnabled;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (spriteData.applySpriteMode)
|
|
235
|
+
{
|
|
236
|
+
changedSettings |= settings.spriteMode != (int)spriteData.spriteMode;
|
|
237
|
+
settings.spriteMode = (int)spriteData.spriteMode;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (spriteData.applyExtrudeEdges)
|
|
241
|
+
{
|
|
242
|
+
changedSettings |= settings.spriteExtrude != spriteData.extrudeEdges;
|
|
243
|
+
settings.spriteExtrude = spriteData.extrudeEdges;
|
|
244
|
+
}
|
|
245
|
+
|
|
152
246
|
if (spriteData.applyWrapMode)
|
|
153
247
|
{
|
|
248
|
+
changedSettings |= settings.wrapMode != spriteData.wrapMode;
|
|
154
249
|
settings.wrapMode = spriteData.wrapMode;
|
|
155
250
|
}
|
|
156
251
|
|
|
157
252
|
if (spriteData.applyFilterMode)
|
|
158
253
|
{
|
|
254
|
+
changedSettings |= settings.filterMode != spriteData.filterMode;
|
|
159
255
|
settings.filterMode = spriteData.filterMode;
|
|
160
256
|
}
|
|
161
257
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
258
|
+
if (changedSettings)
|
|
259
|
+
{
|
|
260
|
+
textureImporter.SetTextureSettings(settings);
|
|
261
|
+
}
|
|
262
|
+
changed |= changedSettings;
|
|
263
|
+
if (changed)
|
|
264
|
+
{
|
|
265
|
+
textureImporter.SaveAndReimport();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return changed;
|
|
165
269
|
}
|
|
166
270
|
}
|
|
167
271
|
#endif
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
using System.Collections.Generic;
|
|
6
6
|
using System.IO;
|
|
7
7
|
using System.Linq;
|
|
8
|
+
using Core.Attributes;
|
|
8
9
|
using Core.Extension;
|
|
9
10
|
using UnityEditor;
|
|
10
11
|
using UnityEngine;
|
|
@@ -18,9 +19,15 @@
|
|
|
18
19
|
public bool applyMipMaps = false;
|
|
19
20
|
public bool generateMipMaps = false;
|
|
20
21
|
public bool applyWrapMode = false;
|
|
22
|
+
|
|
23
|
+
[WShowIf(nameof(applyWrapMode))]
|
|
21
24
|
public TextureWrapMode wrapMode = TextureWrapMode.Clamp;
|
|
25
|
+
|
|
22
26
|
public bool applyFilterMode = false;
|
|
27
|
+
|
|
28
|
+
[WShowIf(nameof(applyFilterMode))]
|
|
23
29
|
public FilterMode filterMode = FilterMode.Trilinear;
|
|
30
|
+
|
|
24
31
|
public TextureImporterCompression compression = TextureImporterCompression.CompressedHQ;
|
|
25
32
|
public bool useCrunchCompression = true;
|
|
26
33
|
public TextureResizeAlgorithm textureResizeAlgorithm = TextureResizeAlgorithm.Bilinear;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
namespace UnityHelpers.Editor
|
|
2
|
+
{
|
|
3
|
+
#if UNITY_EDITOR
|
|
4
|
+
using System.Reflection;
|
|
5
|
+
using Core.Attributes;
|
|
6
|
+
using Core.Extension;
|
|
7
|
+
using UnityEngine;
|
|
8
|
+
using UnityEditor;
|
|
9
|
+
|
|
10
|
+
[CustomPropertyDrawer(typeof(WShowIfAttribute))]
|
|
11
|
+
public sealed class WShowIfPropertyDrawer : PropertyDrawer
|
|
12
|
+
{
|
|
13
|
+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
|
|
14
|
+
{
|
|
15
|
+
return !ShouldShow(property) ? 0f : EditorGUI.GetPropertyHeight(property, label, true);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
19
|
+
{
|
|
20
|
+
if (ShouldShow(property))
|
|
21
|
+
{
|
|
22
|
+
EditorGUI.PropertyField(position, property, label, true);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private bool ShouldShow(SerializedProperty property)
|
|
27
|
+
{
|
|
28
|
+
if (attribute is not WShowIfAttribute showIf)
|
|
29
|
+
{
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
SerializedProperty conditionProperty = property.serializedObject.FindProperty(
|
|
34
|
+
showIf.conditionField
|
|
35
|
+
);
|
|
36
|
+
if (conditionProperty is not { propertyType: SerializedPropertyType.Boolean })
|
|
37
|
+
{
|
|
38
|
+
if (conditionProperty != null)
|
|
39
|
+
{
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// This might not be a unity object, so fall back to reflection
|
|
44
|
+
object enclosingObject = property.GetEnclosingObject(out _);
|
|
45
|
+
FieldInfo conditionField = enclosingObject
|
|
46
|
+
?.GetType()
|
|
47
|
+
.GetField(
|
|
48
|
+
showIf.conditionField,
|
|
49
|
+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
|
50
|
+
);
|
|
51
|
+
if (conditionField?.GetValue(enclosingObject) is bool maybeCondition)
|
|
52
|
+
{
|
|
53
|
+
return showIf.inverse ? !maybeCondition : maybeCondition;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
bool condition = conditionProperty.boolValue;
|
|
59
|
+
return showIf.inverse ? !condition : condition;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
#endif
|
|
63
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
namespace UnityHelpers.Core.Attributes
|
|
2
|
+
{
|
|
3
|
+
using UnityEngine;
|
|
4
|
+
|
|
5
|
+
public sealed class WShowIfAttribute : PropertyAttribute
|
|
6
|
+
{
|
|
7
|
+
public readonly string conditionField;
|
|
8
|
+
public readonly bool inverse;
|
|
9
|
+
|
|
10
|
+
public WShowIfAttribute(string conditionField, bool inverse = false)
|
|
11
|
+
{
|
|
12
|
+
this.conditionField = conditionField;
|
|
13
|
+
this.inverse = inverse;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|