前面我們已經寫了5篇關於編輯器的,這是第六篇,也是最后一篇:
Unity3D編輯器擴展(五)——常用特性(Attribute)以及Selection類
一般來說,Windows應用程序中,對話框分為模態對話框和非模態對話框兩種。二者的區別在於當對話框打開時,是否允許用戶進行其他對象的操作。
“模態”:模態對話框(Modal Dialogue Box,又叫做模式對話框),是指在用戶想要對對話框以外的應用程序進行操作時,必須首先對該對話框進行響應。如單擊【確定】或【取消】按鈕等將該對話框關閉。
下面我們就使用 EditorUtility 類來在 Untiy 里面實現模態對話框:
Unity 里面有兩種模態對話框,分別如下:
第一種:
代碼:
using UnityEngine; using UnityEditor; public class DialogTest : MonoBehaviour { [MenuItem("MyWindow/DialogTest")] private static void CreateDialog() { if (EditorUtility.DisplayDialog("對話框標題", "對話框的消息", "OK", "Cancel")) { Debug.Log("您點擊了OK按鈕"); } else { Debug.Log("您點擊了Cancel按鈕"); } } }
效果圖:
第二種:
代碼:
using UnityEngine; using UnityEditor; public class DialogTest : MonoBehaviour { [MenuItem("MyWindow/DialogComplexTest")] private static void CreateDialogComplex() { int _buttonID = EditorUtility.DisplayDialogComplex("對話框標題", "對話框的消息", "Save", "Don't Save", "Cancel"); switch (_buttonID) { case 0: Debug.Log("您點擊了Save按鈕"); break; case 1: Debug.Log("您點擊了Don't Save按鈕"); break; case 2: Debug.Log("您點擊了Cancel按鈕"); break; default: Debug.Log("Error!"); break; } } }
效果圖:
這就是 Unity 里面的模態對話框,代碼很簡單,我就不做過多解釋了,大家自己看一下效果就行。
我們還可以使用 EditorUtility 類實現進度條,但不是模態模式。
代碼:
using UnityEngine; using UnityEditor; public class EditorUtilityTest : EditorWindow { private float waitTime = 10.0f; private float currentTime = 0f; private float progressValue = 0f; private bool isShow = false; [MenuItem("MyWindow/ProgressBarTest")] private static void Init() { var window = GetWindow(typeof(EditorUtilityTest)); window.Show(); } private void OnGUI() { waitTime = EditorGUILayout.FloatField("Time to wait:", waitTime); if (GUILayout.Button("Display bar")) { isShow = !isShow; } if (currentTime <= waitTime && isShow == true) { //顯示更新一個進度條 EditorUtility.DisplayProgressBar("進度條演示", "一個簡單的進度條", progressValue); currentTime += Time.deltaTime; progressValue = currentTime / waitTime; } else { progressValue = 0.0f; currentTime = 0.0f; isShow = false; //刪除進度條 EditorUtility.ClearProgressBar(); return; } } private void OnInspectorUpdate() { Repaint(); } }
效果圖:
EditorUtility 這個編輯器類還有一些其他的用途,我就不再這里給大家一一介紹了。大家可以去看一下官方文檔:https://docs.unity3d.com/ScriptReference/EditorUtility.DisplayDialogComplex.html
Unity編輯器相關內容,到這里就結束了,記錄給自己看的,同時也希望能幫助到大家,謝謝啦!