将 EditorWindow 显示为一个浮动实用程序窗口。
当实用程序窗口失去焦点时,它会保持在新活动窗口的顶部。这意味着 EditorWindow.ShowUtility 窗口永远不会被 Unity 编辑器隐藏。但是,它不能停靠到编辑器中。
实用程序窗口将始终位于普通 Unity 窗口的前面。当用户从 Unity 切换到其他应用程序时,它将被隐藏。
注意:在使用此函数显示窗口之前,无需使用 EditorWindow.GetWindow。
// Simple script that randomizes the rotation of the selected GameObjects. using UnityEditor; using UnityEngine; using UnityEngine.UIElements; public class RandomizeInSelectionShowUtility : EditorWindow { System.Random random = new System.Random(); public float rotationAmount; public string selected = ""; [MenuItem("Examples/Randomize Objects")] static void Init() { RandomizeInSelectionShowUtility window = EditorWindow.GetWindow<RandomizeInSelectionShowUtility>(true, "Randomize Objects"); window.ShowUtility(); } void CreateGUI() { var label = new Label("Selected an object and click Randomize!"); rootVisualElement.Add(label); var buttonRandomize = new Button(); buttonRandomize.text = "Randomize!"; buttonRandomize.clicked += () => RandomizeSelected(); rootVisualElement.Add(buttonRandomize); } void RandomizeSelected() { foreach (var transform in Selection.transforms) { Quaternion rotation = Random.rotation; rotationAmount = (float)random.NextDouble(); transform.localRotation = Quaternion.Slerp(transform.localRotation, rotation, rotationAmount); } } }