要实现通知中心功能,首先要创建一个游戏物体,在上面挂载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 = ;
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 = - (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 != && stopMoveTime + androidToastStayTime <= Time.time)
{
shouldFadeOut = true;
} if (shouldFadeOut)
{
Color temp = guiTexture.color;
temp.a -= Time.deltaTime / 2f;
guiTexture.color = temp;
if (guiTexture.color.a <= )
{
shouldFadeOut = false;
Destroy(this.gameObject);
}
} break;
case NotificationStyle.WinStoreNotificationCenter: if (sholdMove)
{
guiTexture.transform.Translate(new Vector3(-0.1f, , ) * Time.deltaTime * );
if (guiTexture.transform.position.x <= x)
{
sholdMove = false;
stopMoveTime = Time.time;
}
} if (stopMoveTime != && stopMoveTime + winStoreNotificationCenterStayTime <= Time.time)
{
shouldFadeOut = true;
} if (shouldFadeOut)
{
Color temp = guiTexture.color;
temp.a -= Time.deltaTime;
guiTexture.color = temp;
if (guiTexture.color.a <= )
{
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, "成功截图并保存")。

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

  1. Unity3D开发Windows Store应用程序 注意事项

    原地址:http://blog.csdn.net/jbjwpzyl3611421/article/details/12704491 针对最近在移植window store项目中遇到的问题,我整理了官方 ...

  2. PowerShell将Windows store应用程序安装为开发者模式

    原文: PowerShell将Windows store应用程序安装为开发者模式 在本地部署Windows 商店应用程序时,我们会遇到Add-AppDevPackage.ps1脚本,这个脚本和所在安装 ...

  3. Unity干中学——如何实现游戏截图?

    using UnityEngine; using System.Collections; using System.IO; public class ScreenShot : MonoBehaviou ...

  4. Unity Game Starter Kit for Windows Store and Windows Phone Store games

    原地址:http://digitalerr0r.wordpress.com/2013/09/30/unity-game-starter-kit-for-windows-store-and-window ...

  5. Unity for Windows: II – Publishing Unity games to Windows Store

    原地址:http://digitalerr0r.wordpress.com/2013/08/27/unity-for-windows-ii-publishing-to-windows-8/ Windo ...

  6. Windows Store App 过渡动画

    Windows Store App 过渡动画     在开发Windows应用商店应用程序时,如果希望界面元素进入或者离开屏幕时显得自然和流畅,可以为其添加过渡动画.过渡动画能够及时地提示用户屏幕所发 ...

  7. 在桌面程序上和Metro/Modern/Windows store app的交互(相互打开,配置读取)

    这个标题真是取得我都觉得蛋疼..微软改名狂魔搞得我都不知道要叫哪个好.. 这边记录一下自己的桌面程序跟windows store app交互的过程. 由于某些原因,微软的商店应用的安全沙箱导致很多事情 ...

  8. 用unity4.3发布WINDOWS STORE APP应用的方法

    http://www.cnblogs.com/suxsho/ 原创,转载请声明 ============================================================ ...

  9. kiosk-mode,免密码登陆, sideload Windows Store apps 等

    MVVM带来的性能问题及其解决方案  MVVM 和语言性能提示:https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/mt628050. ...

随机推荐

  1. Bash简单介绍

    Bash不不过一个命令解析程序. 它本身拥有一种程序设计语言,使用这样的语言,能够编写shell脚本来完毕各种各样的工作.而这些工作是使用现成的命令所无法完毕的.Bash脚本能够使用if-then-e ...

  2. Android 颜色渲染(一) 颜色选择器 ColorPickerDialog剖析

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色选择器之ColorPickerDialog剖析 有这样一个需求,可以让用户自定义背景颜色,这就需要提供一个颜色选择器给用户. ...

  3. android 14 进度条和拖动条

    进度条: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l ...

  4. linux块设备IO栈浅析

    http://www.sysnote.org/2015/08/06/linux-io-stack/

  5. linux device driver —— 字符设备

    现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...

  6. Android(java)学习笔记206:利用开源SmartImageView优化网易新闻RSS客户端

    1.我们自己编写的SmartImageView会有很多漏洞,但是我们幸运的可以在网上利用开源项目的,开源项目中有很多成熟的代码,比如SmartImageView都编写的很成熟的 国内我们经常用到htt ...

  7. codevs 3052 多米诺 二分图匹配

    /*codevs 3052 二分图匹配 把矩阵分两批 黑和白 且黑白不相交 这就构成了二分图的两部分 然后求最大匹配*/ #include<cstdio> #include<cstr ...

  8. 移动web设计稿尺寸,关于移动web尺寸的那点事

    我自己的做稿子的时候,一开始就有一个习惯,先放上这段代码<meta name="viewport" content="width=device-width, ini ...

  9. Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 中文帮助文档 分类: ASP.NET 2014-10-28 14:09 177人阅读 评论(1) 收藏

    下载地址1:https://securityswitch.googlecode.com/files/SecuritySwitch%20v4.2.0.0%20-%20Binary.zip 下载地址2:h ...

  10. oracle服务器端-登陆

    由于的的操作系统是windows server版本,所以想装服务器端的server版本,一般的oracle都有'scott'用户,但是貌似服务器端的没有该用户,我用以下方式登陆: sqlplus / ...