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,这样就将物体设置为屏幕之外靠近右上角的位置。
下面给物体挂载脚本,实现通知功能。
然后给游戏物体加上Audio Source组件,由于播放消息声音。接下来将物体命名为Notification,做成Prefab。最终结果如下图所示:
下面写一个Notification脚本,用于统一管理通知。
这样,当我们使用的时候直接调用NotificationCenter.Instance. Add(TestTest.NotificationStyle notificationStyle, string guiTextContent)
方法就行了。其中notificationStyle参数表示消息现实的风格(WinStore样式或Android Toast样式),guiTextContent表示GUIText组件现实的文本,也就是消息内容。
接着我的上一篇文章,要想显示截图保存成功的消息,只需要这样调用NotificationCenter.Instance. Add(TestTest.NotificationStyle.WinStoreNotificationCenter, "成功截图并保存")。
接着设置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, "成功截图并保存")。
相关文章推荐
- 如何实现类似Windows Store 应用程序和Android Toast的通知?
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android开源代码解读-基于SackOfViewAdapter类实现类似状态通知栏的布局
- 举例说明如何用IKAnalyzer实现在android应用程序上面的中文分词
- 深入学习:如何实现不同Android设备之间相同应用程序的网络服务发现功能
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android如何实现类似Gmail的标题栏返回
- 如何用android实现类似windows的画板功能
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- 第三部分:Android 应用程序接口指南---第二节:UI---第八章 Toast通知
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- android中如何隐藏应用程序标题栏和通知栏
- IOS 实现 类似 Android的 Toast
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- Android中如何使用ViewPager实现类似laucher左右拖动效果 3ff8
- Android中如何使用ViewPager实现类似laucher左右拖动效果
- 深入学习:如何实现不同Android设备之间相同应用程序的网络服务发现功能
- Android开发教程——应用程序之间如何实现沟通
- 如何在iOS上实现Android风格的提示信息Toast