UnityInspector給元件自動關聯引用
通過宣告的變數名稱,主動關聯引用.
使用這個關聯引用兩種方式
- 給你元件繼承 MonoAutoQuote 點選元件inspector 按鈕執行
- 給你元件類新增[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();
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學習中心查詢。
相關文章
- swift自動引用計數Swift
- 聯動picker元件元件
- 無聯動picker元件元件
- VUE 父元件動態傳值給子元件Vue元件
- Objective-C自動引用計數ARCObject
- WPF給控制元件新增運動動畫控制元件動畫
- 自定義元件-元件的建立和引用元件
- Autocomplete自動補全元件-HeyUI元件庫元件UI
- React 父元件如何主動“聯絡”子元件React元件
- python+pytest介面自動化(13)-token關聯登入Python
- Spring 自動掃描元件Spring元件
- 關於跑自動化 docker 映象的問題給出個注意?Docker
- 釘釘機器人自動關聯 GitHub 傳送 approval prs機器人GithubAPP
- 關於強引用、軟引用、弱引用、幻象引用,你該如何回答?
- 關於vue動態元件Vue元件
- 給mybatis新增自動建表,自動加欄位的功能MyBatis
- WinForm引用ActiveX元件,對Com元件的學習ORM元件
- 聯想win10如何取消自動更新 關閉win10自動更新的解決方法Win10
- 分析函式改寫自關聯函式
- C++ Qt開發:SqlRelationalTable關聯表元件C++QTSQL元件
- 關於移動端元件庫元件
- 視覺化搭建 - 元件值與聯動視覺化元件
- tpextbuilder- Displayers[元件]- When表單聯動UI元件
- Android 通用的元件自動註冊、自動初始化解決方案Android元件
- Binding(二):控制元件關聯和程式碼提升控制元件
- 前端自定義元件,表單顠浮層( 動態表格,靜態引用)前端元件
- Springboot專案啟動後自動建立多表關聯的資料庫與表的方案Spring Boot資料庫
- Loadrunner11 手動關聯
- 子元件給父元件傳資料元件
- 如何關閉Win10自動更新 win10永久關閉自動更新Win10
- VUE - 區域性引用Element-UI元件VueUI元件
- 微信小程式中引用vant元件庫微信小程式元件
- jmeter 介面自動化連載 (10)- 響應資訊不明確的介面關聯JMeter
- python介面自動化(三十五)-封裝與呼叫--流程類介面關聯(詳解)Python封裝
- 帝國cms專題自動關聯外掛位於/e/extend/plugins/目錄Plugin
- 如何關閉Windows自動更新Windows
- WebStorm關閉自動拆疊WebORM
- filebeat自動關閉解決