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

使用这个关联引用两种方式
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 给组件自动关联引用(二)的更多相关文章

  1. Unity Inspector 给组件自动关联引用

    项目进入上线阶段了, 有一些地方需要总结和优化.  我发现UI一改变,我就要拖很久的UI. UI结构发生改变我还必须给一些变量设置好引用,后来我去看别人预设的时候组件拖放的变量至少10个以上, 它们一 ...

  2. 二、Vue组件(component):组件的相互引用、通过props实现父子组件互传值

    一.组件各部分说明及互相引用 1.一个vue组件由三个部分组成 Template 只能存在一个根元素 2.Script 3.Style scoped:样式只在当前组件内生效 1.1 组件的基本引用代码 ...

  3. 使用Unity做2.5D游戏教程(二)

    最近在研究Unity 3D,看了老外Marin Todorov写的教程很详细,就翻译过来以便自己参考,翻译不好的地方请多包涵. 这是使用Unity 游戏开发工具制作一个简单的2.5D 游戏系列教程的第 ...

  4. 【Android】安卓四大组件之Activity(二)

    [Android]安卓四大组件之Activity(二) 前言 在这篇文章之前,我已经写过了一篇有关Activity的内容,是关于activity之间的页面跳转和数据传递,而这篇文章着重强调的是Acti ...

  5. 关于Entity Framework自动关联查询与自动关联更新导航属性对应的实体注意事项说明

    一.首先了解下Entity Framework 自动关联查询: Entity Framework 自动关联查询,有三种方法:Lazy Loading(延迟加载),Eager Loading(预先加载) ...

  6. 【Unity Shaders】学习笔记——SurfaceShader(二)两个结构体和CG类型

    [Unity Shaders]学习笔记——SurfaceShader(二)两个结构体和CG类型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5596698. ...

  7. 使用Unity制作游戏关卡的教程(二)

    转自:http://gamerboom.com/archives/75554 作者:by Matthias Zarzecki 本文是“使用Unity制作<The Fork Of Truth> ...

  8. 022 component(组件)关联映射

    Component关联映射: 目前有两个类如下: 值对象没有标识,而实体对象具有标识,值对象属于某一个实体,使用它重复使用率提升,而且更清析. 以上关系的映射称为component(组件)关联映射 在 ...

  9. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

随机推荐

  1. 【POJ 2409】 Let it Bead(置换、burnside引理)

    Let it Bead "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. ...

  2. 【差分约束系统】【最短路】【spfa】CDOJ1646 穷且益坚, 不坠青云之志。

    求一个有n个元素的数列,满足任意连续p个数的和不小于s, 任意连续q个数的和不大于t. 令sum[i]表示前i项的和(0<=i<=n,sum[0]=0) 那么题目的条件可转化为: sum[ ...

  3. python3-开发进阶补充Django中的文件的上传

    PS:这段时间有点不在状态,刚刚找回那个状态,那么我们继续曾经的梦想 今天我们来补充一下文件的上传的几种方式: 首先我们先补充的一个知识点: 一.请求头ContentType: ContentType ...

  4. 通过scrapy内置的ImagePipeline下载图片到本地、并提取本地保存地址

    1.通过scrapy内置的ImagePipeline下载图片到本地 2.获取图片保存本地的地址 1.通过scrapy内置的ImagePipeline下载图片到本地 1)在settings.py中打开  ...

  5. mongodb安装和配置,遇到问题和解决方法

    自己不知道怎么的,心血来潮想重新来安装和配置一下mongodb,之前自己也是按照别人的来整的,印象不深刻.第二遍安装配置遇到很多问题. 弄了两个多小时,期间遇到很多问题: 遇到的主要问题 1.拒绝访问 ...

  6. [转]spring security的原理及教程

    Authentication:认证     spring security使用分类: 如何使用spring security,相信百度过的都知道,总共有四种用法,从简到深为:1.不用数据库,全部数据写 ...

  7. NHibernate 存储过程 第十四篇

    NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误. 一.删除 首先,我们新建一个存储过程如下: CREATE PROC DeletePerson @Id int AS DE ...

  8. boost.asio包装类st_asio_wrapper开发教程(转)

    一:什么是st_asio_wrapper它是一个c/s网络编程框架,基于对boost.asio的包装(最低在boost-1.49.0上调试过),目的是快速的构建一个c/s系统: 二:st_asio_w ...

  9. Spring事务管理笔记

    事务的目的就是要保证数据的高度完整性和一致性. 在实际的项目中,大多都是使用注解的方式来实现事物,这里也就简单记录下使用@Transactional方法和注意事项. 在xml中添加配置 1234567 ...

  10. Openshift部署Zookeeper和Kafka

    部署Zookeeper github网址 https://github.com/ericnie2015/zookeeper-k8s-openshift 1.在openshift目录中,首先构建imag ...