Unity Inspector 给组件自动关联引用

简介: 通过声明的变量名称,主动关联引用.使用这个关联引用两种方式给你组件继承 MonoAutoQuote 点击组件inspector 按钮执行给你组件类添加[AAutoQuote] 特性 通过Plateface/SetSelectGameRef 执行[AAutoQuote]public cla...

通过声明的变量名称,主动关联引用.
使用这个关联引用两种方式

  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();
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学习中心查找。

相关文章
|
5月前
|
编解码 前端开发 人机交互
【Unity 3D】UI系统中UGUI各个组件的详细讲解(附源码 超详细)
【Unity 3D】UI系统中UGUI各个组件的详细讲解(附源码 超详细)
96 0
|
5月前
|
C# 图形学
【Unity 3D】游戏对象、添加删除获取组件、预制体Prefabs简介
【Unity 3D】游戏对象、添加删除获取组件、预制体Prefabs简介
86 0
|
5月前
|
编译器 图形学
Unity用脚本获取物体和组件(下)
Unity用脚本获取物体和组件(下)
103 0
|
5月前
|
图形学
Unity 用脚本获取物体和组件(上)
Unity 用脚本获取物体和组件(上)
134 0
|
5月前
|
图形学
Unity常用组件
Unity常用组件
129 0
|
6月前
|
前端开发 C# 开发工具
Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
109 0
|
8月前
|
图形学
|
C# 图形学
Unity通过组件名称字符串添加脚本
通过Type.GetType(string typeName)来得到字符串对应的Type。 Type.GetType(typeName)能获取到自定义类的类型,但是获取Unity的组件不行。 例如Type.GetType(“Rigidbody”)值为null,其实是少了程序集。 string qualifiedName = typeof(Rigidbody).AssemblyQualifiedName; ty 获取Unity的组件程序集全名,再通过Type.GetType()得到的就不为null了。
451 0
Unity通过组件名称字符串添加脚本
|
11月前
|
图形学 索引
【unity每日一记】--transform组件篇
【unity每日一记】--transform组件篇
141 0
|
C# 图形学
unity3d-GameObject组件
unity3d-GameObject组件
unity3d-GameObject组件