forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PositiveValueOnlyAttribute.cs
161 lines (136 loc) · 3.79 KB
/
PositiveValueOnlyAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ----------------------------------------------------------------------------
// Author: Kaynn, Yeo Wen Qin
// https://github.com/Kaynn-Cahya
// Date: 17/02/2019
// ----------------------------------------------------------------------------
using UnityEngine;
namespace MyBox
{
public class PositiveValueOnlyAttribute : PropertyAttribute
{
}
}
#if UNITY_EDITOR
namespace MyBox.Internal
{
using UnityEditor;
using EditorTools;
[CustomPropertyDrawer(typeof(PositiveValueOnlyAttribute))]
public class PositiveValueOnlyAttributeDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!property.IsNumerical())
{
MyGUI.DrawColouredRect(position, MyGUI.Colors.Red);
EditorGUI.LabelField(position, new GUIContent("", "[PositiveValueOnly] used with non-numeric property"));
}
else
{
if (HandleNegativeValues(property)) property.serializedObject.ApplyModifiedProperties();
}
EditorGUI.PropertyField(position, property, label, true);
}
/// <summary>
/// Set number value to 0 if less than 0
/// </summary>
/// <returns>true if fixed</returns>
private bool HandleNegativeValues(SerializedProperty property)
{
switch (property.propertyType)
{
case SerializedPropertyType.Float:
case SerializedPropertyType.Integer:
return HandleNumerics(property);
case SerializedPropertyType.Vector2:
case SerializedPropertyType.Vector3:
case SerializedPropertyType.Vector4:
return HandleVectors(property);
case SerializedPropertyType.Vector2Int:
case SerializedPropertyType.Vector3Int:
return HandleIntVectors(property);
}
return false;
}
private bool HandleNumerics(SerializedProperty property)
{
if (property.propertyType == SerializedPropertyType.Integer && property.intValue < 0)
{
property.intValue = 0;
return true;
}
if (property.propertyType == SerializedPropertyType.Float && property.floatValue < 0)
{
property.floatValue = 0;
return true;
}
return false;
}
private bool HandleVectors(SerializedProperty property)
{
Vector4 vector = Vector4.zero;
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
vector = property.vector2Value;
break;
case SerializedPropertyType.Vector3:
vector = property.vector3Value;
break;
case SerializedPropertyType.Vector4:
vector = property.vector4Value;
break;
}
bool handled = false;
for (int i = 0; i < 4; ++i)
{
if (vector[i] < 0f)
{
vector[i] = 0;
handled = true;
}
}
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
property.vector2Value = vector;
break;
case SerializedPropertyType.Vector3:
property.vector3Value = vector;
break;
case SerializedPropertyType.Vector4:
property.vector4Value = vector;
break;
}
return handled;
}
private bool HandleIntVectors(SerializedProperty property)
{
if (property.propertyType == SerializedPropertyType.Vector2Int)
{
var vector = property.vector2IntValue;
if (vector.x > 0 && vector.y > 0) return false;
property.vector2IntValue = new Vector2Int(
vector.x < 0 ? 0 : vector.x,
vector.y < 0 ? 0 : vector.y);
return true;
}
if (property.propertyType == SerializedPropertyType.Vector3Int)
{
var vector = property.vector3IntValue;
if (vector.x > 0 && vector.y > 0 && vector.z > 0) return false;
property.vector3Value = new Vector3(
vector.x < 0 ? 0 : vector.x,
vector.y < 0 ? 0 : vector.y,
vector.z < 0 ? 0 : vector.z);
return true;
}
return false;
}
}
}
#endif