当前位置:   article > 正文

【Unity】Unity Editor菜单按钮扩展_unity eiditer按钮

unity eiditer按钮

Unity官方教程:http://unity3d.com/cn/learn/tutorials/modules/intermediate/editor/menu-items?playlist=17090

本博客仅供自己记录要点,一切以官方文档为准。

我们可以通过

using UnityEngine;
using UnityEditor;

public class MenuItems
{
    [MenuItem("Tools/Clear PlayerPrefs")]
    private static void NewMenuOption()
    {
        PlayerPrefs.DeleteAll();
    }
}

来扩展已有/自定义菜单按钮,上述代码中的Tools可替换为Window这样的原有菜单项。

热键

  • % – CTRL on Windows / CMD on OSX

  • # – Shift

  • & – Alt

  • LEFT/RIGHT/UP/DOWN – Arrow keys

  • F1…F2 – F keys

  • HOME, END, PGUP, PGDN

eg.

// Add a new menu item with hotkey CTRL-SHIFT-A

[MenuItem("Tools/New Option %#a")]
private static void NewMenuOption()
{
}

// Add a new menu item with hotkey CTRL-G

[MenuItem("Tools/Item %g")]
private static void NewNestedOption()
{
}

// Add a new menu item with hotkey G
[MenuItem("Tools/Item2 _g")]
private static void NewOptionWithHotkey()
{
}

特殊路径

上面文章中讲到的按钮路径都是普通的位于菜单栏的按钮,但是Untiy是有一些特殊的路径的,比如:

  • Assets – items will be available under the “Assets” menu, as well using right-click inside the project view.(我们在Unity的Project下面右击会出现的)

  • Assets/Create – items will be listed when clicking on the “Create” button in the project view (useful when adding new types that can be added to the project)(Unity的Project目录下右击->Create下面)

  • CONTEXT/ComponentName – items will be available by right-clicking inside the inspector of the given component.(在面板上的组件上右击会出现)

Validation/验证?

有些按钮只有在右击某类东西时才会起作用,所以我们需要验证,例子如下

[MenuItem("Assets/ProcessTexture")]
private static void DoSomethingWithTexture()
{
}

// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/ProcessTexture", true)]
private static bool NewMenuOptionValidation()
{
    // This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
    return Selection.activeObject.GetType() == typeof(Texture2D);
}

效果:只有在Texture2D上才会有用
这里写图片描述

按钮优先级

[MenuItem("NewMenu/Option1", false, 1)]
private static void NewMenuOption()
{
}

[MenuItem("NewMenu/Option2", false, 2)]
private static void NewMenuOption2()
{
}

[MenuItem("NewMenu/Option3", false, 3)]
private static void NewMenuOption3()
{
}

[MenuItem("NewMenu/Option4", false, 51)]
private static void NewMenuOption4()
{
}

[MenuItem("NewMenu/Option5", false, 52)]
private static void NewMenuOption5()
{
}

效果:以10为单位一组
列表内容

End.除此之外还有MenuCommand、ContextMenu、ContextMenuItem等几个类,跟MenuItem差不多,看顶上的链接吧,另外有

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1022355
推荐阅读
相关标签
  

闽ICP备14008679号