Skip to content

Commit d83da2e

Browse files
authored
Drawer (#20)
* drawers * update version
1 parent 5d753da commit d83da2e

File tree

8 files changed

+167
-1
lines changed

8 files changed

+167
-1
lines changed

Packages/com.quabug.any-serialize/Editor.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+
{
2+
"name": "AnySerialize.Editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:3ea4545e70041684b9bbf08dd5c6e257"
6+
],
7+
"includePlatforms": [
8+
"Editor"
9+
],
10+
"excludePlatforms": [],
11+
"allowUnsafeCode": false,
12+
"overrideReferences": false,
13+
"precompiledReferences": [],
14+
"autoReferenced": false,
15+
"defineConstraints": [],
16+
"versionDefines": [],
17+
"noEngineReferences": false
18+
}

Packages/com.quabug.any-serialize/Editor/AnySerialize.Editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace AnySerialize.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(AnyValue<>), true)]
7+
[CustomPropertyDrawer(typeof(ReadOnlyAnyArray<,>))]
8+
[CustomPropertyDrawer(typeof(ReadOnlyAnyLazy<,>))]
9+
public class AnySingleValueDrawer : PropertyDrawer
10+
{
11+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
12+
{
13+
property.NextVisible(enterChildren: true);
14+
return EditorGUI.GetPropertyHeight(property, label, includeChildren: true);
15+
}
16+
17+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
18+
{
19+
property.NextVisible(enterChildren: true);
20+
EditorGUI.PropertyField(position, property, label, includeChildren: true);
21+
}
22+
}
23+
}

Packages/com.quabug.any-serialize/Editor/AnySingleValueDrawer.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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text.RegularExpressions;
7+
using UnityEditor;
8+
using UnityEngine;
9+
10+
namespace AnySerialize.Editor
11+
{
12+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,>))]
13+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,>))]
14+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,>))]
15+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,>))]
16+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,>))]
17+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,>))]
18+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,>))]
19+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,>))]
20+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,>))]
21+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,>))]
22+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,>))]
23+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,>))]
24+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
25+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
26+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
27+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
28+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
29+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
30+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
31+
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
32+
public class ReadOnlyAnyClassDrawer : PropertyDrawer
33+
{
34+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
35+
{
36+
return EditorGUI.GetPropertyHeight(property, label, includeChildren: true);
37+
}
38+
39+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
40+
{
41+
position.height = EditorGUIUtility.singleLineHeight;
42+
EditorGUI.PropertyField(position, property, label, includeChildren: false);
43+
if (!property.isExpanded) return;
44+
45+
property.serializedObject.Update();
46+
47+
var classType = property.GetFieldsByPath().Last().field.GetType().GenericTypeArguments[0];
48+
var fields = classType.GetFields(AnyClassUtility.DefaultBindingFlags);
49+
var fieldIndex = 0;
50+
var end = property.GetEndProperty(includeInvisible: false);
51+
property.NextVisible(enterChildren: true);
52+
position.y += EditorGUIUtility.singleLineHeight;
53+
54+
using (new EditorGUI.IndentLevelScope())
55+
{
56+
do
57+
{
58+
var fieldName = fields[fieldIndex].Name;
59+
if (fieldName.EndsWith("k__BackingField")) fieldName = fieldName.Substring(1, fieldName.Length - "k__BackingField".Length - 2);
60+
var fieldLabel = new GUIContent(fieldName);
61+
EditorGUI.PropertyField(position, property, fieldLabel, includeChildren: true);
62+
position.y += EditorGUI.GetPropertyHeight(property, fieldLabel, includeChildren: true);
63+
fieldIndex++;
64+
} while (property.NextVisible(enterChildren: false) && !SerializedProperty.EqualContents(property, end));
65+
}
66+
67+
property.serializedObject.ApplyModifiedProperties();
68+
}
69+
}
70+
71+
static class Extension
72+
{
73+
private static Regex _arrayData = new Regex(@"^data\[(\d+)\]$");
74+
75+
public static IEnumerable<(object field, FieldInfo fi)> GetFieldsByPath(this SerializedProperty property)
76+
{
77+
var obj = (object)property.serializedObject.targetObject;
78+
FieldInfo fi = null;
79+
yield return (obj, fi);
80+
var pathList = property.propertyPath.Split('.');
81+
for (var i = 0; i < pathList.Length; i++)
82+
{
83+
var fieldName = pathList[i];
84+
if (fieldName == "Array" && i + 1 < pathList.Length && _arrayData.IsMatch(pathList[i + 1]))
85+
{
86+
i++;
87+
var itemIndex = int.Parse(_arrayData.Match(pathList[i]).Groups[1].Value);
88+
var array = (IList)obj;
89+
obj = array != null && itemIndex < array.Count ? array[itemIndex] : null;
90+
yield return (obj, fi);
91+
}
92+
else
93+
{
94+
var t = Field(obj, obj?.GetType() ?? fi.FieldType, fieldName);
95+
obj = t.field;
96+
fi = t.fi;
97+
yield return t;
98+
}
99+
}
100+
101+
(object field, FieldInfo fi) Field(object declaringObject, Type declaringType, string fieldName)
102+
{
103+
var fieldInfo = declaringType.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
104+
var fieldValue = declaringObject == null ? null : fieldInfo.GetValue(declaringObject);
105+
return (fieldValue, fieldInfo);
106+
}
107+
}
108+
}
109+
}

Packages/com.quabug.any-serialize/Editor/ReadOnlyAnyClassDrawer.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/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.quabug.any-serialize",
33
"description": "Generate serializable code of any properties for Unity3D serializer",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"unity": "2021.3",
66
"displayName": "AnySerialize",
77
"type": "library",

0 commit comments

Comments
 (0)