You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

49 lines
1.5 KiB

using System;
using System.Collections;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(AutoInjector), true)]
public class AutoInjectorDrawer : PropertyDrawer
{
AutoInjector AutoInjector => attribute as AutoInjector;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Type fieldType = fieldInfo.FieldType;
if (!fieldType.IsSubclassOf(typeof(Component)) ||
fieldType.IsSubclassOf(typeof(IEnumerable)) ||
fieldType.IsArray)
{
return;
}
if (property.objectReferenceValue == null || !property.objectReferenceValue.GetType().IsSubclassOf(fieldType))
{
Component target = property.serializedObject.targetObject as Component;
Transform pwd = RecursiveFind(target.transform, AutoInjector.Path.Split('/'), 0);
if (pwd != null)
{
property.objectReferenceValue = pwd.GetComponent(fieldType);
}
else
{
property.objectReferenceValue = null;
}
}
var guiEnabled = GUI.enabled;
GUI.enabled = false;
EditorGUI.PropertyField(position, property, new GUIContent(label.text, AutoInjector.Path), true);
GUI.enabled = guiEnabled;
}
Transform RecursiveFind(Transform pwd, string[] chunk, int pwdIndex)
{
if(pwd is null || pwdIndex >= chunk.Length) return pwd;
return RecursiveFind(pwd.Find(chunk[pwdIndex]), chunk, pwdIndex + 1);
}
}