您的位置:首页 > 移动开发 > Unity3D

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

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

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

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

using UnityEngine;
using System.Collections;

public class TestTest : MonoBehaviour
{
private GUITexture guiTexture;
private GUIText guiText;
private float x;
private bool sholdMove = true;
private bool shouldFadeOut = true;
public float stopMoveTime = 0;
public bool shouldPlay = true;
private float winStoreNotificationCenterStayTime = 3f;
private float androidToastStayTime = 0.5f;
public enum NotificationStyle { AndroidToast, WinStoreNotificationCenter };

public NotificationStyle currentNotificationStyle = NotificationStyle.WinStoreNotificationCenter;
void Start()
{
guiTexture = GetComponent<GUITexture>();
guiText = GetComponent<GUIText>();

audio.PlayDelayed(0.5f);

switch (currentNotificationStyle)
{
case NotificationStyle.AndroidToast:

Vector3 pos = guiTexture.gameObject.transform.position;
pos.x = 0.5f - (guiTexture.pixelInset.size.x / Screen.width) * 0.5f;
pos.y = 0.5f + (guiTexture.pixelInset.size.y / Screen.height) * 0.5f;
guiTexture.gameObject.transform.position = pos;
guiText.alignment = TextAlignment.Center;

shouldFadeOut = true;

break;

case NotificationStyle.WinStoreNotificationCenter:

x = 1 - (guiTexture.pixelInset.size.x / Screen.width) - 0.02f;
shouldFadeOut = false;
break;
default:

break;
}

}

// Update is called once per frame
void Update()
{
switch (currentNotificationStyle)
{
case NotificationStyle.AndroidToast:

if (stopMoveTime != 0 && stopMoveTime + androidToastStayTime <= Time.time)
{
shouldFadeOut = true;
}

if (shouldFadeOut)
{
Color temp = guiTexture.color;
temp.a -= Time.deltaTime / 2f;
guiTexture.color = temp;
if (guiTexture.color.a <= 0)
{
shouldFadeOut = false;
Destroy(this.gameObject);
}
}

break;
case NotificationStyle.WinStoreNotificationCenter:

if (sholdMove)
{
guiTexture.transform.Translate(new Vector3(-0.1f, 0, 0) * Time.deltaTime * 2);
if (guiTexture.transform.position.x <= x)
{
sholdMove = false;
stopMoveTime = Time.time;
}
}

if (stopMoveTime != 0 && stopMoveTime + winStoreNotificationCenterStayTime <= Time.time)
{
shouldFadeOut = true;
}

if (shouldFadeOut)
{
Color temp = guiTexture.color;
temp.a -= Time.deltaTime;
guiTexture.color = temp;
if (guiTexture.color.a <= 0)
{
shouldFadeOut = false;
Destroy(this.gameObject);
}
}

break;

default:
break;
}
}
}


然后给游戏物体加上Audio Source组件,由于播放消息声音。接下来将物体命名为Notification,做成Prefab。最终结果如下图所示:



下面写一个Notification脚本,用于统一管理通知。

using UnityEngine;
using System.Collections;

public class NotificationCenter : MonoBehaviour
{
public static NotificationCenter Instance;

public GameObject notificationPrefab;
void Awake()
{
Instance = this;
}

public void Add(TestTest.NotificationStyle notificationStyle, string guiTextContent)
{
GameObject go = Instantiate(notificationPrefab) as GameObject;
go.GetComponent<TestTest>().currentNotificationStyle = notificationStyle;
go.guiText.text = guiTextContent;
}
}


这样,当我们使用的时候直接调用NotificationCenter.Instance. Add(TestTest.NotificationStyle notificationStyle, string guiTextContent)

方法就行了。其中notificationStyle参数表示消息现实的风格(WinStore样式或Android Toast样式),guiTextContent表示GUIText组件现实的文本,也就是消息内容。

接着我的上一篇文章,要想显示截图保存成功的消息,只需要这样调用NotificationCenter.Instance. Add(TestTest.NotificationStyle.WinStoreNotificationCenter, "成功截图并保存")。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: