move to namespace + add serialization extensions
This commit is contained in:
parent
51223864a6
commit
63f98cd79b
8
Editor.meta
Normal file
8
Editor.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2b210ff3d17b2814e81a315ad8f9bf32
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
32
Editor/SerializationExtentions.cs
Normal file
32
Editor/SerializationExtentions.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Editor/SerializationExtentions.cs.meta
Normal file
11
Editor/SerializationExtentions.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a42993224bbc704dbe244661de48913
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -3,39 +3,43 @@ using UnityEditor;
|
||||
/// <summary>
|
||||
/// Drawer for the RequireInterface attribute.
|
||||
/// </summary>
|
||||
[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
|
||||
public class RequireInterfaceDrawer : PropertyDrawer
|
||||
|
||||
namespace NEG.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// 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)
|
||||
[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))]
|
||||
public class RequireInterfaceDrawer : PropertyDrawer
|
||||
{
|
||||
// Check if this is reference type property.
|
||||
if (property.propertyType == SerializedPropertyType.ObjectReference)
|
||||
/// <summary>
|
||||
/// 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.
|
||||
var requiredAttribute = this.attribute as RequireInterfaceAttribute;
|
||||
// Begin drawing property field.
|
||||
EditorGUI.BeginProperty(position, label, property);
|
||||
// Draw property field.
|
||||
property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, requiredAttribute.requiredType, true);
|
||||
// Finish drawing property field.
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
else
|
||||
{
|
||||
// If field is not reference, show error message.
|
||||
// Save previous color and change GUI to red.
|
||||
var previousColor = GUI.color;
|
||||
GUI.color = Color.red;
|
||||
// Display label with error message.
|
||||
EditorGUI.LabelField(position, label, new GUIContent("Property is not a reference type"));
|
||||
// Revert color change.
|
||||
GUI.color = previousColor;
|
||||
// Check if this is reference type property.
|
||||
if (property.propertyType == SerializedPropertyType.ObjectReference)
|
||||
{
|
||||
// Get attribute parameters.
|
||||
var requiredAttribute = this.attribute as RequireInterfaceAttribute;
|
||||
// Begin drawing property field.
|
||||
EditorGUI.BeginProperty(position, label, property);
|
||||
// Draw property field.
|
||||
property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, requiredAttribute.requiredType, true);
|
||||
// Finish drawing property field.
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
else
|
||||
{
|
||||
// If field is not reference, show error message.
|
||||
// Save previous color and change GUI to red.
|
||||
var previousColor = GUI.color;
|
||||
GUI.color = Color.red;
|
||||
// Display label with error message.
|
||||
EditorGUI.LabelField(position, label, new GUIContent("Property is not a reference type"));
|
||||
// Revert color change.
|
||||
GUI.color = previousColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,16 +2,20 @@ using UnityEngine;
|
||||
/// <summary>
|
||||
/// Attribute that require implementation of the provided interface.
|
||||
/// </summary>
|
||||
public class RequireInterfaceAttribute : PropertyAttribute
|
||||
|
||||
namespace NEG.Utils
|
||||
{
|
||||
// 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)
|
||||
public class RequireInterfaceAttribute : PropertyAttribute
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user