将 EditorWindow 显示为一个浮动模态窗口。
编辑器运行时,您无法与实用程序窗口交互。 EditorWindow.ShowModalUtility 窗口永远不会被编辑器隐藏。您无法将实用程序窗口停靠到编辑器。
实用程序窗口始终位于普通 Unity 窗口的前面。当用户从 Unity 切换到另一个应用程序时,实用程序窗口会被隐藏。
注意:在使用此函数显示窗口之前,您无需使用 EditorWindow.GetWindow。
// Simple script that randomizes the rotation of the selected GameObjects. using UnityEditor; using UnityEngine; using UnityEngine.UIElements; public class RandomizeInSelection : EditorWindow { System.Random random = new System.Random(); public float rotationAmount; public string selected = ""; [MenuItem("Examples/Randomize Objects")] static void Init() { RandomizeInSelection window = EditorWindow.GetWindow<RandomizeInSelection>(true, "Randomize Objects"); window.ShowModalUtility(); } void CreateGUI() { var label = new Label("Select an object and click the Randomize! button"); rootVisualElement.Add(label); var randomizeButton = new Button(); randomizeButton.text = "Randomize!"; randomizeButton.clicked += () => RandomizeSelected(); rootVisualElement.Add(randomizeButton); } void RandomizeSelected() { foreach (var transform in Selection.transforms) { Quaternion rotation = Random.rotation; rotationAmount = (float)random.NextDouble(); transform.localRotation = Quaternion.Slerp(transform.localRotation, rotation, rotationAmount); } } }