游戏服务器红点系统,Unity简易的红点系统RedPoint System

由于是展示,主要就三个脚本

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class RedPointConst

{

public const string main = "Main";

public const string mail = "Main.Mail";

public const string mailSystem = "Main.Mail.System";

public const string mailTeam = "Main.Mail.Team";

public const string mailAlliance = "Main.Mail.Alliance";

public const string task = "Main.Task";

public const string alliance = "Main.Alliance";

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class RedPointNode

{

public string nodeName;

public int pointNum;

public RedPointNode parent = null;

public RedPointSystem.OnPointNumChange numChangeFuc;

public Dictionary dicChilds = new Dictionary();

public void SetRedPointNum(int num) // 只允许最底层的红点计算数量

{

if (dicChilds.Count>0)

{

Debug.LogError("Only can set leaf node");

return;

}

pointNum = num;

NodifyPointNumChange();

if (parent!=null)

{

parent.ChangeRedPointNum();

}

}

public void ChangeRedPointNum() // 计算红点数量是否有变化

{

int num = 0;

foreach (var node in dicChilds.Values)

{

num += node.pointNum;

}

if (pointNum != num) // 红点数发生变化

{

pointNum = num;

NodifyPointNumChange();

}

}

public void NodifyPointNumChange()

{

//Debug.Log("............");

numChangeFuc?.Invoke(this);

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class RedPointSystem

{

public delegate void OnPointNumChange(RedPointNode node);

RedPointNode mRootNode;

static List litRedPointTreeList = new List()

{

RedPointConst.main,

RedPointConst.mail,

RedPointConst.mailSystem,

RedPointConst.mailTeam,

RedPointConst.mailAlliance,

RedPointConst.alliance,

RedPointConst.task

};

public void InitRedPointTreeNode()

{

mRootNode = new RedPointNode();

mRootNode.nodeName = RedPointConst.main;

foreach (var item in litRedPointTreeList)

{

var node = mRootNode;

var treeNodeArray = item.Split('.');

if (treeNodeArray[0]!= mRootNode.nodeName)

{

Debug.Log("RedPointTree Root Node Error"+ treeNodeArray[0]);

continue;

}

if (treeNodeArray.Length>1)

{

for (int i = 1; i < treeNodeArray.Length; i++)

{

if (!node.dicChilds.ContainsKey(treeNodeArray[i]))

{

node.dicChilds.Add(treeNodeArray[i], new RedPointNode());

}

node.dicChilds[treeNodeArray[i]].nodeName = treeNodeArray[i];

node.dicChilds[treeNodeArray[i]].parent = node;

node = node.dicChilds[treeNodeArray[i]];

}

}

}

}

public void SetRedPointCallBack(string strNode,RedPointSystem.OnPointNumChange callBack)

{

var nodeArray = strNode.Split('.');

if (nodeArray.Length == 1)

{

if (nodeArray[0] != mRootNode.nodeName)

{

Debug.Log("RedPointTree Root Node Error" + nodeArray[0]);

return;

}

}

var node = mRootNode;

for (int i = 1; i < nodeArray.Length; i++)

{

if (!node.dicChilds.ContainsKey(nodeArray[i]))

{

Debug.Log("RedPointTree Root Node Error" + nodeArray[i]);

}

node = node.dicChilds[nodeArray[i]];

if (i== nodeArray.Length-1) // 最后一个节点

{

Debug.Log(node.nodeName);

node.numChangeFuc = callBack;

return;

}

}

}

public void SetInvoke(string strNode,int rpNum)

{

var nodeArray = strNode.Split('.');

if (nodeArray.Length == 1)

{

if (nodeArray[0] != mRootNode.nodeName)

{

Debug.Log("RedPointTree Root Node Error" + nodeArray[0]);

return;

}

}

var node = mRootNode;

for (int i = 1; i < nodeArray.Length; i++)

{

if (!node.dicChilds.ContainsKey(nodeArray[i]))

{

Debug.Log("RedPointTree Root Node Error" + nodeArray[i]);

}

node = node.dicChilds[nodeArray[i]];

if (i == nodeArray.Length - 1) // 最后一个节点

{

node.SetRedPointNum(rpNum);

return;

}

}

}

}

下面是简单的用法

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class BootStart : MonoBehaviour

{

public Image imgMail;

public Image imgMailSystem;

public Image imgMailTeam;

public Text txtMail;

public Text txtMailSystem;

public Text txtMailTeam;

RedPointSystem rps;

private void Start()

{

rps = new RedPointSystem();

rps.InitRedPointTreeNode();

rps.SetRedPointCallBack(RedPointConst.mail, MailCallBack);

rps.SetRedPointCallBack(RedPointConst.mailSystem, MailSystemCallBack);

rps.SetRedPointCallBack(RedPointConst.mailTeam, MailTeamCallBack);

// rps.SetInvoke(RedPointConst.mailSystem, 3);

// rps.SetInvoke(RedPointConst.mailTeam, 2);

}

private void Update()

{

if (Input.GetKeyDown(KeyCode.L))

{

rps.SetInvoke(RedPointConst.mailSystem, 0);

rps.SetInvoke(RedPointConst.mailTeam, 0);

}

else if (Input.GetKeyDown(KeyCode.K))

{

rps.SetInvoke(RedPointConst.mailSystem, 3);

rps.SetInvoke(RedPointConst.mailTeam, 2);

}

}

void MailCallBack(RedPointNode node)

{

txtMail.text = node.pointNum.ToString();

txtMail.gameObject.SetActive(node.pointNum > 0);

imgMail.gameObject.SetActive(node.pointNum> 0);

Debug.Log(node.nodeName + " rp Num changed! num = " + node.pointNum);

}

void MailSystemCallBack(RedPointNode node)

{

txtMailSystem.text = node.pointNum.ToString();

txtMailSystem.gameObject.SetActive(node.pointNum > 0);

imgMailSystem.gameObject.SetActive(node.pointNum > 0);

Debug.Log(node.nodeName + " rp Num changed! num = " + node.pointNum);

}

void MailTeamCallBack(RedPointNode node)

{

txtMailTeam.text = node.pointNum.ToString();

txtMailTeam.gameObject.SetActive(node.pointNum > 0);

imgMailTeam.gameObject.SetActive(node.pointNum > 0);

Debug.Log(node.nodeName + " rp Num changed! num = " + node.pointNum);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值