【Unity3D编辑器扩展】Unity3D的特性AddComponentMenu添加菜单命令

推荐阅读

一、前言

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

第一种,在Component组件菜单下面:
在这里插入图片描述
第二种,添加到编辑器的菜单栏下:

在这里插入图片描述

二、添加组件菜单

有两个重载函数:
在这里插入图片描述
第一种,不带参数实现:

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:排序

public AddComponentMenu(string menuName, int order);

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

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()
    {
        
    }
}
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的前面了。
在这里插入图片描述

三、添加菜单栏菜单

代码:

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 的菜单函数之前进行调用。
在这里插入图片描述
这里就不演示了,有需要可以验证一下。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恬静的小魔龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值