简体   繁体   English

按2按钮退出Unity3D Android应用程序

[英]Quit Unity3D Android app on 2 back button press

I would like my app to show the message on the first back button press as "Please touch back button again to quit the app" and when it is pressed again the app should quit. 我希望我的应用程序在第一个后退按钮上显示消息“再次请再次触摸按钮退出应用程序”,再次按下该应用程序应该退出。 I think I have added appropriate code but it doesn't work. 我想我添加了适当的代码,但它不起作用。

The script is attached as a component to the canvas element. 该脚本作为组件附加到canvas元素。 The script contains the public variable which I assigned the panel(Child of canvas) UI element. 该脚本包含我分配了面板(画布之子)UI元素的公共变量。

Scene hierarchy 场景层次

Observed: When I pressed the back button the text appears but only a fraction of a second and then disappear all of a sudden and the next back button press did not resulted in app quit. 观察:当我按下后退按钮时,文本出现但只有几分之一秒,然后突然消失,下一次按下后退按钮没有导致应用程序退出。

Desired On first back button press it should show the message and with in say 3 seconds if the second back button pressed the app should quit. 所需的第一个后退按钮按下它应该显示消息,如果第二个后退按钮按下应用程序应该退出3秒。

Relevant information: Unity 2017.1.0f3 相关信息: Unity 2017.1.0f3

Here is the Code link : 这是代码链接:

https://gist.github.com/bmohanrajbit27/431221fc80e0b247649289fd136f9cfb 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);
    }
}

I've seen few instances where Application.Quit(); 我见过几个实例 ,其中Application.Quit(); did not work on Android. 在Android上无效。 When this happens, use System.Diagnostics.Process.GetCurrentProcess().Kill(); 发生这种情况时,请使用System.Diagnostics.Process.GetCurrentProcess().Kill(); to exit out of the program. 退出程序。

Now, for your timer issue, start a coroutine in the Update function when input is pressed for the first time. 现在,对于计时器问题,首次按下输入时,在Update函数中启动协同程序。 Use a flag to make sure that this coroutine function is not started again until the last one has finished. 使用标志以确保在最后一个完成之前不再启动此协程功能。 A boolean variable is fine. 布尔变量很好。

Inside, that coroutine function, don't use yield return new WaitForSeconds(3); 在里面,那个协程功能, 不要使用yield return new WaitForSeconds(3); to wait for the timer. 等待计时器。 Use a while loop with the combination of yield return null; 使用带有yield return null;组合的while循环yield return null; to wait until the timer is done. 等到计时器完成。 Increment the timer with Time.deltaTime each frame. 每帧使用Time.deltaTime递增计时器。 Now, you can easily check for the second press in that coroutine function and exit if pressed. 现在,您可以轻松检查该协程功能中的第二次按下,如果按下则退出。

If you also want this to work in the Editor, you have to use UnityEditor.EditorApplication.isPlaying = false; 如果您还希望在编辑器中使用UnityEditor.EditorApplication.isPlaying = false; ,则必须使用UnityEditor.EditorApplication.isPlaying = false; to exit. 退出。 The example below should also work in the Editor. 下面的示例也应该在编辑器中工作。 See the comments in the code if you have any question. 如果您有任何疑问,请参阅代码中的注释。

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
}

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

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