[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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM