Unity3D中的Attribute详解(五)
今天主要来讲一下Unity中带Menu的Attribute。
首先是AddComponentMenu。这是UnityEngine命名空间下的一个Attribute。
按照官方文档的说法,会在Component的菜单下面生成对应的菜单栏。选择预制或者GameObject,再点击菜单项,自动添加该Component。
下面我们来试试,我在Editor文件夹下面新建了一个OhGod.cs的文件,取完名字我就后悔了,于是我修改了类名,代码如下:

完美!我们来看看Component菜单。。。什么也没有多!!
于是我翻了网上的资料,了解到这个属性有个暗坑,就是类名必须和cs的文件名一致!
好吧,我改回去。
然而,还是什么也没有。。。
这时候我想到Editor是一个特殊的文件夹,会不会和这个有关系呢。
把cs脚本移动到其他位置,这时候,菜单显示就正确了!
选中场景中物件或者文件夹里的预制体,点击菜单,脚本就自动添加上去拉~

需要注意的是,我在菜单里命名为cc/dd,如果你直接点击上图的Add Component按钮,是找不到OhGod这个脚本的!只有cc和dd才能找到。
AddComponentMenu构造函数还有一个参数是优先级,可以设置同级菜单下面的菜单项的顺序,这里不做赘述了。
如果我们有其他的菜单功能的话,最好能够新建一个菜单项。放在Component里肯定是不太好了。
Unity继承了这个功能,不过这个Attribute在Editor命名空间里,就是UnityEditor.MenuItem。
看MenuItem的代码定义:
using System;
using UnityEngine.Scripting; namespace UnityEditor
{
// 摘要:
// The MenuItem attribute allows you to add menu items to the main menu and
// inspector context menus.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
[RequiredByNativeCode]
public sealed class MenuItem : Attribute
{
public string menuItem;
public int priority;
public bool validate; // 摘要:
// Creates a menu item and invokes the static function following it, when the
// menu item is selected.
//
// 参数:
// itemName:
// The itemName is the menu item represented like a pathname. For example the
// menu item could be "GameObject/Do Something".
//
// isValidateFunction:
// If isValidateFunction is true, this is a validation function and will be
// called before invoking the menu function with the same itemName.
//
// priority:
// The order by which the menu items are displayed.
public MenuItem(string itemName);
//
// 摘要:
// Creates a menu item and invokes the static function following it, when the
// menu item is selected.
//
// 参数:
// itemName:
// The itemName is the menu item represented like a pathname. For example the
// menu item could be "GameObject/Do Something".
//
// isValidateFunction:
// If isValidateFunction is true, this is a validation function and will be
// called before invoking the menu function with the same itemName.
//
// priority:
// The order by which the menu items are displayed.
public MenuItem(string itemName, bool isValidateFunction);
//
// 摘要:
// Creates a menu item and invokes the static function following it, when the
// menu item is selected.
//
// 参数:
// itemName:
// The itemName is the menu item represented like a pathname. For example the
// menu item could be "GameObject/Do Something".
//
// isValidateFunction:
// If isValidateFunction is true, this is a validation function and will be
// called before invoking the menu function with the same itemName.
//
// priority:
// The order by which the menu items are displayed.
public MenuItem(string itemName, bool isValidateFunction, int priority);
}
}
可以看到它的附着点在函数上,而且必须是静态函数。

我们还能够给菜单项添加热键,具体热键的定义如下:

修改我们的代码:


需要注意,热键重复的话,只会调用其中一条指令。
Unity中的右键菜单也可以使用MenuItem来定义,这里有三条特殊的路径:Assets;Assets/Create;CONTEXT/ComponentName。
修改代码如下:

其中上面两个菜单项,你可以在Project视图中右键找到,Create菜单还能在Project的Create按钮里找到新加项目。
最后一个菜单项则是在Inspector视图里,当你右键一个脚本的时候会有菜单弹出。上述例子中,Rigidbody限定了脚本。

MenuItem还有一个参数是验证(Validation),默认为false,如果设为true,则需要方法返回一个布尔值。
添加代码如下:

或者

这里比较坑爹的是同样的菜单项你必须写两次,一个用来回调,一个用来验证,只写下面的菜单是显示不出来的!

MenuItem的最后一个参数是索引值,可以排顺序。
在我们上述三种特殊路径的菜单中,最后一种绑定在RigidBody的菜单,我们可以在回调中获得脚本对象,只要修改代码如下:

在UnityEngine域名中还有ContextMenu和ContextMenuItem两个菜单属性,声明在UnityEngine的命名空间中。
ContextMenu和MenuItem+“CONTEXT/...”的显示效果是一致的,一般用在当前的脚本上(即ContextMenu定义的脚本上),回调方法不能是静态的方法。
代码如下:


ContextMenu构造函数还有一些参数和MenuItem基本一致,这里不再赘述。
ContextMenuItem的用法我们可以通过一个案例来了解一下:


只有在Random Name上面右键才会出现的菜单项。
至此我们关于Menu相关的Attribute讲完了。
Unity3D中的Attribute详解(五)的更多相关文章
- C#中的Attribute详解(下)
原文地址:https://blog.csdn.net/xiaouncle/article/details/70229119 C#中的Attribute详解(下) 一.Attribute本质 从上篇里我 ...
- Unity3D中的Coroutine详解
Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...
- 【Unity3D/C#】Unity3D中的Coroutine详解
Unity中的coroutine是通过yield expression;来实现的.官方脚本中到处会看到这样的代码. 疑问: yield是什么? Coroutine是什么? unity的coroutin ...
- js中的attribute详解
Attribute是属性的意思,文章仅对部分兼容IE和FF的Attribute相关的介绍.attributes:获取一个属性作为对象getAttribute:获取某一个属性的值object.getAt ...
- oracle中imp命令详解 .
转自http://www.cnblogs.com/songdavid/articles/2435439.html oracle中imp命令详解 Oracle的导入实用程序(Import utility ...
- .Net Attribute详解(下) - 使用Attribute武装枚举类型
接上文.Net Attribute详解(上)-Attribute本质以及一个简单示例,这篇文章介绍一个非常实用的例子,相信你一定能够用到你正在开发的项目中.枚举类型被常常用到项目中,如果要使用枚举To ...
- .NET DLL 保护措施详解(五)常规条件下的破解
为了证实在常规手段破解下能有效保护程序核心功能(演示版本对AES加解密算法及数据库的密钥(一段字符串)进行了保护),特对此DLL保护思路进行相应的测试,包含了反编译及反射测试,看是否能得到AES加解密 ...
- 转:Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。
原文来自于:http://www.ituring.com.cn/article/128439 Windows下的PHP开发环境搭建——PHP线程安全与非线程安全.Apache版本选择,及详解五种运行模 ...
- WCF中队列服务详解
WCF中队列服务详解 一.引言 在前面的WCF服务中,它都要求服务与客户端两端都必须启动并且运行,从而实现彼此间的交互.然而,还有相当多的情况希望一个面向服务的应用中拥有离线交互的能力.WCF通过服务 ...
- .Net Attribute详解(一)
.Net Attribute详解(一) 2013-11-27 08:10 by JustRun, 1427 阅读, 14 评论, 收藏, 编辑 Attribute的直接翻译是属性,这和Property ...
随机推荐
- impdp,depdp 常用参数
转载于:https://www.cnblogs.com/halberd-lee/p/7807032.html 1 导数据注意事项 检查数据库版本(用于决定导出时生成为哪个版本的dmp头文件) sele ...
- c++学习 5 预处理
一 内存分区 内存的分区变量存储,一般可以分为以下五个区,它们分别是: 可读可写 堆区:使用malloc.calloc.realloc.free以及c++里面的new和delete去动态申请. ...
- uniapp 报错 签名不对 请检查签名是否与开放平台上填写的一致
问题描述 用签名工具 输入包名 获取签名 在微信开放平台申请app 用获取的签名申请 申请成功后 在hbuilderx上云打包apk 分享 报 签名不对 请检查签名是否与开放平台上填写的一致 ...
- 【jenkins】jenkins 持续集成本地项目(win)
[项目]--->[配置]: [源码管理]= 无 [Pre Steps]:(因为是windows下,本地装有maven,直接用maven构建,又是个测试用例,就直接用mvn test) 选择win ...
- verilog 进制的表示
n'b 是二进制 n'h 是十六进制 n'd 是四进制 n是位数
- vue组件 子组件没有事件怎么 向父组件传递数据
通过ref去接收值!!! 需求图片 代码实现 //----------父组件 <div class="fingerprint-bottom"> <el-tabs ...
- JAVA——》微信分账
做一个推荐分享的功能,场景:每推荐成功奖励推荐人一定的推荐金. 这里,我就用调用了微信支付分账接口.链接:https://pay.weixin.qq.com/wiki/doc/api/allocati ...
- 4组-Alpha冲刺-总结
组长博客链接 一.基本情况 1.1现场答辩总结 1.1.1柯老师的建议与问题: 界面不够美观,要求达到看不出来是学生作品的水平. 答:好的,我们会进一步改进. alpha完成程度? 答:完成到60%以 ...
- nginx二进制安装脚本
#!/bin/bash NGINX_FILE=nginx-1.22.0#NGINX_FILE=nginx-1.20.2#NGINX_FILE=nginx-1.18.0NGINX_URL=http:// ...
- PTA1002 写出这个数 (20 分)
1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...