要实现通知中心功能,首先要创建一个游戏物体,在上面挂载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 = ;
  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 = - (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 != && 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 <= )
  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, , ) * Time.deltaTime * );
  81. if (guiTexture.transform.position.x <= x)
  82. {
  83. sholdMove = false;
  84. stopMoveTime = Time.time;
  85. }
  86. }
  87.  
  88. if (stopMoveTime != && 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 <= )
  99. {
  100. shouldFadeOut = false;
  101. Destroy(this.gameObject);
  102. }
  103. }
  104.  
  105. break;
  106.  
  107. default:
  108. break;
  109. }
  110. }
  111. }

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

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

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NotificationCenter : MonoBehaviour
  5. {
  6. public static NotificationCenter Instance;
  7.  
  8. public GameObject notificationPrefab;
  9. void Awake()
  10. {
  11. Instance = this;
  12. }
  13.  
  14. public void Add(TestTest.NotificationStyle notificationStyle, string guiTextContent)
  15. {
  16. GameObject go = Instantiate(notificationPrefab) as GameObject;
  17. go.GetComponent<TestTest>().currentNotificationStyle = notificationStyle;
  18. go.guiText.text = guiTextContent;
  19. }
  20. }

这样,当我们使用的时候直接调用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. [置顶] UITableViewCell

    UITableViewCellStyle: 四种Cell类型. UITableViewCellSeparatorStyle 分割线类型.(group三种,plain两种) UITableViewCel ...

  2. TOJ1693(Silver Cow Party)

    Silver Cow Party   Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte Total Submit: ...

  3. [PWA] 19. Cache the avatars

    Cache the avatars is little different from cache photos. We need to serve the page with our cache da ...

  4. android 34 ListView进阶

    public View getView(int position, View convertView, ViewGroup parent) {////convertView是一个缓存,每次返回一个la ...

  5. Qt 学习之路 :信号槽

    信号槽是 Qt 框架引以为豪的机制之一.熟练使用和理解信号槽,能够设计出解耦的非常漂亮的程序,有利于增强我们的技术设计能力. 所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被 ...

  6. linux共享windows资料

    linux 只有NTFS格式不能访问,其的都可以.1.用fdisk -l 查看分区表.2.然后用mount -t vfat /mnt/hda1 /dev/hda1 就可以了./mnt/hda1是一普通 ...

  7. 你的网站为什么会慢?——用YSlow为你的网站提速

    在前面的文章我翻译的文章中分别从内容.服务器.JavaScript和CSS.图片.Coockies和移动应用等几个方面总结了34条提高网站性能的黄金守则,但是这些守则中,有一些是不常用到的,若非有实力 ...

  8. (转)function($){}(window.jQuery) 是什么意思?

    function(){}(); (function(){})(); 这两个是self-invoking anonymous 自调匿名函数,用这类的方法,能强制使匿名函数成为表达式,把不合法变成合法. ...

  9. JAva Collections类方法详解

    http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...

  10. ssh配置事务

    http://blog.csdn.net/jianxin1009/article/details/9202907(不错)