Unity3D教程:安卓游戏退出确认的方法
发表于2017-02-21
有些安卓游戏在你退出游戏时会弹出一个需要你确认退出的界面,那么这个确认退出的功能是怎么实现的,下面就给大家介绍下这个退出确认脚本的实现方法,一起来看看吧。
可以设定一个int值比如escapeTimes初始值设为1;当检测到“Input.GetKey(KeyCode.Escape)”后escapeTimes++;然后
1 2 3 4 | if (Input.GetKey(KeyCode.Escape) && escapeTimes > 1) { Application.Quit(); } |
最后通过协同函数检测如果按两次返回键时间间隔过长,则重置。具体代码(C#)如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | int escapeTimes = 1; void Update() { if (Input.GetKey(KeyCode.Escape)) { //这个地方可以写“再按一次退出”的提示 escapeTimes++; StartCoroutine( "resetTimes" ); if (escapeTimes > 1) { Application.Quit(); } } } IEnumerator resetTimes() { yield return new WaitForSeconds(1); escapeTimes =1; } |
或者使用GUI确认菜单吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | var isQuit; function Start(){ isQuit= false ; } function Update () { if (Input.GetKey(KeyCode.Escape)) { isQuit= true ; } } function OnGUI() { if (isQuit) { GUI.Box(Rect(0,0,200,100), "确定要退出吗?" ) if (GUI.Button(Rect(20,50,70,30), "是" )) { Application.Quit(); } if (GUI.Button(Rect(110,50,70,30), "否" )) { isQuit= false ; } } } |