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/xIl 著作权归作者所有。请勿转载和采集!