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,9 +3,12 @@ using UnityEditor;
/// <summary>
/// Drawer for the RequireInterface attribute.
/// </summary>
[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
public class RequireInterfaceDrawer : PropertyDrawer
namespace NEG.Utils
{
[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
public class RequireInterfaceDrawer : PropertyDrawer
{
/// <summary>
/// Overrides GUI drawing for the attribute.
/// </summary>
@ -38,4 +41,5 @@ public class RequireInterfaceDrawer : PropertyDrawer
GUI.color = previousColor;
}
}
}
}

View File

@ -2,8 +2,11 @@ using UnityEngine;
/// <summary>
/// Attribute that require implementation of the provided interface.
/// </summary>
public class RequireInterfaceAttribute : PropertyAttribute
namespace NEG.Utils
{
public class RequireInterfaceAttribute : PropertyAttribute
{
// Interface type.
public System.Type requiredType { get; private set; }
/// <summary>
@ -14,4 +17,5 @@ public class RequireInterfaceAttribute : PropertyAttribute
{
this.requiredType = type;
}
}
}