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
6 changed files with 93 additions and 2 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue