-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHexDrawer.cs
70 lines (58 loc) · 1.9 KB
/
HexDrawer.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
using System;
using System.Globalization;
using cmdwtf.UnityTools.Attributes;
using UnityEditor;
using UnityEngine;
namespace cmdwtf.UnityTools.Editor
{
[UnityEditor.CustomPropertyDrawer(typeof(HexAttribute))]
public class HexDrawer : CustomPropertyDrawer
{
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
var hexAttribute = (HexAttribute)attribute;
EditorGUI.BeginProperty(rect, label, property);
switch (property.propertyType)
{
case SerializedPropertyType.Integer:
DrawPropertyForInt(rect, property, label, hexAttribute);
break;
default:
EditorGUI.LabelField(rect, $"{label.text}: Unsupported type - {property.propertyType}");
break;
}
EditorGUI.EndProperty();
}
private static void DrawPropertyForInt(Rect rect, SerializedProperty property, GUIContent label, HexAttribute hexAttribute)
{
int width = hexAttribute.MinimumDisplayWidth;
if (width < 0)
{
width = hexAttribute.GetDefaultWidthForType(property.type);
}
string stringValue = width <= 0
? $"0x{property.longValue:X}"
: "0x" + property.longValue.ToString($"X{width}");
stringValue = EditorGUI.TextField(rect, label, stringValue).ToLower();
if (stringValue.StartsWith("0x"))
{
// strip the 0x from the beginning, then parse as hex.
string no0X = stringValue.Remove(0, 2);
if (long.TryParse(no0X, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out long resultHex))
{
property.longValue = resultHex;
}
}
else if (long.TryParse(stringValue, NumberStyles.Any, CultureInfo.CurrentCulture, out long resultAny))
{
// no 0x, parse the number as decimal
property.longValue = resultAny;
}
else if (string.IsNullOrWhiteSpace(stringValue))
{
// no value, default to zero.
property.longValue = 0;
}
}
}
}