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,39 +3,43 @@ using UnityEditor;
/// <summary> /// <summary>
/// Drawer for the RequireInterface attribute. /// Drawer for the RequireInterface attribute.
/// </summary> /// </summary>
[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
public class RequireInterfaceDrawer : PropertyDrawer namespace NEG.Utils
{ {
/// <summary> [CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
/// Overrides GUI drawing for the attribute. public class RequireInterfaceDrawer : PropertyDrawer
/// </summary>
/// <param name="position">Position.</param>
/// <param name="property">Property.</param>
/// <param name="label">Label.</param>
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{ {
// Check if this is reference type property. /// <summary>
if (property.propertyType == SerializedPropertyType.ObjectReference) /// Overrides GUI drawing for the attribute.
/// </summary>
/// <param name="position">Position.</param>
/// <param name="property">Property.</param>
/// <param name="label">Label.</param>
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{ {
// Get attribute parameters. // Check if this is reference type property.
var requiredAttribute = this.attribute as RequireInterfaceAttribute; if (property.propertyType == SerializedPropertyType.ObjectReference)
// Begin drawing property field. {
EditorGUI.BeginProperty(position, label, property); // Get attribute parameters.
// Draw property field. var requiredAttribute = this.attribute as RequireInterfaceAttribute;
property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, requiredAttribute.requiredType, true); // Begin drawing property field.
// Finish drawing property field. EditorGUI.BeginProperty(position, label, property);
EditorGUI.EndProperty(); // Draw property field.
} property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, requiredAttribute.requiredType, true);
else // Finish drawing property field.
{ EditorGUI.EndProperty();
// If field is not reference, show error message. }
// Save previous color and change GUI to red. else
var previousColor = GUI.color; {
GUI.color = Color.red; // If field is not reference, show error message.
// Display label with error message. // Save previous color and change GUI to red.
EditorGUI.LabelField(position, label, new GUIContent("Property is not a reference type")); var previousColor = GUI.color;
// Revert color change. GUI.color = Color.red;
GUI.color = previousColor; // Display label with error message.
EditorGUI.LabelField(position, label, new GUIContent("Property is not a reference type"));
// Revert color change.
GUI.color = previousColor;
}
} }
} }
} }

View File

@ -2,16 +2,20 @@ using UnityEngine;
/// <summary> /// <summary>
/// Attribute that require implementation of the provided interface. /// Attribute that require implementation of the provided interface.
/// </summary> /// </summary>
public class RequireInterfaceAttribute : PropertyAttribute
namespace NEG.Utils
{ {
// Interface type. public class RequireInterfaceAttribute : PropertyAttribute
public System.Type requiredType { get; private set; }
/// <summary>
/// Requiring implementation of the <see cref="T:RequireInterfaceAttribute"/> interface.
/// </summary>
/// <param name="type">Interface type.</param>
public RequireInterfaceAttribute(System.Type type)
{ {
this.requiredType = type; // Interface type.
public System.Type requiredType { get; private set; }
/// <summary>
/// Requiring implementation of the <see cref="T:RequireInterfaceAttribute"/> interface.
/// </summary>
/// <param name="type">Interface type.</param>
public RequireInterfaceAttribute(System.Type type)
{
this.requiredType = type;
}
} }
} }