Unity Inspector 给组件自动关联引用

Posted 盘子脸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity Inspector 给组件自动关联引用相关的知识,希望对你有一定的参考价值。

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

使用这个关联引用两种方式
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;
    }
}

以上是关于Unity Inspector 给组件自动关联引用的主要内容,如果未能解决你的问题,请参考以下文章

Unity游戏开发之怎么在代码里面引用到Inspector里面显示的值

unity inspector 自动装载Commont和Prefab属性

Unity2.7 检视器(Inspector)

如何自定义unity的inspector面板

Unity脚本篇 - 变量和Inspector操作

Unity脚本篇 - 变量和Inspector操作