【问题标题】:Quit Unity3D Android app on 2 back button press按 2 次后退按钮退出 Unity3D Android 应用程序
【发布时间】:2021-06-14 16:03:51
【问题描述】:

我希望我的应用程序在第一次按下后退按钮时显示消息“请再次触摸后退按钮以退出应用程序”,并且当再次按下时应用程序应该退出。我想我已经添加了适当的代码,但它不起作用。

脚本作为组件附加到画布元素。 该脚本包含我分配给面板(画布的子项)UI 元素的公共变量。

Scene hierarchy

观察到: 当我按下后退按钮时,文本会出现,但只有几分之一秒,然后突然消失,下一次按下后退按钮并没有导致应用退出。

希望 在第一次按下后退按钮时,它应该显示消息,如果按下第二个后退按钮,应用程序应该在 3 秒内退出。

相关信息: 统一2017.1.0f3

这里是代码链接:

https://gist.github.com/bmohanrajbit27/431221fc80e0b247649289fd136f9cfb

public class ChangeSceneScript : MonoBehaviour
{
    private bool iQuit = false;
    public GameObject quitobject;

    void Update()
    {
        if (iQuit == true)
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                Application.Quit();
            }
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            quitobject.SetActive(true);
            iQuit = true;
            StartCoroutine(QuitingTimer());

        }
    }

    IEnumerator QuitingTimer()
    {
        yield return new WaitForSeconds(3);
        iQuit = false;
        quitobject.SetActive(false);
    }
}

【问题讨论】:

  • 看来你快完成了,试试这个;当你第一次按下返回按钮时,从 3 开始倒计时。当这个值大于 0 时,你可以在 if 条件下检查它,如果按下返回按钮则退出你的应用程序。
  • 我没有使用协程
  • @Mohan 你可以尝试用 else if (Input.GetKeyDown (KeyCode.Escape) 替换第 12 行 if (Input.GetKeyDown (KeyCode.Escape)) )
  • 奇怪的代码应该可以工作

标签: c# unity3d


【解决方案1】:

我见过很少有instances Application.Quit(); 在 Android 上不起作用。发生这种情况时,请使用System.Diagnostics.Process.GetCurrentProcess().Kill(); 退出程序。

现在,对于您的计时器问题,当第一次按下输入时,在 Update 函数中启动协程。使用一个标志来确保这个协程函数在最后一个完成之前不会再次启动。布尔变量很好。

在里面,那个协程函数,不要使用yield return new WaitForSeconds(3);来等待定时器。使用while 循环和yield return null; 的组合来等待计时器完成。用Time.deltaTime 每帧增加计时器。现在,您可以轻松地检查该协程函数中的第二次按下,如果按下则退出。

如果您还希望它在编辑器中工作,您必须使用UnityEditor.EditorApplication.isPlaying = false; 退出。下面的示例也应该在编辑器中工作。有问题请看代码中的cmets。

public GameObject quitobject;
private bool clickedBefore = false;

void Update()
{
    //Check input for the first time
    if (Input.GetKeyDown(KeyCode.Escape) && !clickedBefore)
    {
        Debug.Log("Back Button pressed for the first time");
        //Set to false so that this input is not checked again. It will be checked in the coroutine function instead
        clickedBefore = true;

        //Activate Quit Object
        quitobject.SetActive(true);

        //Start quit timer
        StartCoroutine(quitingTimer());
    }
}

IEnumerator quitingTimer()
{
    //Wait for a frame so that Input.GetKeyDown is no longer true
    yield return null;

    //3 seconds timer
    const float timerTime = 3f;
    float counter = 0;

    while (counter < timerTime)
    {
        //Increment counter while it is < timer time(3)
        counter += Time.deltaTime;

        //Check if Input is pressed again while timer is running then quit/exit if is
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("Back Button pressed for the second time. EXITING.....");
            Quit();
        }

        //Wait for a frame so that Unity does not freeze
        yield return null;
    }

    Debug.Log("Timer finished...Back Button was NOT pressed for the second time within: '" + timerTime + "' seconds");

    //Timer has finished and NO QUIT(NO second press) so deactivate
    quitobject.SetActive(false);
    //Reset clickedBefore so that Input can be checked again in the Update function
    clickedBefore = false;
}

void Quit()
{
    #if UNITY_EDITOR
    UnityEditor.EditorApplication.isPlaying = false;
    #else
    //Application.Quit();
    System.Diagnostics.Process.GetCurrentProcess().Kill();
    #endif
}

【讨论】: