Unity干中学——如何实现类似Windows Store 应用程序和Android Toast的通知?

分享到:

      要实现通知中心功能,首先要创建一个游戏物体,在上面挂载GUITeture和GUIText脚本。注意GUITexture和GUIText脚本的顺序,GUITexture在前,GUIText在后,否则GUITexture会将GUIText遮挡住。

     接着设置Position属性,讲Position的X属性设置为1.2,Y设置为0.9,这样就将物体设置为屏幕之外靠近右上角的位置。

     下面给物体挂载脚本,实现通知功能。

      

  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 public class TestTest : MonoBehaviour
  5 {
  6     private GUITexture guiTexture;
  7     private GUIText guiText;
  8     private float x;
  9     private bool sholdMove = true;
 10     private bool shouldFadeOut = true;
 11     public float stopMoveTime = 0;
 12     public bool shouldPlay = true;
 13     private float winStoreNotificationCenterStayTime = 3f;
 14     private float androidToastStayTime = 0.5f;
 15     public enum NotificationStyle { AndroidToast, WinStoreNotificationCenter };
 16 
 17     public NotificationStyle currentNotificationStyle = NotificationStyle.WinStoreNotificationCenter;
 18     void Start()
 19     {
 20         guiTexture = GetComponent<GUITexture>();
 21         guiText = GetComponent<GUIText>();
 22 
 23         audio.PlayDelayed(0.5f);
 24 
 25         switch (currentNotificationStyle)
 26         {
 27             case NotificationStyle.AndroidToast:
 28 
 29                 Vector3 pos = guiTexture.gameObject.transform.position;
 30                 pos.x = 0.5f - (guiTexture.pixelInset.size.x / Screen.width) * 0.5f;
 31                 pos.y = 0.5f + (guiTexture.pixelInset.size.y / Screen.height) * 0.5f;
 32                 guiTexture.gameObject.transform.position = pos;
 33                 guiText.alignment = TextAlignment.Center;
 34 
 35                 shouldFadeOut = true;
 36 
 37                 break;
 38 
 39             case NotificationStyle.WinStoreNotificationCenter:
 40 
 41                 x = 1 - (guiTexture.pixelInset.size.x / Screen.width) - 0.02f;
 42                 shouldFadeOut = false;
 43                 break;
 44             default:
 45 
 46                 break;
 47         }
 48 
 49     }
 50 
 51     // Update is called once per frame
 52     void Update()
 53     {
 54         switch (currentNotificationStyle)
 55         {
 56             case NotificationStyle.AndroidToast:
 57 
 58                 if (stopMoveTime != 0 && stopMoveTime + androidToastStayTime <= Time.time)
 59                 {
 60                     shouldFadeOut = true;
 61                 }
 62 
 63                 if (shouldFadeOut)
 64                 {
 65                     Color temp = guiTexture.color;
 66                     temp.a -= Time.deltaTime / 2f;
 67                     guiTexture.color = temp;
 68                     if (guiTexture.color.a <= 0)
 69                     {
 70                         shouldFadeOut = false;
 71                         Destroy(this.gameObject);
 72                     }
 73                 }
 74 
 75                 break;
 76             case NotificationStyle.WinStoreNotificationCenter:
 77 
 78                 if (sholdMove)
 79                 {
 80                     guiTexture.transform.Translate(new Vector3(-0.1f, 0, 0) * Time.deltaTime * 2);
 81                     if (guiTexture.transform.position.x <= x)
 82                     {
 83                         sholdMove = false;
 84                         stopMoveTime = Time.time;
 85                     }
 86                 }
 87 
 88                 if (stopMoveTime != 0 && stopMoveTime + winStoreNotificationCenterStayTime <= Time.time)
 89                 {
 90                     shouldFadeOut = true;
 91                 }
 92 
 93                 if (shouldFadeOut)
 94                 {
 95                     Color temp = guiTexture.color;
 96                     temp.a -= Time.deltaTime;
 97                     guiTexture.color = temp;
 98                     if (guiTexture.color.a <= 0)
 99                     {
100                         shouldFadeOut = false;
101                         Destroy(this.gameObject);
102                     }
103                 }
104 
105                 break;
106 
107             default:
108                 break;
109         }
110     }
111 }
昵    称:
验证码:

相关文档: