Skip to content

Commit 4c210f9

Browse files
authored
System types (#27)
* use nameof for accessing nullable struct fields in drawer * guid * timespan * datetime * datetime * udpate version * datetime drawer
1 parent bb186c2 commit 4c210f9

19 files changed

+267
-8
lines changed

Assets/Samples/ReadOnlySystemValues.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ public readonly struct Struct
99
public int A { get; }
1010
public float B { get; }
1111
public double? C { get; }
12+
public Guid? Guid { get; }
1213
}
1314

1415
public class ReadOnlySystemValues : MonoBehaviour
1516
{
1617
[AnySerialize] public int? NullableInt { get; }
1718
[AnySerialize] public float? NullableFloat { get; }
1819
[AnySerialize] public Struct? NullableStruct { get; }
20+
[AnySerialize] public Guid Guid { get; }
21+
[AnySerialize] public TimeSpan TimeSpan { get; }
22+
[AnySerialize] public DateTime DateTime { get; }
1923

2024
private void Awake()
2125
{

Assets/Samples/ReadOnlySystemValues.prefab

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ MonoBehaviour:
6363
_hasValue: 0
6464
_value:
6565
_value: 0
66+
_field3:
67+
_hasValue: 1
68+
_value:
69+
_guid: d417809a-259f-4070-ba5b-0f0ac76cfa5d
70+
<Guid>k__BackingField:
71+
_guid: e1abca74-8de3-467e-917f-2fb297041819
72+
<TimeSpan>k__BackingField:
73+
_displayUnit: 5
74+
_ticks: 315360000000000
75+
<DateTime>k__BackingField:
76+
_dateTimeKind: 2
77+
_ticks: 637989535461090000
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace AnySerialize.Editor
6+
{
7+
[CustomPropertyDrawer(typeof(AnyDateTime))]
8+
public class AnyDateTimeDrawer : PropertyDrawer
9+
{
10+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
11+
{
12+
return EditorGUIUtility.singleLineHeight;
13+
}
14+
15+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
16+
{
17+
var ticks = property.FindPropertyRelative(nameof(AnyDateTime._ticks));
18+
var kind = property.FindPropertyRelative(nameof(AnyDateTime._dateTimeKind));
19+
var kindEnum = (DateTimeKind)kind.enumValueIndex;
20+
var dateTime = new DateTime(ticks.longValue, kindEnum);
21+
22+
var labelPosition = new Rect(position.x, position.y, EditorGUIUtility.labelWidth, position.height);
23+
var propertyPosition = new Rect(labelPosition.width + 20, position.y, position.width, position.height);
24+
EditorGUI.LabelField(labelPosition, label);
25+
26+
var kindWidth = 50;
27+
propertyPosition.width = kindWidth;
28+
propertyPosition.x -= kindWidth;
29+
EditorGUI.PropertyField(propertyPosition, kind, new GUIContent());
30+
propertyPosition.x += kindWidth;
31+
32+
var year = IntField(dateTime.Year, "Y", 4, min: 1);
33+
var month = IntField(dateTime.Month, "M", 2, min: 1, max: 12);
34+
var day = IntField(dateTime.Day, "D", 2, min: 1, max: 31);
35+
var hour = IntField(dateTime.Hour, ":", 2, min: 0, max: 23);
36+
var minute = IntField(dateTime.Minute, ":", 2, min: 0, max: 59);
37+
var second = IntField(dateTime.Second, ".", 2, min: 0, max: 59);
38+
var millisecond = IntField(dateTime.Millisecond, "", 3, min: 0, max: 999);
39+
40+
propertyPosition.width = 40;
41+
if (GUI.Button(propertyPosition, "now"))
42+
{
43+
ticks.longValue = DateTime.Now.Ticks;
44+
kind.enumValueIndex = (int)DateTimeKind.Local;
45+
}
46+
else
47+
{
48+
ticks.longValue = new DateTime(year, month, day, hour, minute, second, millisecond, kindEnum).Ticks;
49+
}
50+
51+
int IntField(int number, string name, int digits, int min = 0, int max = int.MaxValue)
52+
{
53+
var singleNumberWidth = 10;
54+
var width = digits * singleNumberWidth;
55+
propertyPosition.width = width;
56+
number = EditorGUI.IntField(propertyPosition, number);
57+
number = Math.Clamp(number, min, max);
58+
propertyPosition.x += width;
59+
EditorGUI.LabelField(propertyPosition, name);
60+
propertyPosition.x += name.Length * singleNumberWidth;
61+
return number;
62+
}
63+
}
64+
}
65+
}

Packages/com.quabug.any-serialize/Editor/AnyDateTimeDrawer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace AnySerialize.Editor
6+
{
7+
[CustomPropertyDrawer(typeof(AnyGuid))]
8+
public class AnyGuidDrawer : PropertyDrawer
9+
{
10+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
11+
{
12+
return EditorGUIUtility.singleLineHeight;
13+
}
14+
15+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
16+
{
17+
property.NextVisible(enterChildren: true);
18+
var buttonWidth = 40;
19+
var propertyPosition = new Rect(position.x, position.y, position.width - buttonWidth - 20, position.height);
20+
var buttonPosition = new Rect(position.width - buttonWidth, position.y, buttonWidth, position.height);
21+
EditorGUI.PropertyField(propertyPosition, property, label);
22+
if (GUI.Button(buttonPosition, "new")) property.stringValue = Guid.NewGuid().ToString();
23+
}
24+
}
25+
}

Packages/com.quabug.any-serialize/Editor/AnyGuidDrawer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.quabug.any-serialize/Editor/AnyNullableStructDrawer.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using System;
2-
using UnityEditor;
1+
using UnityEditor;
32
using UnityEngine;
43

54
namespace AnySerialize.Editor
65
{
6+
using NullableStruct = ReadOnlyAnyNullableStruct<int,AnyValue<int>>;
7+
78
[CustomPropertyDrawer(typeof(ReadOnlyAnyNullableStruct<,>))]
89
public class AnyNullableStructDrawer : PropertyDrawer
910
{
1011
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
1112
{
12-
property = property.FindPropertyRelative("_value");
13+
property = property.FindPropertyRelative(nameof(NullableStruct._value));
1314
return EditorGUI.GetPropertyHeight(property, label, includeChildren: true);
1415
}
1516

1617
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1718
{
18-
var valueProperty = property.FindPropertyRelative("_value");
19-
var hasValueProperty = property.FindPropertyRelative("_hasValue");
19+
var valueProperty = property.FindPropertyRelative(nameof(NullableStruct._value));
20+
var hasValueProperty = property.FindPropertyRelative(nameof(NullableStruct._hasValue));
2021

2122
var toggleWidth = 20;
2223
var togglePosition = new Rect(position.width - EditorGUI.indentLevel * 15f, position.y, toggleWidth, EditorGUIUtility.singleLineHeight);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace AnySerialize.Editor
6+
{
7+
[CustomPropertyDrawer(typeof(AnyTimeSpan))]
8+
public class AnyTimeSpanDrawer : PropertyDrawer
9+
{
10+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
11+
{
12+
return EditorGUIUtility.singleLineHeight;
13+
}
14+
15+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
16+
{
17+
var ticks = property.FindPropertyRelative(nameof(AnyTimeSpan._ticks));
18+
var unit = property.FindPropertyRelative(nameof(AnyTimeSpan._displayUnit));
19+
var unitEnum = (AnyTimeSpan.Unit)unit.enumValueIndex;
20+
21+
var buttonWidth = 80;
22+
var labelPosition = new Rect(position.x, position.y, EditorGUIUtility.labelWidth, position.height);
23+
var buttonPosition = new Rect(position.width - buttonWidth, position.y, buttonWidth, position.height);
24+
var propertyPosition = new Rect(labelPosition.width + 20, position.y, position.width - buttonWidth - labelPosition.width - 20, position.height);
25+
EditorGUI.LabelField(labelPosition, label);
26+
EditorGUI.PropertyField(buttonPosition, unit, new GUIContent());
27+
if (unitEnum == AnyTimeSpan.Unit.Ticks)
28+
{
29+
ticks.longValue = EditorGUI.LongField(propertyPosition, ticks.longValue);
30+
}
31+
else
32+
{
33+
var time = EditorGUI.DoubleField(propertyPosition, ToDouble(ticks.longValue, unitEnum));
34+
ticks.longValue = FromDouble(time, unitEnum).Ticks;
35+
}
36+
37+
static double ToDouble(long ticks, AnyTimeSpan.Unit unit)
38+
{
39+
var timespan = TimeSpan.FromTicks(ticks);
40+
return unit switch
41+
{
42+
AnyTimeSpan.Unit.Milliseconds => timespan.TotalMilliseconds,
43+
AnyTimeSpan.Unit.Seconds => timespan.TotalSeconds,
44+
AnyTimeSpan.Unit.Minutes => timespan.TotalMinutes,
45+
AnyTimeSpan.Unit.Hours => timespan.TotalHours,
46+
AnyTimeSpan.Unit.Days => timespan.TotalDays,
47+
_ => throw new ArgumentOutOfRangeException()
48+
};
49+
}
50+
51+
static TimeSpan FromDouble(double time, AnyTimeSpan.Unit unit)
52+
{
53+
return unit switch
54+
{
55+
AnyTimeSpan.Unit.Milliseconds => TimeSpan.FromMilliseconds(time),
56+
AnyTimeSpan.Unit.Seconds => TimeSpan.FromSeconds(time),
57+
AnyTimeSpan.Unit.Minutes => TimeSpan.FromMinutes(time),
58+
AnyTimeSpan.Unit.Hours => TimeSpan.FromHours(time),
59+
AnyTimeSpan.Unit.Days => TimeSpan.FromDays(time),
60+
_ => throw new ArgumentOutOfRangeException()
61+
};
62+
}
63+
}
64+
}
65+
}

Packages/com.quabug.any-serialize/Editor/AnyTimeSpanDrawer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace AnySerialize
5+
{
6+
[Serializable]
7+
public class AnyDateTime : IReadOnlyAny<DateTime>, IAny<DateTime>
8+
{
9+
[SerializeField] internal DateTimeKind _dateTimeKind = DateTimeKind.Utc;
10+
[SerializeField] internal long _ticks;
11+
12+
public DateTime Value
13+
{
14+
get => new(_ticks, _dateTimeKind);
15+
set => (_dateTimeKind, _ticks) = (value.Kind, value.Ticks);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)