红点系统--Unity功能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 定义树结构常量,后续做键
/// </summary>
public class RedPointConst
{
    public const string main = "Main";
    public const string mail = "Main.Mail";
    public const string mailSystem = "Main.Mail.System";
    public const string mainTeam = "Main.Mail.Team";
    public const string mainAlliance = "Main.Mail.Alliance";
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RedPointNode
{
    //节点名称
    public string nodeName;
    //总的红点数量
    public int pointNum = 0;
    //父节点
    public RedPointNode parent = null;
    //发生变化的回调函数
    public RedPointSystems.OnPointNumChange numChangeFunc;
    //子节点 
    public Dictionary<string, RedPointNode> dicChilds = new Dictionary<string, RedPointNode>();

    /// <summary>
    /// 设置当前节点的红点数量
    /// </summary>
    /// <param name="rpNum"></param>
    public void SetRedPointNum(int rpNum)
    {
        //红点数量只能设置叶子节点
        if (dicChilds.Count > 0)
        {
            Debug.LogError("只能设置叶子节点");
            return;
        }
        pointNum = rpNum;
        NotifyPointNumChange();
        if (parent != null)
        {
            parent.ChangePredPointNum();
        }
    }
    /// <summary>
    /// 计算当前红点数量
    /// </summary>
    private void ChangePredPointNum()
    {
        int num = 0;
        foreach (var node in dicChilds.Values)
        {
            num += node.pointNum;
        }
        //红点有变化
        if (num != pointNum)
        {
            pointNum = num;
            NotifyPointNumChange();
        }
    }
    /// <summary>
    /// 通知红点数量变化
    /// </summary>
    private void NotifyPointNumChange()
    {
        numChangeFunc?.Invoke(this);
    }
}

using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using UnityEngine;
/// <summary>
/// 红点系统
/// </summary>
public class RedPointSystems
{
    //红点变化通知
    public delegate void OnPointNumChange(RedPointNode node);
    RedPointNode mRootNode;//红点树Root节点
    //初始化红点树
    static List<string> IsPointTreeList = new List<string>
    {
        RedPointConst.main,
        RedPointConst.mail,
        RedPointConst.mailSystem,
        RedPointConst.mainTeam,
        RedPointConst.mainAlliance,
    };
    //开始进行初始化
    public void InitRedPointTreeNode()
    {
        mRootNode = new RedPointNode();
        mRootNode.nodeName = RedPointConst.main;

        foreach (var s in IsPointTreeList)
        {
            var node = mRootNode;
            //把我们每一个节点的名字通过截取存成数组
            var treeNodeAy = s.Split('.');
            //判断数组的第[0]项是否是根结点,不是的话就会报错
            if (treeNodeAy[0] != mRootNode.nodeName)
            {
                Debug.Log("红点树根结点报错:" + treeNodeAy[0]);
                continue;
            }
            //数组存储每一个节点的名字[0]根结点[1]根的子节点  [2]是【1】的子节点以此类推
            if (treeNodeAy.Length > 1)
            {
                for (int i = 1; i < treeNodeAy.Length; i++)
                {
                    if (!node.dicChilds.ContainsKey(treeNodeAy[i]))
                    {
                        //添加节点
                        node.dicChilds.Add(treeNodeAy[i], new RedPointNode());
                    }
                    //节点名字  节点的父节点是是上一级节点
                    node.dicChilds[treeNodeAy[i]].nodeName = treeNodeAy[i];
                    node.dicChilds[treeNodeAy[i]].parent = node;
                    //初始化红点树的最后一次重新赋值
                    node = node.dicChilds[treeNodeAy[i]];
                }
            }
        }
    }
    //现在树结构有了,那么我们要对树结构设置事件驱动,其实就是给这棵树绑定一个事件回调:
    //事件回调    参数(节点名字,节点数量改变的回调方法)
    public void SetRedPointNodeCallBack(string strNode, RedPointSystems.OnPointNumChange callBack)
    {
        var nodelist = strNode.Split('.');
        if (nodelist.Length == 1)
        {
            if (nodelist[0] != RedPointConst.main)
            {
                Debug.Log("当前是根节点" + nodelist[0]);
                return;
            }
        }

        var node = mRootNode;
        for (int i = 1; i < nodelist.Length; i++)
        {
            if (!node.dicChilds.ContainsKey(nodelist[i]))
            {
                Debug.Log("不包含这个孩子结点:" + nodelist[i]);
                return;
            }
            node = node.dicChilds[nodelist[i]];
            //最后一个节点了
            if (i == nodelist.Length - 1)
            {
                node.numChangeFunc = callBack;
                return;
            }
        }
    }

    //驱动层
    public void SetInvoke(string strNode, int rpNum)
    {
        //分析树节点
        var nodeList = strNode.Split('.');
        if (nodeList.Length == 1)
        {
            if (nodeList[0] != RedPointConst.main)
            {
                Debug.Log("根结点发生错误!现在的节点是" + nodeList[0]);
                return;
            }
        }

        var node = mRootNode;
        for (int i = 1; i < nodeList.Length; i++)
        {
            if (!node.dicChilds.ContainsKey(nodeList[i]))
            {
                Debug.Log("不包含这个孩子结点:" + nodeList[i]);
                return;
            }
            node = node.dicChilds[nodeList[i]];
            //最后一个节点
            if (i == nodeList.Length - 1)
            {
                //设置节点的红点数量
                node.SetRedPointNum(rpNum);
            }
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BootStart : MonoBehaviour
{
    //每一个界面的照片  红点数量文本
    public Button btn_Mail;
    public Button btn_System;
    public Button btn_Team;

    public int mailNodeNum = 0;
    public int systemNodeNum = 0;
    public int teamNodeNum = 0;
    public int allianceNodeNum = 0;

    public Image imageMail;
    public Image imageMailSystem;
    public Image imageMailTeam;
    public Image imageMailAlliance;

    public Text textMail;
    public Text textMailSystem;
    public Text textMailTeam;
    public Text textMailAlliance;

    public Transform content;
    int mailType = 1;
    // Start is called before the first frame update
    void Start()
    {
     
        RedPointSystems rps = new RedPointSystems();
        //树结构的初始化
        rps.InitRedPointTreeNode();

        //注册事件的回调
        rps.SetRedPointNodeCallBack(RedPointConst.mail, MailCallBack);
        rps.SetRedPointNodeCallBack(RedPointConst.mailSystem, MailSystemCallBack);
        rps.SetRedPointNodeCallBack(RedPointConst.mainTeam, MailTeamCallBack);
        rps.SetRedPointNodeCallBack(RedPointConst.mainAlliance, MailAllianceCallBack);
        systemNodeNum = 5;
        teamNodeNum = 5;
        allianceNodeNum = 1;
        rps.SetInvoke(RedPointConst.mailSystem, systemNodeNum);
        rps.SetInvoke(RedPointConst.mainTeam, teamNodeNum);
        rps.SetInvoke(RedPointConst.mainAlliance, allianceNodeNum);
        mailNodeNum = systemNodeNum + teamNodeNum + allianceNodeNum;
    }

    private void MailAllianceCallBack(RedPointNode node)
    {
        if (node.pointNum == 1)
        {
            textMailAlliance.gameObject.SetActive(false);
        }
        else
        {
            textMailAlliance.text = node.pointNum.ToString();
        }
        imageMailAlliance.gameObject.SetActive(node.pointNum > 0);
        Debug.Log(node.nodeName + "rp Num changed num=" + node.pointNum);
    }

    private void MailTeamCallBack(RedPointNode node)
    {
        if (node.pointNum == 1)
        {
            textMailTeam.gameObject.SetActive(false);
        }
        else
        {
            textMailTeam.text = node.pointNum.ToString();
        }
        textMailTeam.text = node.pointNum.ToString();
        imageMailTeam.gameObject.SetActive(node.pointNum > 0);
        Debug.Log(node.nodeName + "rp Num changed num=" + node.pointNum);
    }

    private void MailSystemCallBack(RedPointNode node)
    {
        if (node.pointNum == 1)
        {
            textMailSystem.gameObject.SetActive(false);
        }
        else
        {
            textMailSystem.text = node.pointNum.ToString();
        }
        textMailSystem.text = node.pointNum.ToString();
        imageMailSystem.gameObject.SetActive(node.pointNum > 0);
        Debug.Log(node.nodeName + "rp Num changed num=" + node.pointNum);
    }

    private void MailCallBack(RedPointNode node)
    {
        if (node.pointNum == 1)
        {
            textMail.gameObject.SetActive(false);
        }
        else
        {
            textMail.text = node.pointNum.ToString();
        }
        textMail.text = node.pointNum.ToString();
        imageMail.gameObject.SetActive(node.pointNum > 0);
        Debug.Log(node.nodeName + "rp Num changed num=" + node.pointNum);
    }

    // Update is called once per frame
    void Update()
    {

    }
}

版权声明:本文为qq_45071375原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:Unity--树实现的红点系统_unity 红点系统-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值