赞
踩
游戏中需要开始也需要结束,有退出按钮最好,没有退出按钮或者用户想直接退出怎么办呢?最简单的方法就是按下返回键退出,但是有时候用户会误操作,不小心按下了返回键不能直接退出吧,于是就要加个判断,双击退出,话不多说直接上代码:
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
-
- public class ExitGame : MonoBehaviour {
- public Text Show;
- float fadingSpeed = 1;
- bool fading;
- float startFadingTimep;
- Color originalColor;
- Color transparentColor;
-
- void Start () {
- originalColor = Show.color;
- transparentColor = originalColor;
- transparentColor.a = 0;
- Show.text = "再次按下返回键退出游戏";
- Show.color = transparentColor;
- }
-
- void Update () {
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- if (startFadingTimep==0)
- {
- Show.color = originalColor;
- startFadingTimep = Time.time;
- fading = true;
- }
- else
- {
- Application.Quit();
- }
- }
- if (fading)
- {
- Show.color = Color.Lerp(originalColor, transparentColor, (Time.time - startFadingTimep) * fadingSpeed);
- if (Show.color.a<2.0/255)
- {
- Show.color = transparentColor;
- startFadingTimep = 0;
- fading = false;
- }
- }
- }
- }
此代码带提示,当你按下返回键时给用户一个提示:再次按下返回键退出游戏 此时颜色渐变直至消失,整体效果还是不错的,快来试试吧GoodLuck!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。