zoukankan      html  css  js  c++  java
  • [UnityAPI]MenuItem属性

    参考链接:

    https://blog.csdn.net/qq_33337811/article/details/72852342

    https://blog.csdn.net/zxl321365712/article/details/80080586

    0.定义

    itemName表示菜单路径

    isValidateFunction表示该方法是否为验证方法

    priority表示菜单优先级

    使用方法:

     1 using UnityEditor;
     2 using UnityEngine;
     3 
     4 public class TestMenuItem
     5 {
     6     [MenuItem("GameObject/Test41", false)]
     7     private static void Test41()
     8     {
     9         Debug.Log("Test41");
    10     }
    11 }

    1.参数itemName

    a.与视图的对应关系

    Assets->Project视图

    GameObject->Hierarchy视图

    Componemt->Inspector视图的AddComponent窗口

    b.快捷键设置

    _w表示字母w

    #w表示shift+w

    %w表示ctrl+w

    &w表示alt+w

    直接按快捷键就能调用,支持混合,注意前面要加空格,使用如下:

    [MenuItem("GameObject/Test41 %#w", false, 41)]

    2.参数isValidateFunction

    true表示该方法是该菜单项的检测方法,false表示该方法是该菜单项的执行方法,默认为false

    在检测方法中,返回true表示该菜单项可以点击,否则不可以点击。如下表示选中一个东西才可以点击

     1 using UnityEditor;
     2 using UnityEngine;
     3 
     4 public class TestMenuItem
     5 {
     6     [MenuItem("GameObject/Test41 %#w", true, 41)]
     7     private static bool Test41ValidateFunc()
     8     {
     9         return Selection.activeObject != null;
    10     }
    11 
    12     [MenuItem("GameObject/Test41 %#w", false, 41)]
    13     private static void Test41()
    14     {
    15         Debug.Log("Test41");
    16     }
    17 }

    3.参数priority

    表示优先级,越大越后,默认为1000即放最后

    a.分割线

    当相邻的两个菜单项优先级相差>10时,表示不同组,这时会有分割线

     1 using UnityEditor;
     2 using UnityEngine;
     3 
     4 public class TestMenuItem
     5 {
     6     [MenuItem("GameObject/Test30", false, 30)]
     7     private static void Test30()
     8     {
     9         Debug.Log("Test30");
    10     }
    11 
    12     [MenuItem("GameObject/Test41", false, 41)]
    13     private static void Test41()
    14     {
    15         Debug.Log("Test41");
    16     }
    17 }

    b.相同优先级的情况下,先定义的在前面,不会出现覆盖的情况

    c.相同菜单路径的情况下,后定义的会覆盖先定义的。unity内置的菜单项同样也可以被覆盖

    d.GameObject/下的菜单项优先级<50的才会显示在Hierarchy视图中

    4.为组件添加菜单项

    使用方法:[MenuItem("CONTEXT/组件名/显示方法名")]

     1 using UnityEditor;
     2 using UnityEngine;
     3 using System.Collections.Generic;
     4 using System.Text;
     5 
     6 public class TestMenuItem
     7 {
     8     [MenuItem("CONTEXT/TestEditorComponent/TestTest")]
     9     private static void TestTest(MenuCommand cmd)
    10     {
    11         TestEditorComponent comp = cmd.context as TestEditorComponent;
    12         List<Transform> traList = comp.traList;
    13         StringBuilder stringBuilder = new StringBuilder();
    14         for (int i = 0; i < traList.Count; i++)
    15         {
    16             string temp = string.Format("{0}_{1}", traList[i].gameObject.name, i);
    17             stringBuilder.AppendLine(temp);
    18         }
    19         TextEditor textEditor = new TextEditor();
    20         textEditor.text = stringBuilder.ToString();
    21         textEditor.OnFocus();
    22         textEditor.Copy();
    23     }
    24 }

  • 相关阅读:
    Perl 杂记
    Block abstraction view(Create & Reference)
    sed & awk
    multi-voltage design apr
    APR Recipe
    IN2REG group 的时序分析
    关于 clock tree
    ICC Stage Flow
    ocv & derate & crpr
    clock gating check
  • 原文地址:https://www.cnblogs.com/lyh916/p/13061296.html
Copyright © 2011-2022 走看看