1.2.0: Added AssignIfNull which allows for fields to get components from self or children
This commit is contained in:
parent
45411c018a
commit
ea849a715d
39
Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs
Normal file
39
Editor/ValueModifiers/AssignIfNullAttributeDrawer.cs
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e029b0d7c1384ab2b961e74a2bf23fe0
|
||||||
|
timeCreated: 1638695373
|
||||||
|
|
@ -121,6 +121,8 @@ List of all value attributes:
|
||||||
* If value is less than min value, then set value to min value
|
* If value is less than min value, then set value to min value
|
||||||
* `SmallerThanField`
|
* `SmallerThanField`
|
||||||
* If value is less than other fields value, then set value to other fields value
|
* 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
|
## Validate
|
||||||
|
|
|
||||||
36
Runtime/ValueModifiers/AssignIfNullAttribute.cs
Normal file
36
Runtime/ValueModifiers/AssignIfNullAttribute.cs
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Runtime/ValueModifiers/AssignIfNullAttribute.cs.meta
Normal file
11
Runtime/ValueModifiers/AssignIfNullAttribute.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: dfd8ecaea012b084f994832dd519d850
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "com.module.inspector",
|
"name": "com.module.inspector",
|
||||||
"version": "1.1.1",
|
"version": "1.2.0",
|
||||||
"displayName": "Module.Inspector",
|
"displayName": "Module.Inspector",
|
||||||
"description": "Custom inspector with various useful property drawers",
|
"description": "Custom inspector with various useful property drawers",
|
||||||
"unity": "2019.2",
|
"unity": "2019.2",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue