ScriptableWizard類繼承自EditorWindow類,因此在繼承自ScriptableWizard的類中也能夠自定義窗口中的內容,只不過ScriptableWizard爲簡單的窗口需求提供了便捷ide
——建立窗口
函數
using UnityEditor; public class ModificationValue : ScriptableWizard { public float value; [MenuItem("Tools/Create Wizard")] private static void CreateWizard() { DisplayWizard<ModificationValue>("統一修改", "修改並退出", "修改不退出"); } }
——定義屬性
3d
using UnityEditor; public class ModificationValue : ScriptableWizard { public float targetValue; [MenuItem("Tools/Create Wizard")] private static void CreateWizard() { DisplayWizard<ModificationValue>("統一修改", "修改並退出", "修改不退出"); } }
——建立按鈕與其餘按鈕的監聽
建立按鈕對應DisplayWizard的第一個參數,其餘按鈕對應DisplayWizard的第二個參數code
using UnityEditor; using UnityEngine; public class ModificationValue : ScriptableWizard { public float targetValue; [MenuItem("Tools/Create Wizard")] private static void CreateWizard() { DisplayWizard<ModificationValue>("統一修改", "修改並退出", "修改不退出"); } /// <summary> /// 按下建立按鈕時 /// </summary> private void OnWizardCreate() { GameObject[] gos = Selection.gameObjects; foreach (var go in gos) { Test _Test = go.GetComponent<Test>(); if (_Test != null) { //記錄撤銷操做 //Undo.RecordObject(_Test, "value"); _Test.value = targetValue; } } } /// <summary> /// 按下其餘按鈕時 /// </summary> private void OnWizardOtherButton() { Debug.Log("點擊了其餘按鈕"); } }
——ScriptableWizard類中的回調函數
OnWizardUpdate觸發條件:窗口被建立出來和窗口中的數據更新時會調用(不止調用一次)——ScriptableWizard類中的回調
OnSelectionChange觸發條件:當選擇的物體改變時——EditorWindow類中的回調blog
/// <summary> /// 當窗口中的數據更新時 /// </summary> private void OnWizardUpdate() { Debug.Log("當窗口中的數據更新時"); } /// <summary> /// 當選擇的物體改變時 /// </summary> private void OnSelectionChange() { Debug.Log("當選擇的物體改變時"); }
——提示信息
繼承
private void OnSelectionChange() { errorString = ""; helpString = ""; if (Selection.gameObjects.Length <= 0) { errorString = "沒有選擇預製體"; } else { helpString = "當前選擇了" + Selection.gameObjects.Length + "個預製體"; } }