Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Packages/com.quabug.any-serialize/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "AnySerialize.Editor",
"rootNamespace": "",
"references": [
"GUID:3ea4545e70041684b9bbf08dd5c6e257"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Packages/com.quabug.any-serialize/Editor/AnySingleValueDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UnityEditor;
using UnityEngine;

namespace AnySerialize.Editor
{
[CustomPropertyDrawer(typeof(AnyValue<>), true)]
[CustomPropertyDrawer(typeof(ReadOnlyAnyArray<,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyLazy<,>))]
public class AnySingleValueDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
property.NextVisible(enterChildren: true);
return EditorGUI.GetPropertyHeight(property, label, includeChildren: true);
}

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
property.NextVisible(enterChildren: true);
EditorGUI.PropertyField(position, property, label, includeChildren: true);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions Packages/com.quabug.any-serialize/Editor/ReadOnlyAnyClassDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace AnySerialize.Editor
{
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
[CustomPropertyDrawer(typeof(ReadOnlyAnyClass<,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>))]
public class ReadOnlyAnyClassDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, includeChildren: true);
}

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position.height = EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(position, property, label, includeChildren: false);
if (!property.isExpanded) return;

property.serializedObject.Update();

var classType = property.GetFieldsByPath().Last().field.GetType().GenericTypeArguments[0];
var fields = classType.GetFields(AnyClassUtility.DefaultBindingFlags);
var fieldIndex = 0;
var end = property.GetEndProperty(includeInvisible: false);
property.NextVisible(enterChildren: true);
position.y += EditorGUIUtility.singleLineHeight;

using (new EditorGUI.IndentLevelScope())
{
do
{
var fieldName = fields[fieldIndex].Name;
if (fieldName.EndsWith("k__BackingField")) fieldName = fieldName.Substring(1, fieldName.Length - "k__BackingField".Length - 2);
var fieldLabel = new GUIContent(fieldName);
EditorGUI.PropertyField(position, property, fieldLabel, includeChildren: true);
position.y += EditorGUI.GetPropertyHeight(property, fieldLabel, includeChildren: true);
fieldIndex++;
} while (property.NextVisible(enterChildren: false) && !SerializedProperty.EqualContents(property, end));
}

property.serializedObject.ApplyModifiedProperties();
}
}

static class Extension
{
private static Regex _arrayData = new Regex(@"^data\[(\d+)\]$");

public static IEnumerable<(object field, FieldInfo fi)> GetFieldsByPath(this SerializedProperty property)
{
var obj = (object)property.serializedObject.targetObject;
FieldInfo fi = null;
yield return (obj, fi);
var pathList = property.propertyPath.Split('.');
for (var i = 0; i < pathList.Length; i++)
{
var fieldName = pathList[i];
if (fieldName == "Array" && i + 1 < pathList.Length && _arrayData.IsMatch(pathList[i + 1]))
{
i++;
var itemIndex = int.Parse(_arrayData.Match(pathList[i]).Groups[1].Value);
var array = (IList)obj;
obj = array != null && itemIndex < array.Count ? array[itemIndex] : null;
yield return (obj, fi);
}
else
{
var t = Field(obj, obj?.GetType() ?? fi.FieldType, fieldName);
obj = t.field;
fi = t.fi;
yield return t;
}
}

(object field, FieldInfo fi) Field(object declaringObject, Type declaringType, string fieldName)
{
var fieldInfo = declaringType.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var fieldValue = declaringObject == null ? null : fieldInfo.GetValue(declaringObject);
return (fieldValue, fieldInfo);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Packages/com.quabug.any-serialize/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.quabug.any-serialize",
"description": "Generate serializable code of any properties for Unity3D serializer",
"version": "0.1.0",
"version": "0.2.0",
"unity": "2021.3",
"displayName": "AnySerialize",
"type": "library",
Expand Down