Unity 编辑器:禁用父窗口内容但不关闭它
在 Unity 编辑器中,您可以使用 EditorWindow 类创建窗口。如果您希望在子窗口中禁用但不关闭父窗口,可以在子窗口中使用 EditorGUIUtility.SetEnabled 方法来禁用父窗口的 GUI 元素。
以下是一个示例代码,演示如何在子窗口中禁用父窗口的 GUI 元素:
using UnityEditor;
using UnityEngine;
public class MyWindow : EditorWindow
{
bool isEnabled = true;
[MenuItem("Window/My Window")]
public static void ShowWindow()
{
EditorWindow.GetWindow<MyWindow>('My Window');
}
void OnGUI()
{
isEnabled = EditorGUILayout.Toggle('Enable', isEnabled);
if (isEnabled)
{
EditorGUIUtility.SetEnabled(true); // enable GUI elements in parent window
}
else
{
EditorGUIUtility.SetEnabled(false); // disable GUI elements in parent window
}
EditorGUILayout.LabelField('This is a label in parent window');
if (GUILayout.Button('Open Child Window'))
{
ChildWindow.ShowWindow(this); // open child window
}
}
}
public class ChildWindow : EditorWindow
{
static ChildWindow window;
MyWindow parentWindow;
public static void ShowWindow(MyWindow parent)
{
window = EditorWindow.GetWindow<ChildWindow>('Child Window');
window.parentWindow = parent;
}
void OnGUI()
{
EditorGUILayout.LabelField('This is a label in child window');
if (GUILayout.Button('Close'))
{
Close();
}
}
void OnDestroy()
{
EditorGUIUtility.SetEnabled(true); // enable GUI elements in parent window when child window closes
}
void OnFocus()
{
EditorGUIUtility.SetEnabled(false); // disable GUI elements in parent window when child window is focused
}
void OnLostFocus()
{
EditorGUIUtility.SetEnabled(true); // enable GUI elements in parent window when child window loses focus
}
}
在这个示例中,当在父窗口中点击“Open Child Window”按钮时,会打开一个子窗口。在子窗口中,父窗口的 GUI 元素被禁用,但父窗口本身并没有关闭。当子窗口关闭时,父窗口的 GUI 元素会被重新启用。
原文地址: https://www.cveoy.top/t/topic/lLbF 著作权归作者所有。请勿转载和采集!