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 给组件自动关联引用
项目进入上线阶段了, 有一些地方需要总结和优化. 我发现UI一改变,我就要拖很久的UI. UI结构发生改变我还必须给一些变量设置好引用,后来我去看别人预设的时候组件拖放的变量至少10个以上, 它们一 ...
- 二、Vue组件(component):组件的相互引用、通过props实现父子组件互传值
一.组件各部分说明及互相引用 1.一个vue组件由三个部分组成 Template 只能存在一个根元素 2.Script 3.Style scoped:样式只在当前组件内生效 1.1 组件的基本引用代码 ...
- 使用Unity做2.5D游戏教程(二)
最近在研究Unity 3D,看了老外Marin Todorov写的教程很详细,就翻译过来以便自己参考,翻译不好的地方请多包涵. 这是使用Unity 游戏开发工具制作一个简单的2.5D 游戏系列教程的第 ...
- 【Android】安卓四大组件之Activity(二)
[Android]安卓四大组件之Activity(二) 前言 在这篇文章之前,我已经写过了一篇有关Activity的内容,是关于activity之间的页面跳转和数据传递,而这篇文章着重强调的是Acti ...
- 关于Entity Framework自动关联查询与自动关联更新导航属性对应的实体注意事项说明
一.首先了解下Entity Framework 自动关联查询: Entity Framework 自动关联查询,有三种方法:Lazy Loading(延迟加载),Eager Loading(预先加载) ...
- 【Unity Shaders】学习笔记——SurfaceShader(二)两个结构体和CG类型
[Unity Shaders]学习笔记——SurfaceShader(二)两个结构体和CG类型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5596698. ...
- 使用Unity制作游戏关卡的教程(二)
转自:http://gamerboom.com/archives/75554 作者:by Matthias Zarzecki 本文是“使用Unity制作<The Fork Of Truth> ...
- 022 component(组件)关联映射
Component关联映射: 目前有两个类如下: 值对象没有标识,而实体对象具有标识,值对象属于某一个实体,使用它重复使用率提升,而且更清析. 以上关系的映射称为component(组件)关联映射 在 ...
- Unity UGUI图文混排源码(二)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...
随机推荐
- DBCS 从256开始
ASCII(American Standard Code for Information Interchange,美国信息互换标准代码)是一套基于拉丁字母的字符编码,共收录了 128 个字符,用一个字 ...
- phonegap安卓视频播放解决方案
使用phonegap开发的时候,视频播放很多人一开始选择用html5的Video标签作为备选方案,实际上这种方案在真实环境下常常是无法满足需求的,因为目前HTML5的Video标签只支持MP4,OGG ...
- tomcat服务器上web项目日志存放位置
1.找到log日志的配置文件:log.xml,或者log.property,找到rollingFile标签,该标签的fileName属性就是更新的日志文件的存放位置.(相对于tomcat的bin目录)
- noip 1999 回文数
题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...
- BZOJ 1828 [Usaco2010 Mar]balloc 农场分配(贪心+线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1828 [题目大意] 现在有一些线段[l,r]的需求需要满足,i位置最多允许a[i]条线 ...
- File I/O知识点
问题1:File类的作用? 解答:File 类用于访问文件或目录的属性.File类位于java.io包中. 问题2:流?及流的分类? 解答:流是指一连串流动的字符,是以先进先出的方式发送信息的通道.程 ...
- Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...
- iOS UIPrintInteractionController打印
- (void)printData{ //为打印做准备,创建一个指向sharedPrintController的引用 UIPrintInteractionController *printer = [ ...
- HDU 4576 Robot (很水的概率题)
Robot Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Sub ...
- win7与Ubuntu 13.04双系统修改启动项顺序
在在win7下安装了Ubuntu 13.04后,在grub中,win7启动想是最后一个,为了把win7设置为默认启动项,需要更改grub设置.google了半天,发现更改/etc /default/g ...