diff --git a/Editor.meta b/Editor.meta new file mode 100644 index 0000000..5c133f8 --- /dev/null +++ b/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2b210ff3d17b2814e81a315ad8f9bf32 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/SerializationExtentions.cs b/Editor/SerializationExtentions.cs new file mode 100644 index 0000000..e3ba2c3 --- /dev/null +++ b/Editor/SerializationExtentions.cs @@ -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 + } + } +} diff --git a/Editor/SerializationExtentions.cs.meta b/Editor/SerializationExtentions.cs.meta new file mode 100644 index 0000000..83588fb --- /dev/null +++ b/Editor/SerializationExtentions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0a42993224bbc704dbe244661de48913 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/RequireInterface/Editor/RequireInterfaceDrawer.cs b/RequireInterface/Editor/RequireInterfaceDrawer.cs index dfdd4f2..5535ce5 100644 --- a/RequireInterface/Editor/RequireInterfaceDrawer.cs +++ b/RequireInterface/Editor/RequireInterfaceDrawer.cs @@ -3,39 +3,43 @@ using UnityEditor; /// /// Drawer for the RequireInterface attribute. /// -[CustomPropertyDrawer(typeof(RequireInterfaceAttribute))] -public class RequireInterfaceDrawer : PropertyDrawer + +namespace NEG.Utils { - /// - /// Overrides GUI drawing for the attribute. - /// - /// Position. - /// Property. - /// Label. - 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) + /// + /// Overrides GUI drawing for the attribute. + /// + /// Position. + /// Property. + /// Label. + 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; + } } } } \ No newline at end of file diff --git a/RequireInterface/RequireInterfaceAttribute.cs b/RequireInterface/RequireInterfaceAttribute.cs index ba3c599..47cd16e 100644 --- a/RequireInterface/RequireInterfaceAttribute.cs +++ b/RequireInterface/RequireInterfaceAttribute.cs @@ -2,16 +2,20 @@ using UnityEngine; /// /// Attribute that require implementation of the provided interface. /// -public class RequireInterfaceAttribute : PropertyAttribute + +namespace NEG.Utils { - // Interface type. - public System.Type requiredType { get; private set; } - /// - /// Requiring implementation of the interface. - /// - /// Interface type. - public RequireInterfaceAttribute(System.Type type) + public class RequireInterfaceAttribute : PropertyAttribute { - this.requiredType = type; + // Interface type. + public System.Type requiredType { get; private set; } + /// + /// Requiring implementation of the interface. + /// + /// Interface type. + public RequireInterfaceAttribute(System.Type type) + { + this.requiredType = type; + } } } \ No newline at end of file