编译器属性
属性 | 介绍 | 用例 |
AddComponentMenu | 在Component菜单中添加新的菜单项 | [AddComponentMenu("Duan/Script/TestScript")] |
ContextMenu | 在当前脚本的组件中添加右键菜单内容 | |
ExecuteInEditMode | 让当前脚本可以在运行模式中实时更新修改 | |
HideInInspector | 是变量在检测时不被显示,但是会被实例化 | [HideInInspector]public int a; |
NonSerialized | 标记一个变量不会被序列化? | |
RPC | ||
RenderBeforeQueues | 附加一个自定义渲染 在整个引擎渲染队列之前 | |
RequireComponent | 强制添加一个组件,(限定一定要有这个个组件),并且不能删除 | [RequireComponent(typeof(Rigidbody))] |
Serializable | 序列化一个类 (js继承Object类默认就是会被序列化) | |
SerializeField | 序列化一个字段 (当unity序列你的脚本,它只会序列化公共字段。如果除了那些也想序列化私有字段,你可以添加SerializeField属性。) | [SerializeField]private int a; |
AddComponentMenu 添加组件菜单项
在编辑器添加一个用于添加组件的菜单项,将拥有该属性的脚本添加到选中的物体上。(用法:[AddComponentMenu("Duan/Script_Mobile/BreakAndEnd")])
例:在当前脚本中加入AddComponentMenu属性,选中某物体后,将拥有该属性的脚本添加到选中的物体上。
using UnityEngine;
using System.Collections; /// <summary>
/// 按返回退出应用
/// </summary>
[AddComponentMenu("Duan/Script_Mobile/BreakAndEnd")]
public class BreakAndEnd : MonoBehaviour { // Update is called once per frame
void Update () {
endGame();
} void endGame(){
if ( Application.platform == RuntimePlatform.Android &&
(Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home)) )
{
//Home键好像不一定能用。。默认就是把程序挂到后台,并不是退出。
Application.Quit();
}
}
}
MenuItem 自定义菜单项
在编辑器添加一个自定义菜单项(用法:[MenuItem("DuanEditor/init Scenes")])
例:菜单项启动一个自定义的编辑器窗口(EditorWindow ),该窗口试用OnGUI布局。
using UnityEngine;
using System.Collections;
using UnityEditor; public class initScenes : EditorWindow
{
[MenuItem("DuanEditor/init Scenes")]
static void Init()
{
initScenes window = (initScenes)EditorWindow.GetWindow(typeof(initScenes));
window.Show();
} void OnGUI() {
if (GUI.Button(new Rect(,,,),"hello world"))
{
Debug.Log("hello world");
}
}
}