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

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

最新文章

  1. xutils3
  2. Dex Loader] Unable to execute dex: Multiple dex files define
  3. C#/.NET Little Wonders: Use Cast() and OfType() to Change Sequence Type(zz)
  4. nagios plugin 开发
  5. codeforces Gym 100500H A. Potion of Immortality 简单DP
  6. Netflix工程总监眼中的分类算法:深度学习优先级最低
  7. LeetCode解题报告:LRU Cache
  8. Java批量文件打包下载zip
  9. poj2993 poj2669
  10. codeforces MemSQL start[c]up Round 2 - online version B 最长公共子系列
  11. contextmenu
  12. 百度地图api写在html上可以实现,在jsp上会出现Bmap未定义的问题
  13. CASE WHEN的两种格式
  14. while循环学习之统计流量
  15. PaddlePaddle︱开发文档中学习情感分类(CNN、LSTM、双向LSTM)、语义角色标注
  16. 启动Eclipse时An internal error occurred during: &quot;Initializing Java Tooling&quot;.错误
  17. abc
  18. yield(),wait(),sleep(),join()
  19. C++常见函数使用
  20. XMLHttpRequest.withCredentials

热门文章

  1. CSS排版常用值指南
  2. Section One
  3. HihoCoder - 1715 树的连通问题
  4. java面试笔试总结(一)--亲生经历的面试题
  5. hdu3401 Trade 单调队列优化dp
  6. Run-Time Check Failure #0
  7. [HTML/CSS]div显示在object、embed之上~
  8. GNU C内联汇编(AT&amp;amp;T语法)
  9. matlab中cell array的理解
  10. DB2数据库性能调整和优化(第2版)