move to namespace + add serialization extensions

This commit is contained in:
Hubert Mattusch 2022-09-17 22:17:12 +02:00
parent 51223864a6
commit 63f98cd79b
5 changed files with 98 additions and 39 deletions

8
Editor.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2b210ff3d17b2814e81a315ad8f9bf32
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,32 @@
using System;
using UnityEditor;
namespace NEG.Utils.Serialization
{
public static class SerializationExtentions
{
public static SerializedProperty FindAutoProperty(this SerializedObject @this, string name)
{
return @this.FindProperty(GetBackingFieldName(name));
}
public static SerializedProperty FindAutoPropertyRelative(this SerializedProperty @this, string name)
{
return @this.FindPropertyRelative(GetBackingFieldName(name));
}
private static string GetBackingFieldName(string name)
{
#if NET_STANDARD || NET_STANDARD_2_1
return string.Create(1/*<*/ + name.Length + 16/*>k__BackingField*/, name, static (span, name) =>
{
span[0] = '<';
name.AsSpan().CopyTo(span[1..]);
">k__BackingField".AsSpan().CopyTo(span[(name.Length + 1)..]);
});
#else
return '<' + name + ">k__BackingField";
#endif
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a42993224bbc704dbe244661de48913
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,6 +3,9 @@ using UnityEditor;
/// <summary> /// <summary>
/// Drawer for the RequireInterface attribute. /// Drawer for the RequireInterface attribute.
/// </summary> /// </summary>
namespace NEG.Utils
{
[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))] [CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
public class RequireInterfaceDrawer : PropertyDrawer public class RequireInterfaceDrawer : PropertyDrawer
{ {
@ -39,3 +42,4 @@ public class RequireInterfaceDrawer : PropertyDrawer
} }
} }
} }
}

View File

@ -2,6 +2,9 @@ using UnityEngine;
/// <summary> /// <summary>
/// Attribute that require implementation of the provided interface. /// Attribute that require implementation of the provided interface.
/// </summary> /// </summary>
namespace NEG.Utils
{
public class RequireInterfaceAttribute : PropertyAttribute public class RequireInterfaceAttribute : PropertyAttribute
{ {
// Interface type. // Interface type.
@ -15,3 +18,4 @@ public class RequireInterfaceAttribute : PropertyAttribute
this.requiredType = type; this.requiredType = type;
} }
} }
}