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 { }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM