美文网首页
Unity简易的红点系统RedPoint System

Unity简易的红点系统RedPoint System

作者: UnityChan | 来源:发表于2020-01-17 09:33 被阅读0次

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

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<string, RedPointNode> dicChilds = new Dictionary<string, RedPointNode>();

    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<string> litRedPointTreeList = new List<string>()
    {
        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);
    }



}

相关文章

网友评论

      本文标题:Unity简易的红点系统RedPoint System

      本文链接:https://www.haomeiwen.com/subject/kkcczctx.html