有unity中的菜單欄是咱們常用到的地方,以下圖:html
MenuItem的做用就是增長一個本身的菜單函數
使用方法:spa
在工程中Assets目錄下任意一個Editor目錄(之後簡稱Editor目錄,若是不存在,請自行建立)中建立任意腳本,好比我建立了叫作MenuTest .cs,雙擊進入代碼編輯,建立一個靜態函數,(代碼來自官方文檔http://docs.unity3d.com/Documentation/ScriptReference/MenuItem.html)以下:3d
public class MenuTest : MonoBehaviour { // Add a menu item named "Do Something" to MyMenu in the menu bar. [MenuItem ("MyMenu/Do Something")] static void DoSomething () { Debug.Log ("Doing Something..."); } }
保存回到unity,編譯好以後會發如今菜單欄上多了一個MyMenu,點擊以後會出現二級選擇菜單Do Something。點擊Do Something會在輸出窗口輸出Doing Something...code
是的,MenuItem的用法就是這麼簡單,將[MenuItem()]標籤放在一個靜態函數上,括號內部就是菜單層級。xml
public class MenuTest : MonoBehaviour { // Add a menu item named "Do Something" to MyMenu in the menu bar. [MenuItem ("MyMenu/Do Something")] static void DoSomething () { Debug.Log ("Doing Something..."); } [MenuItem ("Assets/MyMenu")] static void DoSomething () { Debug.Log ("MyMenu Click"); } }
要補充的是,菜單層級能夠是系統自身的層級,好比Window菜單,Assets菜單等。例如htm
這樣就會在系統的Assets菜單下生成一個MyMenu菜單。blog
對於Assets菜單,就是project窗口的右鍵菜單。因此,利用這個能夠建立文件的右鍵菜單,好比建立一個xml文件等等ip
ok,這就是MenuItem的基本功能,再高級的用法,好比菜單的快捷鍵,不可用狀態等,就見官網吧。文檔