第一次点击Esc或者返回,显示提示文字"再次按下返回键退出游戏",在文字消失之前再次点击Esc或者返回,退出游戏.
此脚本挂在Text文字提示上:
1 using UnityEngine; 2 using System.Collections; 3 using UnityEngine.UI; 7 public class ExitGame : MonoBehaviour 8 { 9 [Tooltip("ExitMessage拖进来")] 10 Text ExitMessageObj; 11 float fadingSpeed = 1; 12 bool fading; 13 float startFadingTimep; 14 Color originalColor; 15 Color transparentColor; 16 string sss; 17 18 void Start() 19 { 20 ExitMessageObj = this.GetComponent<Text>(); 21 originalColor = ExitMessageObj.color; 22 transparentColor = originalColor; 23 transparentColor.a = 0; 24 ExitMessageObj.text = "再次按下返回键退出游戏"; 25 ExitMessageObj.color = transparentColor; 26 } 27 28 void Update() 29 { 30 if (Input.GetKeyDown(KeyCode.Escape)) 31 { 32 if (startFadingTimep == 0) 33 { 34 ExitMessageObj.color = originalColor; 35 startFadingTimep = Time.time; 36 fading = true; 37 } 38 else 39 { 44 Application.Quit();//退出游戏 45 } 46 } 47 if (fading) 48 { 49 ExitMessageObj.color = Color.Lerp(originalColor, transparentColor, (Time.time - startFadingTimep) * fadingSpeed); 50 if (ExitMessageObj.color.a < 2.0 / 255) 51 { 52 ExitMessageObj.color = transparentColor; 53 startFadingTimep = 0; 54 fading = false; 55 } 56 } 57 } 58 }