Unity Inspector 给组件自动关联引用

项目进入上线阶段了, 有一些地方需要总结和优化.  我发现UI一改变,我就要拖很久的UI。 UI结构发生改变我还必须给一些变量设置好引用,后来我去看别人预设的时候组件拖放的变量至少10个以上, 它们一旦丢失了引用了, 作为一个外人就很难把他们关联起来.  预设就定义了m_xxx名字必须和GameObject名字一样, 这样就方便其他人帮你修复预设的引用啦.

今天就突然想起写一个辅助用具, 一键把一些简单的引用帮我赋值上去. 就再也不用手动拖啦.

代码如下:

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

public class PLAutoAssignment : MonoBehaviour
{


    /// <summary>
    /// 变量名存在_, m_name,  name代表你要去搜索的GameObject名字,再根据类型自动赋值到该变量上面去,
    /// m_name_a, a代表你要去搜索的GameObject名字
    /// </summary>
    [MenuItem("Plateface/AutoAssignment %F1")]
    public static void AutoAssignment()
    {
        Debug.ClearDeveloperConsole();

        GameObject go = Selection.activeGameObject;
        //Debug.Log(go.name);

        IAutoAssignment com = go.GetComponent<IAutoAssignment>();

        Type e = com.GetType();
        var fiedlAry = e.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

        object valueObj = null;
        object[] attrAry = null;
        bool isSeriualizeField = false;
        Component[] comAry = null;
        string gameObjectName = string.Empty;

        foreach (var item in fiedlAry)
        {
            //Debug.Log(item.Name + "-" + item.FieldType.Name + " - ");

            valueObj = item.GetValue(com);
            if (item.Name.Contains("_"))
            {
                gameObjectName = item.Name.Substring(item.Name.LastIndexOf('_') + 1);
            }
            else
            {
                gameObjectName = item.Name;
            }

            if (item.FieldType.IsClass && valueObj == null || "null".Equals(valueObj.ToString()))
            {

                if (!item.IsPublic)
                {
                    attrAry = item.GetCustomAttributes(typeof(SerializeField), false);
                    for (int i = 0; i < attrAry.Length; i++)
                    {
                        if (attrAry[i] is SerializeField)
                        {
                            isSeriualizeField = true;
                            break;
                        }
                    }
                }

                if (item.IsPublic || isSeriualizeField)
                {

                    Transform[] tfAry = go.GetComponentsInChildren<Transform>(true);

                    for (int i = 0; i < tfAry.Length; i++)
                    {
                        if (tfAry[i].name == gameObjectName)
                        {
                            if (item.FieldType.Name == "GameObject")
                            {
                                item.SetValue(com, tfAry[i].gameObject);
                                break;
                            }
                            else
                            {
                                comAry = tfAry[i].GetComponents<Component>();
                                for (int j = 0; j < comAry.Length; j++)
                                {
                                    if (comAry[j].GetType().Name == item.FieldType.Name)
                                    {
                                        item.SetValue(com, comAry[j]);
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                isSeriualizeField = false;
            }
        }
    }
}

public interface IAutoAssignment { }
posted @ 2016-09-18 14:54  盘子脸  阅读(2257)  评论(0编辑  收藏  举报