Bootstrap

Unity UI同名自动绑定

using System;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

///
/// 这个类方到Editor下
///
[CustomPropertyDrawer(typeof(AutoBindAttribute))]
public class AutoBindSameNameDrawer : PropertyDrawer
{
// 在Inspector面板中绘制带有AutoBindSameName属性的字段
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);

    // 显示属性名称
    position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

     自动绑定同名UI组件
    //Component[] components = property.serializedObject.targetObject.GetComponentsInChildren<Component>();
    //foreach (Component component in components)
    //{
    //    if (component is Button && component.name == property.name)
    //    {
    //        property.objectReferenceValue = c
;