Unity定制Inspector常用的辅助功能

2017-06-08  本文已影响582人  魁犸

转载 本文地址:http://fengyu.name/article/310

在Unity中,我们可以很方便的对Inspector进行定制,有很多简单方便的小功能都会用到。例如给一个int或float加一个范围等。这些操作不止可以在Editor类型的脚本中进行,在普通的MonoBehaviour中也可以做到。这里给大家分享一下在Unity中试验的结果。

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

//在 Add Component 按钮中增加一个菜单项

[AddComponentMenu("Transform/Follow Transform")]

//在 Create 菜单中,增加一个菜单项,前提条件是:该脚本继承自ScriptableObject

[CreateAssetMenu(fileName = "New AttributeTest File", menuName = "AttributeTest", order =1)]

//限制同一个GameObject只能有一个该组件(脚本)

[DisallowMultipleComponent]

//编辑器的编辑模式下,在Update、OnGUI、 OnRenderObject时会执行

[ExecuteInEditMode]

//自定义组件右上角?图标的链接

[HelpURL("http://fengyu.name/")]

//如果该组件继承自 MonoBehaviour,则必须有一个 BoxCollider 组件同时存在

[RequireComponent(typeof(BoxCollider))]

//没发现任何改变

[SelectionBase]

//只有在该组件继承自 StateMachineBehaviour 时有效,具体作用未知

[SharedBetweenAnimators]

public class AttributeTest : MonoBehaviour

{

//将一个字段变为颜色原则

[ColorUsage(true)]

public Color color;

//脚本管理的地方增加一个菜单

[ContextMenu("Do Something")]

void DoSomething()

{

Debug.Log("Perform operation");

}

//字段名称处,增加一个右键菜单。第一个参数为菜单名称,第二个参数为功能的函数名

[ContextMenuItem("Reset", "ResetBiography")]

[Multiline(2)]

public string playerBiography = "";

void ResetBiography()

{

playerBiography = "";

}

//该值,只有在点击Enter键、丢失焦点时才会被返回

[Delayed]

public float delay;

//没有发现产生的影响

[GUITarget(0, 1)]

void OnGUI()

{

GUI.Label(new Rect(10, 10, 300, 100), "Visible on TV and Wii U GamePad only");

}

//用于增加一个标题头

[Header("Header之后的部分")]

public string header;

//会在 Inspector 中隐藏字段

[HideInInspector]

public string hide;

//创建一个显示3行的文本框

[Multiline(3)]

public string multiline;

//使值变成滑动条的方式,并限制大小

[Range(0, 10)]

public float range;

//加载时初始化运行函数

[RuntimeInitializeOnLoadMethod]

static void OnRuntimeMethodLoad()

{

Debug.Log("After scene is loaded and game is running");

}

//可以序列化私有字段,让 private 也在 Inspector 中显示

[SerializeField]

private string serializeField;

//创造一个高度为10的空白区域,可以用做分割线,高度单位估计是像素

[Space(10)]

public string space;

//创建一个文本区域,文本区域会单独一行存在

[TextArea]

public string textArea;

//当字段获得焦点后,鼠标指向字段,会获得的提示信息

[TooltipAttribute("这是这个字段的提示信息")]

public string toolTip;

}

上一篇下一篇

猜你喜欢

热点阅读