GUI.ModalWindow 不是带有按钮的模态
GUI.ModalWindow not modal with buttons
我正在尝试使用 GUI.ModalWindow 来阻止用户与场景中的其他项目交互,但仍然可以在 ModalWindow 之外使用按钮。
public class ModalDialog : MonoBehaviour
{
private Rect windowRect = new Rect(100, 130, 220, 100);
void OnGUI()
{
windowRect = GUI.ModalWindow(0, windowRect, DoMyWindow, "My Window");
}
void DoMyWindow(int windowID)
{
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
{
print("Got a click");
}
}
}
将上面的 class(摘自 the Unity docs)附加到 Canvas,我可以单击屏幕上的两个按钮:
有什么我遗漏的吗?
为了回答我自己的问题,我联系了 Unity 支持并收到以下回复:
UI components which you create manually, buttons, text etc behave separately from GUI you create trough script because these are 2 separate classes. So the behavior You get is expected and not a bug.
对我来说,这使得 ModalWindow 的使用有些受限,因为您无法使用 Unity 编辑器创建 UI。
为了解决我的问题,我放弃了 ModalWindow,而是创建了一个 Canvas
作为我的根游戏对象。我添加了一个 Panel
游戏对象作为 canvas 的子对象,并确保它出现在所有其他 UI 元素之上。然后当面板显示时,它下面的UI元素无法交互。
Scene
|- Canvas
|- Modal Panel
|- UI Button
我正在尝试使用 GUI.ModalWindow 来阻止用户与场景中的其他项目交互,但仍然可以在 ModalWindow 之外使用按钮。
public class ModalDialog : MonoBehaviour
{
private Rect windowRect = new Rect(100, 130, 220, 100);
void OnGUI()
{
windowRect = GUI.ModalWindow(0, windowRect, DoMyWindow, "My Window");
}
void DoMyWindow(int windowID)
{
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
{
print("Got a click");
}
}
}
将上面的 class(摘自 the Unity docs)附加到 Canvas,我可以单击屏幕上的两个按钮:
有什么我遗漏的吗?
为了回答我自己的问题,我联系了 Unity 支持并收到以下回复:
UI components which you create manually, buttons, text etc behave separately from GUI you create trough script because these are 2 separate classes. So the behavior You get is expected and not a bug.
对我来说,这使得 ModalWindow 的使用有些受限,因为您无法使用 Unity 编辑器创建 UI。
为了解决我的问题,我放弃了 ModalWindow,而是创建了一个 Canvas
作为我的根游戏对象。我添加了一个 Panel
游戏对象作为 canvas 的子对象,并确保它出现在所有其他 UI 元素之上。然后当面板显示时,它下面的UI元素无法交互。
Scene
|- Canvas
|- Modal Panel
|- UI Button