/
SimpleFieldDrawer.cs
68 lines (60 loc) · 2.57 KB
/
SimpleFieldDrawer.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
using System;
using System.Linq;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace OneLine {
internal class SimpleFieldDrawer : Drawer {
public float GetWeight(SerializedProperty property){
switch (property.propertyType){
case SerializedPropertyType.Boolean: {
return 0;
}
default: {
var weights = property.GetCustomAttributes<WeightAttribute>()
.Select(x => x.Weight)
.ToArray();
return weights.Length > 0 ? weights.Sum() : 1;
}
}
}
public float GetFixedWidth(SerializedProperty property){
switch (property.propertyType){
case SerializedPropertyType.Boolean: {
return EditorGUIUtility.singleLineHeight - 2;
}
default: {
return property.GetCustomAttributes<WidthAttribute>()
.Select(x => x.Width)
.Sum();
}
}
}
public override void AddSlices(SerializedProperty property, Slices slices){
highlight.Draw(property, slices);
slices.Add(new SliceImpl(GetWeight(property), GetFixedWidth(property), rect => Draw(rect, property.Copy())));
tooltip.Draw(property, slices);
}
public virtual void Draw(Rect rect, SerializedProperty property) {
DrawProperty(rect, property);
}
/*
* WORKAROUND
* Unity3d `feature`: EditorGUI.PropertyField draws field
* with all decorators (like Header, Space, etc) and this behaviour
* can not be ommited.
* see: http://answers.unity3d.com/questions/1394991/how-to-preserve-drawing-decoratordrawer-like-heade.html
* Headers and Separators (provided by one-line) produces artefacts.
* We solve this problem with reflection, but we call internal method
* and this may be dangerous: unity3d developers may change API =(
*/
private void DrawProperty(Rect rect, SerializedProperty property){
//EditorGUI.PropertyField(rect, property, GUIContent.none);
typeof(EditorGUI)
.GetMethod("DefaultPropertyField", BindingFlags.NonPublic | BindingFlags.Static)
.Invoke(null, new object[]{rect, property, GUIContent.none});
}
}
}