今天主要来讲一下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域名中还有ContextMenuContextMenuItem两个菜单属性,声明在UnityEngine的命名空间中。

ContextMenu和MenuItem+“CONTEXT/...”的显示效果是一致的,一般用在当前的脚本上(即ContextMenu定义的脚本上),回调方法不能是静态的方法。

代码如下:

ContextMenu构造函数还有一些参数和MenuItem基本一致,这里不再赘述。

ContextMenuItem的用法我们可以通过一个案例来了解一下:

只有在Random Name上面右键才会出现的菜单项。

至此我们关于Menu相关的Attribute讲完了。

从Unity中的Attribute到AOP(五)的更多相关文章

  1. 从Unity中的Attribute到AOP(七)

    本章我们将依然讲解Unity中的Attribute,继续命名空间在UnityEngine里的. PropertyAttribute,这个特性主要来控制变量或者类在Inspector里面的显示方式.和P ...

  2. 从Unity中的Attribute到AOP(四)

    本篇我们将逐一讲解Unity中经常使用的Attribute(Unity对应的文档版本为2018.1b). 首先是Serializable,SerializeField以及NonSerialized,H ...

  3. 从Unity中的Attribute到AOP(三)

    上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...

  4. 从Unity中的Attribute到AOP(六)

    本文将重点对Unity剩下常用的Attribute进行讲解,其他不常用的Attribute各位可以自行去官方文档查阅. 首先是UnityEngine命名空间下的. ColorUsage,这个主要作用于 ...

  5. 从Unity中的Attribute到AOP(八)

    本文将讲一下在UnityEditor命名空间下的一些特性. CallBackOrder,这个Attribute是所有带callback index的Attribute的基类,由于官方也没有给出详细的说 ...

  6. 从Unity中的Attribute到AOP(二)

    上一篇文章我们初步了解了一下Attributes的含义,并且使用系统自带的Attributes写了点代码.在进一步解剖我们的代码之前,我觉得有个概念可能需要巩固一下:什么是元数据? 我们知道C#代码会 ...

  7. 从Unity中的Attribute到AOP(一)

    首先来看一下微软官方对Attributes(C#)的定义: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/conce ...

  8. Unity中使用Attribute

    Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...

  9. Unity应用架构设计(12)——AOP思想的实践

    想象一下,当程序所有的业务逻辑都完成的时候,你可能还来不及喘口气,紧张的测试即将来临.你的Boss告诉你,虽然程序没问题,但某些方法为什么执行这么慢,性能堪忧.领会了Boss的意图之后,漫长的排查问题 ...

随机推荐

  1. [UWP]了解模板化控件(10):原则与技巧

    1. 原则 推荐以符合以下原则的方式编写模板化控件: 选择合适的父类:选择合适的父类可以节省大量的工作,从UWP自带的控件中选择父类是最安全的做法,通常的选择是Control.ContentContr ...

  2. Structured Streaming从Kafka 0.8中读取数据的问题

    众所周知,Structured Streaming默认支持Kafka 0.10,没有提供针对Kafka 0.8的Connector,但这对高手来说不是事儿,于是有个Hortonworks的邵大牛(前段 ...

  3. SPCircleView的使用(圆心向四周扩散动画)

    今天封装了一个动画,想着以后可能会用,就封装了一下.欢迎下载 https://github.com/USimpleLife/SPCircleView 参数说明 @param centerPoint 中 ...

  4. ABP PUT、DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法(HTTP 谓词) 引发客户端错误 No 'Access-Control-Allow-Origin' header is present on the requested resource

    先请检查是否是跨域配置问题,请参考博客:http://www.cnblogs.com/donaldtdz/p/7882225.html 一.问题描述 ABP angular前端部署后,查询,新增都没问 ...

  5. [WinForm]委托应用①——窗口之间方法/控件调用

    不传参数 第二窗口:public partial class Form2 : Form { /// <summary> /// 定义委托 /// </summary> publ ...

  6. 单源最短路径(1):Dijkstra 算法

    一:背景 Dijkstra 算法(中文名:迪杰斯特拉算法)是由荷兰计算机科学家 Edsger Wybe Dijkstra 提出.该算法常用于路由算法或者作为其他图算法的一个子模块.举例来说,如果图中的 ...

  7. C#中级-Windows Service程序安装注意事项

    一.前言 这周除了改写一些识别算法外,继续我的Socket服务编写.服务器端的Socket服务是以Windows Service的形式运行的. 在我完成Windows Service编写后,启动服务时 ...

  8. Effective Java 第三版——11. 重写equals方法时同时也要重写hashcode方法

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. Oracle ADG搭建

    Oracle Active Data Guard搭建 一:安装 1.基础环境配置 1.1.开启强制日志记录 DG日志发送方式中ARCH进程和LGWR进程的ASYNC模式都是基于日志同步的,所以我们必须 ...

  10. ConcurrentHashMap 从Java7 到 Java8的改变

    一.关于分段锁 集合框架很大程度减少了java程序员的重复劳动,然而,在Java多线程环境中,以线程安全的方式使用集合类是一个首先考虑的问题. 越来越多的程序员了解到了ConcurrentHashMa ...