C#/Problems

안드로이드 디바이스에서 뒤로가기 2번 누르면 종료하기

dev_sr 2020. 8. 17. 11:02

인터넷에 찾아서 나온 코드들은 한번 누르면 입력이 여러번 돼서 아예 안됨

 

입력을 한번 받고 코루틴에서 한번 기다렸다가 bool 값을 바꿔서 연속되는 입력을 잠시 막아버리고

입력을 다시 받아야 제대로 작동됨

 

 

0.3초 안에 뒤로가기를 두번 연속해서 눌러야 어플을 종료하게 함

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class BackButtonManager : MonoBehaviour
{
    private bool isClick;
    private int count;
   
    void Awake()
    {
        DontDestroyOnLoad(this);
    }

    public void Init()
    {
        Debug.Log("ButtonManager::Init");
        this.isClick = false;
    }


    private void Update()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            if (Input.GetKey(KeyCode.Escape) && !this.isClick)
            {
               
                this.StartCoroutine(this.CrQuitTimer());
                this.isClick = true;

                this.count++;
                Debug.Log(this.count);

                if(this.count>=2)
                {
                    this.count = 0;
                    this.isClick = false;
                    Application.Quit();
                }
            }

        }

        else if (Application.platform == RuntimePlatform.IPhonePlayer)
        {

        }
    }

    IEnumerator CrQuitTimer()
    {
        yield return new WaitForSeconds(0.1f);
        this.isClick = false;

        yield return new WaitForSeconds(0.3f);
        this.isClick = false;
        this.count = 0;

    }


}