注意事項(xiàng):
必須在Assets/Editor文件下創(chuàng)建腳本
必須static微酬,這樣才能通過類名調(diào)用,否則需要通過對象調(diào)用
需要using UnityEngine;
(一)MenuItem添加菜單按鈕
[MenuItem("tools/test")] static void test() { Debug.Log("test"); }
效果 菜單欄出現(xiàn)tools,tools下有test,點(diǎn)擊test將執(zhí)行test()
(二)菜單優(yōu)先級
[MenuItem("tools/test", false, 444)] static void test() { Debug.Log("test"); }
[MenuItem("tools/test", false, 44)] static void test3() { Debug.Log("test3"); }
[MenuItem("tools/test2", false, 3444)] static void test2() { Debug.Log("test2"); }
效果 第三個(gè)參數(shù)為priority決定了優(yōu)先級画恰,越小同一級下越靠前
(三)project右鍵菜單
[MenuItem("Assets/test2", false, 3444)] static void test2() { Debug.Log("test2"); }
效果 (四)inspect某組件區(qū)域右鍵菜單
[MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]// CONTEXT 組件名 按鈕名
static void InitHealthAndSpeed() { Debug.Log("Init"); }
必須是 “CONTEXT/組件名/菜單名”
以下方法可獲取到操作的組件
[MenuItem("CONTEXT/PlayerHealth/InitHealthAndSpeed")]// CONTEXT 組件名 按鈕名
static void InitHealthAndSpeed( MenuCommand cmd )//menucommand是當(dāng)前正在操作的組件
{
//Debug.Log(cmd.context.GetType().FullName);
CompleteProject.PlayerHealth health = cmd.context as CompleteProject.PlayerHealth;
health.startingHealth = 200;
health.flashSpeed = 10;
Debug.Log("Init");
}
(五)獲取選擇物體,進(jìn)行操作
[MenuItem("GameObject/my delete", true, 11)]
static bool MyDeleteValidate()
{
Debug.Log(Selection.activeGameObject.name );//Selection.activeGameObject是我們第一個(gè)選擇的游戲物體
if (Selection.objects.Length > 0)//Selection.objects獲得選擇所有物體
return true;
else
return false;
}
刪除選擇物體
[MenuItem("GameObject/my delete", false, 11)]
static void Mydelete()
{
foreach (Object o in Selection.objects)
{
//GameObject.DestroyImmediate(o);//無法撤銷
Undo.DestroyObjectImmediate(o);//利用Undo進(jìn)行的刪除操作 是可以撤銷的
}
//需要把刪除操作注冊到 操作記錄里面
}
(六)快捷鍵
[MenuItem("Tools/test2 %q",false,100)]//快捷鍵為ctrl+q
static void Test2() { Debug.Log("Test2"); }
注意事項(xiàng) %=ctrl #=shift &=alt 記得加空格
(七)菜單是否可用
[MenuItem("GameObject/my delete", true, 11)]//true時(shí)無法點(diǎn)擊
static bool MyDeleteValidate()//驗(yàn)證方法蹲蒲,會最先執(zhí)行該方法梦染,返回true才能執(zhí)行其他方法
{
if (Selection.objects.Length > 0)
return true;
else
return false;
}
(八)ContextMenuItem和ContextMenu 不是在Editor下,而是在組件里 ContextMenu組件區(qū)域右鍵點(diǎn)擊
[ContextMenu("SetColor")] void SetColor() { flashColour = Color.green; }
ContextMenuItem是給某組件某個(gè)屬性右鍵點(diǎn)擊添加菜單
[ContextMenuItem("Add Hp","AddHp")] //Add Hp為菜單名稱void AddHp() { startingHealth += 20; }
(八)對話框 創(chuàng)建對話框
public class EnemyChange : ScriptableWizard {//必須繼承ScriptableWizard
[MenuItem("Tools/CreateWizard")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<EnemyChange>("統(tǒng)一修改敵人","Change And Close","Change");//Change And Close為按鈕名字
}
檢測對話框按鈕點(diǎn)擊
//檢測create按鈕的點(diǎn)擊 void OnWizardCreate() { }
//當(dāng)前對話框字段值修改的時(shí)候會被調(diào)用 void OnWizardUpdate() {
對話框第二個(gè)按鈕
ScriptableWizard.DisplayWizard<EnemyChange>("統(tǒng)一修改敵人","Change And Close","Change");//change為第二個(gè)button
第二個(gè)button的點(diǎn)擊事件猜憎,other button 不同于第一個(gè)button娩怎,點(diǎn)擊后不會自動關(guān)閉對話框
void OnWizardOtherButton()
(九)記錄操作,以便撤銷
Undo.RecordObject(hp, "change health and speed");
(十)顯示提示信息
void OnWizardCreate() {
ShowNotification(new GUIContent(Selection.gameObjects.Length + "個(gè)游戲物體的值被修改了"));
}
(十一)保存數(shù)據(jù)
EditorPrefs.SetInt(changeStartHealthValueKey, changeStartHealthValue);//第一個(gè)參數(shù)是key胰柑,第二個(gè)是保存的值
(十二)進(jìn)度條
void OnWizardCreate()
{
GameObject[] enemyPrefabs = Selection.gameObjects;
EditorUtility.DisplayProgressBar("進(jìn)度", "0/" + enemyPrefabs.Length + " 完成修改值", 0);
int count = 0;
foreach (GameObject go in enemyPrefabs)
{
CompleteProject.EnemyHealth hp = go.GetComponent<CompleteProject.EnemyHealth>();
Undo.RecordObject(hp, "change health and speed");
hp.startingHealth += changeStartHealthValue;
hp.sinkSpeed += changeSinkSpeedValue;
count++;
EditorUtility.DisplayProgressBar("進(jìn)度", count+"/" + enemyPrefabs.Length + " 完成修改值", (float)count/enemyPrefabs.Length);
}
EditorUtility.DisplayProgressBar("進(jìn)度", "0/" + enemyPrefabs.Length + " 完成修改值", 0);
(十三)自定義窗口 必須繼承EditorWindow
public class MyWindow : EditorWindow {
[MenuItem("Window/show mywindow")]
static void ShowMyWindow()
{
MyWindow window= EditorWindow.GetWindow<MyWindow>();
window.Show();
}
private string name="";
void OnGUI()
{
GUILayout.Label("這是我的窗口");
name = GUILayout.TextField(name);
if (GUILayout.Button("創(chuàng)建"))
{
GameObject go = new GameObject(name);
Undo.RegisterCreatedObjectUndo(go, "create gameobject");//注冊到操作記錄
}
}
本文轉(zhuǎn)自:http://blog.csdn.net/qq_34244317/article/details/76718284