多条提示型渐显渐隐文本消息框制作
发表于2017-06-07
游戏项目中,当玩家操作游戏界面时,比如点击领取奖励按钮时,领取成功时,常常会及时弹出一些讯息反馈给玩家,比如提示玩家奖励已经进入他的邮箱或者背包等。这样,玩家不至于会懵逼!
比较简单的方式,就是直接弹出一些文本消息给玩家,但文本消息弹出有一定的规则:首先并列显示;其次渐渐隐藏掉;最后是有最大条数限制,并且显示时间最长的放在最上面显示。接下来把实现脚本贴出如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /* * Author:LuShang * 关于游戏项目中常用的弱信息提示功能进行封装 * 最多同时弹出2条弱信息提示 */ public class WeakInfoManager : MonoBehaviour { ///
/// 单例模式 /// public static WeakInfoManager Instance; ///
/// 用于显示弱信息的Text组件集合 /// public List WeakTextList; ///
/// 弱信息的父级容器 /// public GameObject WeakInfosParent; ///
/// 每条弱信息显示的生命周期 /// private const float ShowTime = 2; ///
/// 特定弱信息的索引值 /// private int curIndex = 0; ///
/// 允许最多同时显示弱信息的条数 /// private const int MaxShowCount = 2; ///
/// 当前正在展示弱信息的条数 /// private int curShowCount = 0; ///
/// 弱信息当前的颜色 /// private Color curColor = new Color(); ///
/// 字典存储各条在显示的弱信息当前剩余的显示时间 /// private Dictionary< int , float = "" > WeakInfoLeftTimeDic = new Dictionary< int , float = "" >(); #region 弱信息管理器的初始化 void Awake() { if (Instance == null ) Instance = this ; } #endregion #region 供外界设置弱信息提示的接口 ///
/// 设定一条弱信息提示 /// /// 弱信息内容 public void SetWeakInfo( string msg) { Debug.Log( "设定一条弱信息提示:" + msg); WeakTextList[curIndex].text = msg; // 初始化新建弱信息的颜值 SetTmpColor(WeakTextList[curIndex].color, 1); WeakTextList[curIndex].gameObject.SetActive( true ); //初始化新建弱信息的显示时间 WeakInfoLeftTimeDic[curIndex] = ShowTime; ++curShowCount; //当需要新建一条弱信息时,此时弱信息总共显示的条数达到最大上限时,需要移除最早显示的那条弱信息 if (curShowCount > MaxShowCount) { //移除最早显示的那条弱信息 removeIndex = 0 int removeIndex = (curIndex - MaxShowCount + WeakTextList.Count) % WeakTextList.Count; //把这条弱信息移出父级容器之外,隐藏并且重置显示时间为0 WeakTextList[removeIndex].gameObject.transform.SetParent(WeakInfosParent.transform.parent); WeakTextList[removeIndex].gameObject.SetActive( false ); WeakInfoLeftTimeDic[removeIndex] = 0; --curShowCount; } //对当前显示的所有弱信息进行排序,把剩余显示时间最长的,摆在最上头显示 curIndex = (curIndex + 1) % WeakTextList.Count; for ( int i = 1; i <= curShowCount; ++i) { int key = ((i * -1) + curIndex + WeakTextList.Count) % WeakTextList.Count; WeakTextList[key].gameObject.transform.SetParent(WeakInfosParent.transform.parent); WeakTextList[key].gameObject.transform.SetParent(WeakInfosParent.transform); } } #endregion #region 实时刷新当前显示所有弱信息的透明度 void Update() { if (curShowCount > 0) { for ( int i = 0; i < WeakTextList.Count; ++i) { if (!WeakInfoLeftTimeDic.ContainsKey(i)) { continue ; } if (WeakInfoLeftTimeDic[i] > 0) { WeakInfoLeftTimeDic[i] -= Time.deltaTime; if (WeakInfoLeftTimeDic[i] <= 0) // 周期结束,把这条弱信息移出父级容器之外,隐藏并且重置显示时间为0 { WeakTextList[i].gameObject.transform.SetParent(WeakInfosParent.transform.parent); WeakTextList[i].gameObject.SetActive( false ); WeakInfoLeftTimeDic[i] = 0; --curShowCount; } else //根据弱信息剩余的显示时间,逐渐淡化文本内容 { SetTmpColor(WeakTextList[i].color, WeakInfoLeftTimeDic[i]); WeakTextList[i].color = curColor; } } } } } #endregion ///
/// 设定弱信息文本的颜色 /// /// /// private void SetTmpColor(Color tc, float alpha) { curColor.r = tc.r; curColor.g = tc.g; curColor.b = tc.b; curColor.a = alpha; } } |

接着在测试场景中,创建一个Button组件,并挂上测试脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /* * Author:LuShang * */ public class Test : MonoBehaviour { public Button btn; // Use this for initialization void Awake () { btn.onClick.AddListener(OnBtnClickEvent); } void OnDestroy() { btn.onClick.RemoveListener(OnBtnClickEvent); } ///
/// 按钮点击事件处理 /// private void OnBtnClickEvent() { WeakInfoManager.Instance.SetWeakInfo( "First Time!" ); } // Update is called once per frame void Update () { } } |

这样多条提示型渐显渐隐文本消息框制作就完成了,欢迎读者@交流哈,谢谢!