前言 游戏开发中最常见的用到树形结构的功能就是红点系统和行为树。
我今天先写一下红点系统的开发。
需求分析 红点的作用就是给玩家提示,例如:玩家有未读邮则主界面邮件功能出现红点,玩家看到红点后点击邮件功能入口,进入邮件功能主界面后又看到邮件标签页显示红点于是又点击邮件标签进入邮件列表,在众多邮件中找到某一封显示红点的未读邮件。
整个提示流程是:主界面邮件入口→邮件界面邮件页签→邮件列表中的未读邮件。直观的看,就是从外到内逐层进行提示。
然而在实现红点功能的时候,需要在主界面,邮件界面,邮件列表界面分别写红点提示代码吗?
别说,我还真见过有人这么搞,一个功能的红点提示要到处写,可想而知他弄的功能要涉及多少代码,尤其是主界面,那代码已经不能看了,一个界面调用十几二十个模块的方法就为了判断红点显不显示。
所以优雅代码的诞生就是从偷懒开始,我不想为了一个红点提示在其他地方加入一句本不该出现的XXX.IsShowRedPoint(),我希望只要在邮件列表的刷新逻辑加上一句:RedPointMgr.ShowPoint(RedPointType.Mail,true)就完成上面整个提示流程。
功能实现 于是红点树应运而生,当调用SetState的时候首先根据主Key找到红点树的根节点,然后逐层向下遍历直到找到目标子节点,接着从该子节点的父节点逐层向上依次计算红点状态,最后派发红点事件。即:触发红点功能的节点的所有父节点全部进行一次红点提示即可。
PS:为什么要先遍历到目标子节点?显然,每个节点只有一个父节点,有众多子节点,无法得知当前节点在哪个节点下,只能先遍历一次找到当前节点,再从当前节点一层一层的访问其父节点进行红点的提示。
那么到这里就很清晰了,首先要有一个红点类,它是一个树形结构
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 using System;using System.Collections.Generic;using UnityEngine.UI;public enum RedPointType{ None, Enternal, Once, } public enum RedPointState{ None, Show, Hide, } public class RedPoint { public string key { get { return m_Key; } } public string subKey { get { return m_SubKey; } } public bool isRoot { get { return m_IsRoot; } } public RedPointType type { get { return m_Type; } } public RedPointState state { get { return m_State; } } public int data { get { return m_Data; } } public RedPoint parent { get { return m_Parent; } } public List<RedPoint> children { get { return m_Children; } } public RedPoint (string key, string subKey, bool isRoot, RedPointType type ) { m_Key = key; m_SubKey = subKey; m_IsRoot = isRoot; m_Type = type; m_State = RedPointState.Hide; m_Data = 0 ; m_Children = new List<RedPoint>(); } public void Init (Action<RedPointState, int > showEvent, Button btn ) { m_ShowEvent = showEvent; if (btn != null ) { m_Btn = btn; m_Btn.onClick.AddListener(OnClick); } m_ShowEvent?.Invoke(m_State, m_Data); } public void AddChild (RedPoint node, string parentKey ) { if (m_SubKey.Equals(parentKey)) { node.SetParent(this ); m_Children.Add(node); return ; } for (int i = 0 ; i < m_Children.Count; i++) { m_Children[i].AddChild(node, parentKey); } } public RedPoint GetChild (string subKey ) { if (m_SubKey.Equals(subKey)) { return this ; } if (m_Children == null ) { return null ; } for (int i = 0 ; i < m_Children.Count; i++) { RedPoint node = m_Children[i].GetChild(subKey); if (node != null ) { return node; } } return null ; } public void RemoveChild (string subKey ) { if (m_SubKey.Equals(subKey)) { m_Parent.children.Remove(this ); Dispose(); return ; } if (m_Children == null ) { return ; } for (int i = 0 ; i < m_Children.Count; i++) { m_Children[i].RemoveChild(subKey); } } public void SetParent (RedPoint parent ) { m_Parent = parent; } public void SetState (string subKey, RedPointState state, int data ) { RedPoint node = GetChild(subKey); if (node == null ) { return ; } node.SetTreeState(subKey, state, data); m_Data = 0 ; for (int i = 0 ; i < m_Children.Count; i++) { m_Data += m_Children[i].m_Data; } m_ShowEvent?.Invoke(m_State, m_Data); } private void SetTreeState (string subKey, RedPointState state, int data ) { m_State = state; if (m_SubKey.Equals(subKey)) { m_Data = data; } else { m_Data = 0 ; for (int i = 0 ; i < m_Children.Count; i++) { if (m_Children[i].state == RedPointState.Show) { m_State = RedPointState.Show; m_Data += m_Children[i].data; } } } if (m_Parent != null ) { m_Parent.SetTreeState(subKey, state, data); } m_ShowEvent?.Invoke(m_State, m_Data); } private void OnClick () { if (m_Type == RedPointType.Once) { HideChildren(); SetState(m_SubKey, RedPointState.Hide, m_Data); } } private void HideChildren () { m_State = RedPointState.Hide; for (int i = 0 ; i < m_Children.Count; i++) { m_Children[i].HideChildren(); } m_ShowEvent?.Invoke(m_State, m_Data); } public void Dispose () { for (int i = 0 ; i < m_Children.Count; i++) { m_Children[i].Dispose(); } m_Children.Clear(); m_Children = null ; if (m_Btn != null ) { m_Btn.onClick.RemoveListener(OnClick); } m_Btn = null ; m_Parent = null ; m_Key = null ; m_SubKey = null ; m_ShowEvent = null ; m_Type = RedPointType.None; m_State = RedPointState.None; } private string m_Key = string .Empty; private string m_SubKey = string .Empty; private bool m_IsRoot = false ; private int m_Data = 0 ; private RedPointType m_Type = RedPointType.None; private RedPointState m_State = RedPointState.None; private Action<RedPointState, int > m_ShowEvent = null ; private Button m_Btn; private RedPoint m_Parent = null ; private List<RedPoint> m_Children = null ; }
这里面key就是归属,也就是属于哪一个大功能,subKey是自己的关键字。
然后需要一个管理器来管理游戏中所有红点的根节点,需要的时候通过关键字找到红点的根节点,再向其中的某个节点插入子节点。
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 using System;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class RedPointMgr : IDisposable { public static RedPointMgr instance { get { if (s_Instance == null ) { s_Instance = new RedPointMgr(); } return s_Instance; } } public RedPointMgr () { m_ListRedPointTrees = new List<RedPoint>(); } public void Add (string key, string subKey, string parentKey, RedPointType type ) { RedPoint root = GetRoot(key); if (string .IsNullOrEmpty(subKey) || key.Equals(subKey)) { if (root != null ) { Debug.LogError("The red point root [" + key + "] is already exist!" ); return ; } root = new RedPoint(key, key, true , type); m_ListRedPointTrees.Add(root); } else { if (root == null ) { Debug.LogError("The red point root [" + key + "] is invalid,please add it first" ); return ; } RedPoint node = new RedPoint(key, subKey, false , type); root.AddChild(node, parentKey); } } public void Remove (string key, string subKey ) { if (string .IsNullOrEmpty(subKey) || key.Equals(subKey)) { for (int i = m_ListRedPointTrees.Count - 1 ; i >= 0 ; i--) { if (m_ListRedPointTrees[i].key.Equals(key)) { m_ListRedPointTrees[i].Dispose(); m_ListRedPointTrees.RemoveAt(i); return ; } } return ; } RedPoint root = GetRoot(key); if (root == null ) { return ; } root.RemoveChild(subKey); } public void Init (string key, string subKey, Action<RedPointState, int > showEvent, Button btn = null ) { RedPoint root = GetRoot(key); if (root == null ) { Debug.LogError("The red point root [" + key + "] is invalid,please add it first" ); return ; } RedPoint node = root.GetChild(subKey); if (node == null ) { Debug.LogError("The red point node [" + subKey + "] is invalid,please add it first" ); return ; } node.Init(showEvent, btn); } public void SetState (string key, string subKey, RedPointState state, int data = 0 ) { RedPoint root = GetRoot(key); if (root == null ) { Debug.LogError("The red point root [" + key + "] is invalid,please add it first" ); return ; } root.SetState(subKey, state, data); } private RedPoint GetRoot (string key ) { if (string .IsNullOrEmpty(key)) { return null ; } for (int i = 0 ; i < m_ListRedPointTrees.Count; i++) { if (m_ListRedPointTrees[i].key.Equals(key)) { return m_ListRedPointTrees[i]; } } return null ; } public void Dispose () { for (int i = m_ListRedPointTrees.Count - 1 ; i >= 0 ; i--) { m_ListRedPointTrees[i].Dispose(); } m_ListRedPointTrees.Clear(); ; } private static RedPointMgr s_Instance = null ; private List<RedPoint> m_ListRedPointTrees = null ; }
使用时,先在游戏初始化的时候调用Add方法声明有哪些红点构建红点树;然后在UI界面初始化时调用Init方法加入红点的显示和点击的回调;最后在功能逻辑处调用SetState方法。
实例测试 例如:现在有mail1、mail2、mail3、mail4、mail5、mail6需要红点提示,mail4、mail5、mail6是具体业务管理的且都是mail3的子节点,mial1、mail2、mail3要随着他们的子节点变化。
先构建,再初始化,最后写显示回调。(一顿操作猛如虎,一看工资2k5))
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 using UnityEngine;using UnityEngine.UI;public class Test : MonoBehaviour { public GameObject mail1RedPoint; public GameObject mail2RedPoint; public GameObject mail3RedPoint; public GameObject mail4RedPoint; public GameObject mail5RedPoint; public GameObject mail6RedPoint; public Text txtMail1; public Text txtMail2; public Text txtMail3; public Text txtMail4; public Text txtMail5; public Text txtMail6; public Button mail4Btn; public Button mail5Btn; public Button mail6Btn; public Button btnSet1; public Button btnSet2; public Button btnSet3; public int count1 = 5 ; public int count2 = 6 ; public int count3 = 7 ; string mail1 = "mail1" ; string mail2 = "mail2" ; string mail3 = "mail3" ; string mail4 = "mail4" ; string mail5 = "mail5" ; string mail6 = "mail6" ; private void Awake () { RedPointMgr.instance.Add(mail1, null , null , RedPointType.Enternal); RedPointMgr.instance.Add(mail1, mail2, mail1, RedPointType.Enternal); RedPointMgr.instance.Add(mail1, mail3, mail2, RedPointType.Enternal); RedPointMgr.instance.Add(mail1, mail4, mail3, RedPointType.Once); RedPointMgr.instance.Add(mail1, mail5, mail3, RedPointType.Once); RedPointMgr.instance.Add(mail1, mail6, mail3, RedPointType.Once); RedPointMgr.instance.Init(mail1, mail1, OnMail1Show); RedPointMgr.instance.Init(mail1, mail2, OnMail2Show); RedPointMgr.instance.Init(mail1, mail3, OnMail3Show); RedPointMgr.instance.Init(mail1, mail4, OnMail4Show, mail4Btn); RedPointMgr.instance.Init(mail1, mail5, OnMail5Show, mail5Btn); RedPointMgr.instance.Init(mail1, mail6, OnMail6Show, mail6Btn); btnSet1.onClick.AddListener(OnBtnSet1Click); btnSet2.onClick.AddListener(OnBtnSet2Click); btnSet3.onClick.AddListener(OnBtnSet3Click); } private void OnMail1Show (RedPointState state, int data ) { mail1RedPoint.SetActive(state == RedPointState.Show); txtMail1.text = data.ToString(); } private void OnMail2Show (RedPointState state, int data ) { mail2RedPoint.SetActive(state == RedPointState.Show); txtMail2.text = data.ToString(); } private void OnMail3Show (RedPointState state, int data ) { mail3RedPoint.SetActive(state == RedPointState.Show); txtMail3.text = data.ToString(); } private void OnMail4Show (RedPointState state, int data ) { mail4RedPoint.SetActive(state == RedPointState.Show); txtMail4.text = data.ToString(); } private void OnMail5Show (RedPointState state, int data ) { mail5RedPoint.SetActive(state == RedPointState.Show); txtMail5.text = data.ToString(); } private void OnMail6Show (RedPointState state, int data ) { mail6RedPoint.SetActive(state == RedPointState.Show); txtMail6.text = data.ToString(); } private void OnBtnSet1Click () { RedPointMgr.instance.SetState(mail1, mail4, count1 == 0 ? RedPointState.Hide : RedPointState.Show, count1); } private void OnBtnSet2Click () { RedPointMgr.instance.SetState(mail1, mail5, count2 == 0 ? RedPointState.Hide : RedPointState.Show, count2); } private void OnBtnSet3Click () { RedPointMgr.instance.SetState(mail1, mail6, count3 == 0 ? RedPointState.Hide : RedPointState.Show, count3); } }
RedPointMgr.instance.SetState方法的第三个参数是显示在红点上的数字,也是业务需要统计的数据,如果只显示红点不显示数字就不用传了。
运行Test脚本,效果如下
结语 以上是红点系统的简单实现,写这篇东西也是因为见到我的大神同事竟然手写每一个红点,这实在是令我惊叹不已。
我可不想写那么多的cv代码。