forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyDrawers.cs
More file actions
136 lines (115 loc) · 5.78 KB
/
Copy pathPropertyDrawers.cs
File metadata and controls
136 lines (115 loc) · 5.78 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
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
namespace UnityEditor
{
// Built-in PropertyDrawers. See matching attributes in PropertyAttribute.cs
[CustomPropertyDrawer(typeof(RangeAttribute))]
internal sealed class RangeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
RangeAttribute range = (RangeAttribute)attribute;
if (property.propertyType == SerializedPropertyType.Float)
EditorGUI.Slider(position, property, range.min, range.max, label);
else if (property.propertyType == SerializedPropertyType.Integer)
EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, label);
else
EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
}
}
[CustomPropertyDrawer(typeof(MultilineAttribute))]
internal sealed class MultilineDrawer : PropertyDrawer
{
private const int kLineHeight = 13;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.String)
{
label = EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.MultiFieldPrefixLabel(position, 0, label, 1);
EditorGUI.BeginChangeCheck();
int oldIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0; // The MultiFieldPrefixLabel already applied indent, so avoid indent of TextArea itself.
string newValue = EditorGUI.TextArea(position, property.stringValue);
EditorGUI.indentLevel = oldIndent;
if (EditorGUI.EndChangeCheck())
property.stringValue = newValue;
EditorGUI.EndProperty();
}
else
EditorGUI.LabelField(position, label.text, "Use Multiline with string.");
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return (EditorGUIUtility.wideMode ? 0 : EditorGUI.kSingleLineHeight) // header
+ EditorGUI.kSingleLineHeight // first line
+ (((MultilineAttribute)attribute).lines - 1) * kLineHeight; // remaining lines
}
}
[CustomPropertyDrawer(typeof(TextAreaAttribute))]
internal sealed class TextAreaDrawer : PropertyDrawer
{
private const int kLineHeight = 13;
private Vector2 m_ScrollPosition = new Vector2();
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.String)
{
label = EditorGUI.BeginProperty(position, label, property);
Rect labelPosition = EditorGUI.IndentedRect(position);
labelPosition.height = EditorGUI.kSingleLineHeight;
position.yMin += labelPosition.height;
EditorGUI.HandlePrefixLabel(position, labelPosition, label);
EditorGUI.BeginChangeCheck();
string newValue = EditorGUI.ScrollableTextAreaInternal(position, property.stringValue, ref m_ScrollPosition, EditorStyles.textArea);
if (EditorGUI.EndChangeCheck())
property.stringValue = newValue;
EditorGUI.EndProperty();
}
else
EditorGUI.LabelField(position, label.text, "Use TextAreaDrawer with string.");
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
TextAreaAttribute textAreaAttribute = attribute as TextAreaAttribute;
string text = property.stringValue;
float fullTextHeight = EditorStyles.textArea.CalcHeight(GUIContent.Temp(text), EditorGUIUtility.contextWidth);
int lines = Mathf.CeilToInt(fullTextHeight / kLineHeight);
lines = Mathf.Clamp(lines, textAreaAttribute.minLines, textAreaAttribute.maxLines);
return EditorGUI.kSingleLineHeight // header
+ EditorGUI.kSingleLineHeight // first line
+ (lines - 1) * kLineHeight; // remaining lines
}
}
[CustomPropertyDrawer(typeof(ColorUsageAttribute))]
internal sealed class ColorUsageDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var colorUsage = (ColorUsageAttribute)attribute;
EditorGUI.BeginChangeCheck();
Color newColor = EditorGUI.ColorField(position, label, property.colorValue, true, colorUsage.showAlpha, colorUsage.hdr);
if (EditorGUI.EndChangeCheck())
{
property.colorValue = newColor;
}
}
}
[CustomPropertyDrawer(typeof(DelayedAttribute))]
internal sealed class DelayedDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.Float)
EditorGUI.DelayedFloatField(position, property, label);
else if (property.propertyType == SerializedPropertyType.Integer)
EditorGUI.DelayedIntField(position, property, label);
else if (property.propertyType == SerializedPropertyType.String)
EditorGUI.DelayedTextField(position, property, label);
else
EditorGUI.LabelField(position, label.text, "Use Delayed with float, int, or string.");
}
}
}