From ea849a715d1409b0225789c4f6dc70c682f5a950 Mon Sep 17 00:00:00 2001 From: Anders Ejlersen Date: Sun, 5 Dec 2021 10:31:29 +0100 Subject: [PATCH] 1.2.0: Added AssignIfNull which allows for fields to get components from self or children --- .../AssignIfNullAttributeDrawer.cs | 39 +++++++++++++++++++ .../AssignIfNullAttributeDrawer.cs.meta | 3 ++ README.md | 4 +- .../ValueModifiers/AssignIfNullAttribute.cs | 36 +++++++++++++++++ .../AssignIfNullAttribute.cs.meta | 11 ++++++ package.json | 2 +- 6 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs create mode 100644 Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs.meta create mode 100644 Runtime/ValueModifiers/AssignIfNullAttribute.cs create mode 100644 Runtime/ValueModifiers/AssignIfNullAttribute.cs.meta diff --git a/Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs b/Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs new file mode 100644 index 0000000..021544b --- /dev/null +++ b/Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs @@ -0,0 +1,39 @@ +using System; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace Module.Inspector.Editor +{ + [CustomPropertyDrawer(typeof(AssignIfNullAttribute))] + internal sealed class AssignIfNullAttributeDrawer : ValueModifierPropertyDrawer + { + public override void Modify(ValueModifierPropertyAttribute attribute, SerializedProperty property) + { + if (property.propertyType != SerializedPropertyType.ObjectReference) + return; + if (property.objectReferenceValue != null) + return; + + var att = (AssignIfNullAttribute)attribute; + Type type = att.useType ? att.type : property.GetValueType(); + + if (!typeof(Component).IsAssignableFrom(type)) + return; + + for (var i = 0; i < property.serializedObject.targetObjects.Length; i++) + { + Object obj = property.serializedObject.targetObjects[i]; + + if (!(obj is Component component)) + continue; + + Component c = att.includeChildren + ? component.gameObject.GetComponentInChildren(type) + : component.gameObject.GetComponent(type); + + property.objectReferenceValue = c; + } + } + } +} \ No newline at end of file diff --git a/Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs.meta b/Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs.meta new file mode 100644 index 0000000..fd1013c --- /dev/null +++ b/Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e029b0d7c1384ab2b961e74a2bf23fe0 +timeCreated: 1638695373 \ No newline at end of file diff --git a/README.md b/README.md index 7910d06..06fbaff 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ There are six different kinds of attributes: * Validate field (if invalid, draws as red) * Decorator * Adds additional information or styling to a field/method (E.g. Header displays a title/category to fields after attribute) - + ## Access @@ -121,6 +121,8 @@ List of all value attributes: * If value is less than min value, then set value to min value * `SmallerThanField` * If value is less than other fields value, then set value to other fields value +* `AssignIfNull` + * If value is null will either use field or custom type to get from self or children ## Validate diff --git a/Runtime/ValueModifiers/AssignIfNullAttribute.cs b/Runtime/ValueModifiers/AssignIfNullAttribute.cs new file mode 100644 index 0000000..81d71f7 --- /dev/null +++ b/Runtime/ValueModifiers/AssignIfNullAttribute.cs @@ -0,0 +1,36 @@ +using System; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class AssignIfNullAttribute : ValueModifierPropertyAttribute + { + public readonly bool useType; + public readonly Type type; + public readonly bool includeChildren; + + public AssignIfNullAttribute() + { + useType = false; + } + + public AssignIfNullAttribute(bool includeChildren) + { + useType = false; + this.includeChildren = includeChildren; + } + + public AssignIfNullAttribute(Type type) + { + this.type = type; + useType = true; + } + + public AssignIfNullAttribute(Type type, bool includeChildren) + { + this.type = type; + this.includeChildren = includeChildren; + useType = true; + } + } +} \ No newline at end of file diff --git a/Runtime/ValueModifiers/AssignIfNullAttribute.cs.meta b/Runtime/ValueModifiers/AssignIfNullAttribute.cs.meta new file mode 100644 index 0000000..687a964 --- /dev/null +++ b/Runtime/ValueModifiers/AssignIfNullAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dfd8ecaea012b084f994832dd519d850 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index 51aeec5..7bf7670 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.module.inspector", - "version": "1.1.1", + "version": "1.2.0", "displayName": "Module.Inspector", "description": "Custom inspector with various useful property drawers", "unity": "2019.2",