简体   繁体   English

在 Unity 中单击按钮后如何显示模态窗口?

[英]How to show a modal window after button click in Unity?

I am new to Unity and C# programming.我是 Unity 和 C# 编程的新手。 I just want to know how to open a modal window on button click.我只想知道如何在单击按钮时打开模态窗口。

public class Buttonwindow: MonoBehaviour 
{
    public void clicked(Button button)
    {
        Debug.Log("Button click!");

    }
}

What code will be going in here to show a pop up window?什么代码会在这里显示一个弹出窗口? Thanks!谢谢!

Use yourModelWindowPanel.SetActive(true) to enable/show your Window and pass in false to the SetActive function to hide it.使用yourModelWindowPanel.SetActive(true)启用/显示您的窗口并将false传递给SetActive函数以隐藏它。 This could be a panel with UI components under it.这可能是一个带有 UI 组件的面板。

public class Buttonwindow: MonoBehaviour 
{
    public GameObject modalWindow;
    public void clicked(Button button)
    {
        Debug.Log("Button click!");
        modalWindow.SetActive(true);
    }
}

In the Scene, right click -> UI -> Canvas to create a canvas(All UI elements should be in a Canvas) Then right click in the canvas that your created and then UI-> the element that you want(Text maybe works for your purpouse)在场景中,右键单击 -> UI -> 画布以创建画布(所有 UI 元素都应在画布中)然后右键单击您创建的画布,然后 UI-> 您想要的元素(文本可能适用于你的目的)

Then as @Programmer said然后正如@Programmer 所说

public class Buttonwindow: MonoBehaviour 
{
    public GameObject modalWindow;
    public void clicked(Button button)
    {
    Debug.Log("Button click!");
    modalWindow.SetActive(true);
    }
}

Don't forget to set the modalWindow to your object in the Inspector不要忘记在检查器中将 modalWindow 设置为您的对象

I can't comment due to my reputation, but this answer is only an improvement to the real answer by @Programmer.由于我的声誉,我无法发表评论,但这个答案只是对@Programmer 真实答案的改进。

Here is a tutorial for Unity UI.是 Unity UI 的教程。

Neither of these "answers" actually answer the question.这些“答案”实际上都没有回答这个问题。 A modal window is a window that maintains focus and nothing else can be done until the user clicks a button to satisfy a condition such as yes/no.模态窗口是一个保持焦点的窗口,在用户单击按钮以满足诸如是/否之类的条件之前,不能执行任何其他操作。 That means that while a modal window is active, nothing else can have focus.这意味着当模态窗口处于活动状态时,其他任何东西都无法获得焦点。 A modal window cannot be navigated away from.模态窗口无法导航离开。

To make a modal window, any buttons within the panel must be set to explicit navigation and set so that they can only navigate to other buttons within the panel.要制作模态窗口,面板内的任何按钮都必须设置为显式导航并设置为只能导航到面板内的其他按钮。

If there is only one button within the panel, set the navigation to none.如果面板中只有一个按钮,请将导航设置为无。

To answer @Ba'al consern you can use this script to lock all non child interactables to get that modal effect with keyboard/controller navigation.要回答@Ba'al consern,您可以使用此脚本锁定所有非子交互项,以通过键盘/控制器导航获得模态效果。

using System.Collections.Generic;
using UnityEngine.UI;
using System;
using System.Linq;

namespace Legend
{
    public class ModalLocker : MonoBehaviour
    {
        Selectable[] selectables;

        void OnEnable()
        {
            selectables = FindObjectsOfType<Selectable>().Where(s => s.interactable && !s.transform.IsChildOf(transform)).ToArray();
            foreach (var selectable in selectables)
            {
                selectable.interactable = false;
                //Debug.Log($"{selectable} disabled");
            }
        }

        void OnDisable()
        {
            if (selectables == null)
                return;

            foreach (var selectable in selectables)
                selectable.interactable = true;
            selectables = null;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM