MinMax.cs
2.42 KB
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
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections.Generic;
namespace Leap.Unity.Attributes {
public class MinMax : CombinablePropertyAttribute, IFullPropertyDrawer {
public const float PERCENT_NUM = 0.2f;
public const float SPACING = 3;
public readonly float min, max;
public readonly bool isInt;
public MinMax(float min, float max) {
this.min = min;
this.max = max;
isInt = false;
}
public MinMax(int min, int max) {
this.min = min;
this.max = max;
isInt = true;
}
#if UNITY_EDITOR
public void DrawProperty(Rect rect, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty(rect, label, property);
Vector2 value = property.vector2Value;
rect = EditorGUI.PrefixLabel(rect, GUIUtility.GetControlID(FocusType.Passive), label);
int prevIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
float w = rect.width * PERCENT_NUM;
Rect leftNum = new Rect(rect.x, rect.y, w, rect.height);
Rect slider = new Rect(rect.x + w + SPACING, rect.y, rect.width - 2 * w - SPACING * 2, rect.height);
Rect rightNum = new Rect(rect.x + rect.width - w, rect.y, w, rect.height);
float newMin = EditorGUI.FloatField(leftNum, value.x);
float newMax = EditorGUI.FloatField(rightNum, value.y);
value.x = Mathf.Clamp(newMin, min, value.y);
value.y = Mathf.Clamp(newMax, value.x, max);
EditorGUI.MinMaxSlider(slider, ref value.x, ref value.y, min, max);
property.vector2Value = value;
EditorGUI.EndProperty();
EditorGUI.indentLevel = prevIndent;
}
public override IEnumerable<SerializedPropertyType> SupportedTypes {
get {
yield return SerializedPropertyType.Vector2;
}
}
#endif
}
}