Skip to content

Commit 84c985c

Browse files
committed
Some refactoring to ensure coding standard
1 parent 8e23635 commit 84c985c

File tree

12 files changed

+89
-41
lines changed

12 files changed

+89
-41
lines changed

Samples/DemoScene/DemoScene.unity

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,24 @@ MonoBehaviour:
200200
m_EditorClassIdentifier:
201201
enable1: 0
202202
enable2: 0
203+
enum1: 0
203204
enableIfAll:
204205
enableIfAny:
206+
enableIfEnum:
205207
nest1:
206208
enable1: 1
207209
enable2: 0
210+
enum1: 0
208211
enableIfAll: 1
209212
enableIfAny: 2
213+
enableIfEnum: 0
210214
nest2:
211215
enable1: 1
212216
enable2: 1
217+
enum1: 0
213218
enableIfAll: {x: 0.25, y: 0.75}
214219
enableIfAny: {x: 0.25, y: 0.75}
220+
enableIfEnum: {x: 0.25, y: 0.75}
215221
--- !u!114 &114650326
216222
MonoBehaviour:
217223
m_ObjectHideFlags: 0
@@ -226,18 +232,24 @@ MonoBehaviour:
226232
m_EditorClassIdentifier:
227233
disable1: 0
228234
disable2: 0
235+
enum1: 0
229236
disableIfAll:
230237
disableIfAny:
238+
disableIfEnum:
231239
nest1:
232240
disable1: 1
233241
disable2: 0
242+
enum1: 0
234243
disableIfAll: 1
235244
disableIfAny: 2
245+
disableIfEnum: 3
236246
nest2:
237247
disable1: 1
238248
disable2: 1
249+
enum1: 0
239250
enableIfAll: {x: 0.25, y: 0.75}
240251
enableIfAny: {x: 0.25, y: 0.75}
252+
enableIfEnum: {x: 0.25, y: 0.75}
241253
--- !u!1 &155697335
242254
GameObject:
243255
m_ObjectHideFlags: 0
@@ -1387,18 +1399,24 @@ MonoBehaviour:
13871399
m_EditorClassIdentifier:
13881400
show1: 0
13891401
show2: 0
1402+
enum1: 0
13901403
showIfAll:
13911404
showIfAny:
1405+
showIfEnum:
13921406
nest1:
13931407
show1: 1
13941408
show2: 0
1409+
enum1: 0
13951410
showIfAll: 0
13961411
showIfAny: 0
1412+
showIfEnum: 0
13971413
nest2:
13981414
show1: 1
13991415
show2: 1
1416+
enum1: 0
14001417
showIfAll: {x: 0.25, y: 0.75}
14011418
showIfAny: {x: 0.25, y: 0.75}
1419+
showIfEnum: {x: 0.25, y: 0.75}
14021420
--- !u!114 &1524906393
14031421
MonoBehaviour:
14041422
m_ObjectHideFlags: 0
@@ -1413,18 +1431,24 @@ MonoBehaviour:
14131431
m_EditorClassIdentifier:
14141432
hide1: 0
14151433
hide2: 0
1434+
enum1: 0
14161435
hideIfAll:
14171436
hideIfAny:
1437+
hideIfEnum:
14181438
nest1:
14191439
hide1: 1
14201440
hide2: 0
1441+
enum1: 0
14211442
hideIfAll: 0
14221443
hideIfAny: 0
1444+
hideIfEnum: 0
14231445
nest2:
14241446
hide1: 1
14251447
hide2: 1
1448+
enum1: 0
14261449
hideIfAll: {x: 0.25, y: 0.75}
14271450
hideIfAny: {x: 0.25, y: 0.75}
1451+
hideIfEnum: {x: 0.25, y: 0.75}
14281452
--- !u!1 &1552114399
14291453
GameObject:
14301454
m_ObjectHideFlags: 0

Scripts/Core/MetaAttributes/DisableIfAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public DisableIfAttribute(EConditionOperator conditionOperator, params string[]
1717
Inverted = true;
1818
}
1919

20-
public DisableIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
20+
public DisableIfAttribute(string enumName, object enumValue)
21+
: base(enumName, enumValue as Enum)
2122
{
2223
Inverted = true;
2324
}

Scripts/Core/MetaAttributes/EnableIfAttribute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public EnableIfAttribute(EConditionOperator conditionOperator, params string[] c
1616
{
1717
Inverted = false;
1818
}
19-
20-
public EnableIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
19+
20+
public EnableIfAttribute(string enumName, object enumValue)
21+
: base(enumName, enumValue as Enum)
2122
{
2223
Inverted = false;
2324
}

Scripts/Core/MetaAttributes/EnableIfAttributeBase.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public abstract class EnableIfAttributeBase : MetaAttribute
77
public string[] Conditions { get; private set; }
88
public EConditionOperator ConditionOperator { get; private set; }
99
public bool Inverted { get; protected set; }
10-
10+
1111
/// <summary>
1212
/// If this not null, <see cref="Conditions"/>[0] is name of an enum variable.
1313
/// </summary>
@@ -25,10 +25,14 @@ public EnableIfAttributeBase(EConditionOperator conditionOperator, params string
2525
Conditions = conditions;
2626
}
2727

28-
public EnableIfAttributeBase(string enumName, Enum enumValue) : this(enumName)
28+
public EnableIfAttributeBase(string enumName, Enum enumValue)
29+
: this(enumName)
2930
{
30-
if (enumName is null)
31+
if (enumValue == null)
32+
{
3133
throw new ArgumentNullException(nameof(enumValue), "This parameter must be an enum value.");
34+
}
35+
3236
EnumValue = enumValue;
3337
}
3438
}

Scripts/Core/MetaAttributes/HideIfAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public HideIfAttribute(EConditionOperator conditionOperator, params string[] con
1717
Inverted = true;
1818
}
1919

20-
public HideIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
20+
public HideIfAttribute(string enumName, object enumValue)
21+
: base(enumName, enumValue as Enum)
2122
{
2223
Inverted = true;
2324
}

Scripts/Core/MetaAttributes/ShowIfAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public ShowIfAttribute(EConditionOperator conditionOperator, params string[] con
1717
Inverted = false;
1818
}
1919

20-
public ShowIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
20+
public ShowIfAttribute(string enumName, object enumValue)
21+
: base(enumName, enumValue as Enum)
2122
{
2223
Inverted = false;
2324
}

Scripts/Core/MetaAttributes/ShowIfAttributeBase.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class ShowIfAttributeBase : MetaAttribute
77
public string[] Conditions { get; private set; }
88
public EConditionOperator ConditionOperator { get; private set; }
99
public bool Inverted { get; protected set; }
10-
10+
1111
/// <summary>
1212
/// If this not null, <see cref="Conditions"/>[0] is name of an enum variable.
1313
/// </summary>
@@ -25,10 +25,14 @@ public ShowIfAttributeBase(EConditionOperator conditionOperator, params string[]
2525
Conditions = conditions;
2626
}
2727

28-
public ShowIfAttributeBase(string enumName, Enum enumValue) : this(enumName)
28+
public ShowIfAttributeBase(string enumName, Enum enumValue)
29+
: this(enumName)
2930
{
30-
if (enumName is null)
31+
if (enumValue == null)
32+
{
3133
throw new ArgumentNullException(nameof(enumValue), "This parameter must be an enum value.");
34+
}
35+
3236
EnumValue = enumValue;
3337
}
3438
}

Scripts/Editor/Utility/PropertyUtility.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ public static bool IsEnabled(SerializedProperty property)
8888
if (enableIfAttribute.EnumValue != null)
8989
{
9090
Enum value = GetEnumValue(target, enableIfAttribute.Conditions[0]);
91-
if (value != null) return enableIfAttribute.EnumValue.Equals(value) != enableIfAttribute.Inverted;
92-
91+
if (value != null)
92+
{
93+
return enableIfAttribute.EnumValue.Equals(value) != enableIfAttribute.Inverted;
94+
}
95+
9396
string message = enableIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work";
9497
Debug.LogWarning(message, property.serializedObject.targetObject);
98+
9599
return false;
96100
}
97101

102+
// deal with normal conditions
98103
List<bool> conditionValues = GetConditionValues(target, enableIfAttribute.Conditions);
99104
if (conditionValues.Count > 0)
100105
{
@@ -124,13 +129,18 @@ public static bool IsVisible(SerializedProperty property)
124129
if (showIfAttribute.EnumValue != null)
125130
{
126131
Enum value = GetEnumValue(target, showIfAttribute.Conditions[0]);
127-
if (value != null) return showIfAttribute.EnumValue.Equals(value) != showIfAttribute.Inverted;
128-
132+
if (value != null)
133+
{
134+
return showIfAttribute.EnumValue.Equals(value) != showIfAttribute.Inverted;
135+
}
136+
129137
string message = showIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work";
130138
Debug.LogWarning(message, property.serializedObject.targetObject);
139+
131140
return false;
132141
}
133142

143+
// deal with normal conditions
134144
List<bool> conditionValues = GetConditionValues(target, showIfAttribute.Conditions);
135145
if (conditionValues.Count > 0)
136146
{
@@ -156,15 +166,21 @@ internal static Enum GetEnumValue(object target, string enumName)
156166
{
157167
FieldInfo enumField = ReflectionUtility.GetField(target, enumName);
158168
if (enumField != null && enumField.FieldType.IsSubclassOf(typeof(Enum)))
159-
return (Enum) enumField.GetValue(target);
160-
169+
{
170+
return (Enum)enumField.GetValue(target);
171+
}
172+
161173
PropertyInfo enumProperty = ReflectionUtility.GetProperty(target, enumName);
162174
if (enumProperty != null && enumProperty.PropertyType.IsSubclassOf(typeof(Enum)))
163-
return (Enum) enumProperty.GetValue(target);
164-
175+
{
176+
return (Enum)enumProperty.GetValue(target);
177+
}
178+
165179
MethodInfo enumMethod = ReflectionUtility.GetMethod(target, enumName);
166180
if (enumMethod != null && enumMethod.ReturnType.IsSubclassOf(typeof(Enum)))
167-
return (Enum) enumMethod.Invoke(target, null);
181+
{
182+
return (Enum)enumMethod.Invoke(target, null);
183+
}
168184

169185
return null;
170186
}

Scripts/Test/DisableIfTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class DisableIfNest1
4242
public int disableIfAny = 2;
4343

4444
[DisableIf("Enum1", DisableIfEnum.Case1)]
45-
[AllowNesting]
45+
[AllowNesting] // Because it's nested we need to explicitly allow nesting
4646
public int disableIfEnum = 3;
4747

4848
public DisableIfNest2 nest2;
@@ -65,7 +65,7 @@ public class DisableIfNest2
6565
[DisableIf(EConditionOperator.Or, "GetDisable1", "GetDisable2")]
6666
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
6767
public Vector2 enableIfAny = new Vector2(0.25f, 0.75f);
68-
68+
6969
[DisableIf("GetEnum1", DisableIfEnum.Case2)]
7070
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
7171
public Vector2 enableIfEnum = new Vector2(0.25f, 0.75f);
@@ -76,7 +76,6 @@ public enum DisableIfEnum
7676
{
7777
Case0,
7878
Case1,
79-
Case2,
80-
Case3
79+
Case2
8180
}
8281
}

Scripts/Test/EnableIfTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class EnableIfNest1
4242
public int enableIfAny;
4343

4444
[EnableIf("Enum1", EnableIfEnum.Case1)]
45-
[AllowNesting]
45+
[AllowNesting] // Because it's nested we need to explicitly allow nesting
4646
public int enableIfEnum;
4747

4848
public EnableIfNest2 nest2;
@@ -65,7 +65,7 @@ public class EnableIfNest2
6565
[EnableIf(EConditionOperator.Or, "GetEnable1", "GetEnable2")]
6666
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
6767
public Vector2 enableIfAny = new Vector2(0.25f, 0.75f);
68-
68+
6969
[EnableIf("GetEnum1", EnableIfEnum.Case2)]
7070
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
7171
public Vector2 enableIfEnum = new Vector2(0.25f, 0.75f);
@@ -76,7 +76,6 @@ public enum EnableIfEnum
7676
{
7777
Case0,
7878
Case1,
79-
Case2,
80-
Case3,
79+
Case2
8180
}
8281
}

0 commit comments

Comments
 (0)