Unity Inspector 给组件自动关联引用

本文介绍了在Unity中如何实现组件之间的自动引用关联,包括两种方法:通过继承`MonoAutoQuote`和添加`[AAutoQuote]`特性。示例代码展示了如何在Inspector中操作,以及`AutioQuoteMenu`类的详细功能,用于查找并设置子节点的引用。此外,提供了链接指向paws3d学习中心,以获取更多Unity2018的相关教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过声明的变量名称,主动关联引用.

使用这个关联引用两种方式
1.  给你组件继承  MonoAutoQuote 点击组件inspector 按钮执行
2.  给你组件类添加[AAutoQuote] 特性  通过Plateface/SetSelectGameRef 执行

 

[AAutoQuote]public class MonoAutoQuote : MonoBehaviour ,IAutoQuote{}

public interface IAutoQuote { }

public class AAutoQuote : Attribute {}

 

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using System.Reflection;

 

//[CanEditMultipleObjects]

[CustomEditor(typeof(MonoAutoQuote), true)]public class AutoQuoteEditor : Editor

{

    public override void OnInspectorGUI()

    {

        base.OnInspectorGUI();

 

        if (GUILayout.Button("关联子节点引用"))

        {

            Component c = target as Component;

            if (c != null)

                AutioQuoteMenu.SetRef(c, c.gameObject);

        }

 

 

    }

}

public class AutioQuoteMenu

{

    [MenuItem("Plateface/SetSelectGameRef %&A")]

    public static void SetRef()

    {

        GameObject o = Selection.activeGameObject;

        if (o != null)

        {

            Component[] cAry = o.GetComponents<Component>();

            foreach (var c in cAry)

            {

                System.Type componentType = c.GetType();

                if ((typeof(MonoBehaviour).IsAssignableFrom(componentType)) || IsHasAttribute(componentType))

                {

                    SetRef(c, o);

                }

            }

        }

    }

 

    public static void SetRef(Component c, GameObject o)

    {

        System.Type t = c.GetType();

        var infoList = t.GetFields(BindingFlags.Public | BindingFlags.Instance);

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        string name = string.Empty;

        foreach (var item in infoList)

        {

            var fieldType = item.FieldType;

 

            if ((typeof(MonoBehaviour).IsAssignableFrom(fieldType)))

            {

                if (item.Name.StartsWith("m"))

                {

                    name = item.Name.Substring(1);

                    Transform tr = o.transform.Find(name);

                    if (tr == null)

                    {

                        Debug.LogError(name + "引用没找到");

                        continue;

                    }

 

                    Component com = tr.GetComponent(fieldType);

                    item.SetValue(c, com);

                }

            }

        }

    }

 

 

    public static bool IsHasAttribute(System.Type type)

    {

        System.Object[] oList = type.GetCustomAttributes(typeof(AAutoQuote), false);

        foreach (var item in oList)

        {

            if ((item as AAutoQuote) != null)

                return true;

        }

 

        return false;

    }

}

 

更多unity2018的功能介绍请到paws3d学习中心查找。链接https://www.paws3d.com/learn/,也可以加入unity学习讨论群935714213

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值