前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >看别人在Unity的编辑器上添加菜单那么帅,我也要给Unity添加菜单

看别人在Unity的编辑器上添加菜单那么帅,我也要给Unity添加菜单

作者头像
恬静的小魔龙
发布2022-08-07 09:03:02
8480
发布2022-08-07 09:03:02
举报
文章被收录于专栏:Unity3DUnity3D

一、前言

今天分享,如何添加自定义菜单栏,效果如下图所示。

第一种,在Component组件菜单下面:

在这里插入图片描述
在这里插入图片描述

第二种,添加到编辑器的菜单栏下:

在这里插入图片描述
在这里插入图片描述

二、添加组件菜单

有两个重载函数:

在这里插入图片描述
在这里插入图片描述

第一种,不带参数实现:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[AddComponentMenu("Tools/自定义菜单")]
public class addTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

第二种,使用参数:

menuName:菜单名 order:排序

代码语言:javascript
复制
public AddComponentMenu(string menuName, int order);

比如说,我在Tools菜单下有很多个子菜单,但是为了控制它们之间的排序情况,就可以使用order参数,如下所示:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[AddComponentMenu("Tools/自定义菜单1",1)]
public class addTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[AddComponentMenu("Tools/自定义菜单2",2)]
public class addTest2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

这样,自定义菜单1就会一直排在自定义菜单2的前面了。

在这里插入图片描述
在这里插入图片描述

三、添加菜单栏菜单

代码:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class addTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    [MenuItem("Tools/菜单栏菜单")]
    static void Test()
    {

    }
}
在这里插入图片描述
在这里插入图片描述

当然,这个也可以使用priority参数排序优先级。 isValidateFunction验证函数: 如果isValidateFunction为 true,它将表示一个验证 函数,并在系统调用具有相同 itemName 的菜单函数之前进行调用。

在这里插入图片描述
在这里插入图片描述

这里就不演示了,有需要可以验证一下。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、添加组件菜单
  • 三、添加菜单栏菜单
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档