Skip to content

Commit 079c395

Browse files
author
Thomas Hummes
committed
Refactored some attribute classes and added extended property type validation
1 parent f6bea66 commit 079c395

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

Attributes/LimitAttribute.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@ public class LimitAttribute : PropertyAttribute
77
{
88
public enum Mode { LimitLower, LimitUpper, LimitBoth }
99

10-
public Mode LimitMode;
10+
private readonly Mode _limitMode;
1111

12-
public int LowerLimit;
13-
public int UpperLimit;
12+
private readonly int _lowerLimit;
13+
private readonly int _upperLimit;
1414

1515
public LimitAttribute(int lowerLimit) : this(Mode.LimitLower, lowerLimit, int.MaxValue) { }
1616

1717
public LimitAttribute(int lowerLimit, int upperLimit) : this(Mode.LimitLower, lowerLimit, upperLimit) { }
1818

19-
public LimitAttribute(Mode mode, int lowerLimit, int upperLimit)
19+
private LimitAttribute(Mode mode, int lowerLimit, int upperLimit)
2020
{
21-
LimitMode = mode;
22-
LowerLimit = lowerLimit;
23-
UpperLimit = upperLimit;
21+
_limitMode = mode;
22+
_lowerLimit = lowerLimit;
23+
_upperLimit = upperLimit;
2424
}
2525

2626
public int Limit(int value)
2727
{
28-
switch (LimitMode)
28+
switch (_limitMode)
2929
{
3030
case Mode.LimitLower:
31-
return Mathf.Clamp(value, LowerLimit, int.MaxValue);
31+
return Mathf.Clamp(value, _lowerLimit, int.MaxValue);
3232
case Mode.LimitUpper:
33-
return Mathf.Clamp(value, int.MinValue, UpperLimit);
33+
return Mathf.Clamp(value, int.MinValue, _upperLimit);
3434
case Mode.LimitBoth:
35-
return Mathf.Clamp(value, LowerLimit, UpperLimit);
35+
return Mathf.Clamp(value, _lowerLimit, _upperLimit);
3636
default:
3737
throw new ArgumentOutOfRangeException();
3838
}

Editor/PropertyDrawer/LayerPropertyDrawer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class LayerPropertyDrawer : PropertyDrawer
88
{
99
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1010
{
11+
if (property.propertyType != SerializedPropertyType.Integer)
12+
{
13+
Debug.LogWarning("LayerAttribute can only be applied on integer properties/fields");
14+
return;
15+
}
16+
1117
property.intValue = EditorGUI.LayerField(position, property.name, property.intValue);
1218
}
1319
}

Editor/PropertyDrawer/LimitPropertyDrawer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class LimitPropertyDrawer : PropertyDrawer
88
{
99
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1010
{
11+
if (property.propertyType != SerializedPropertyType.Integer)
12+
{
13+
Debug.LogWarning("LimitAttribute can only be applied on integer properties/fields");
14+
return;
15+
}
16+
1117
LimitAttribute limiter = attribute as LimitAttribute;
1218
property.intValue = limiter.Limit(EditorGUI.IntField(position, property.name, property.intValue));
1319
}

Editor/PropertyDrawer/SortingLayerPropertyDrawer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
1212
{
1313
if (property.propertyType != SerializedPropertyType.Integer)
1414
{
15-
// Integer is expected. Everything else is ignored.
15+
Debug.LogWarning("SortingLayerAttributes can only be applied on integer properties/fields");
1616
return;
1717
}
1818
EditorGUI.LabelField(position, label);

Editor/PropertyDrawer/TagPropertyDrawer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class TagPropertyDrawer : PropertyDrawer
88
{
99
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1010
{
11+
if (property.propertyType != SerializedPropertyType.String)
12+
{
13+
Debug.LogWarning("TagAttribute can only be applied on string properties/fields");
14+
return;
15+
}
16+
1117
property.stringValue = EditorGUI.TagField(position, property.name, property.stringValue);
1218
}
1319
}

0 commit comments

Comments
 (0)