当前位置:网站首页>Unity universal red dot system
Unity universal red dot system
2022-07-28 01:58:00 【lcl20093466】
The red dot system is generally implemented by multi tree .
Trees :
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class SGTreeNode
{
[SerializeField]
System.Object mData = null;
[SerializeField]
SGTreeNode mParent = null;
[SerializeField]
List<SGTreeNode> mChildren = null;
[SerializeField]
bool mIsLeaf = true;
public bool IsLeaf {
get {
return mIsLeaf; } }
public SGTreeNode Parent {
get {
return mParent; } }
public List<SGTreeNode> Children {
get {
return mChildren; } }
public SGTreeNode(System.Object data)
{
mChildren = new List<SGTreeNode>();
mData = data;
}
public System.Object GetData()
{
return mData;
}
public SGTreeNode SearchNode(Predicate<System.Object> compare)
{
if (compare.Invoke(mData))
{
return this;
}
else
{
SGTreeNode searchedNode = null;
foreach (var item in mChildren)
{
if (compare.Invoke(item.mData))
{
searchedNode = item;
}
else
{
searchedNode = item.SearchNode(compare);
}
if (searchedNode != null)
{
break;
}
}
return searchedNode;
}
}
public SGTreeNode AddChild(SGTreeNode child)
{
this.mIsLeaf = false;
child.mParent = this;
mChildren.Add(child);
return child;
}
}
Red dot Manager :
See initialization for the following tree structure of the red dot system .
using SG.Utils;
using System;
using System.Collections.Generic;
using UnityEngine;
public enum RedPointType
{
Null,
Root,// Red dot system root node
Task,// Task node
SignIn,// Sign in
DailyTask, // Daily tasks
DailyTaskItem, // Daily specific items
WeeklyTask,// Weekly task
WeeklyTaskItem,// Specific tasks of each week
AchievementTask,// Achievement task
AchievementTaskItem,// Achieve specific tasks
}
public enum RedPointEvent
{
RefreshRedPoint,
}
public class RedPointData
{
public RedPointType mType = RedPointType.Null;
public bool mIsShow = false;
public Dictionary<string, bool> mExtDatas = new Dictionary<string, bool>();
public int mRedCount = 0;
public RedPointData(RedPointType type)
{
mType = type;
mIsShow = false;
}
}
public class RedPointManager2 : MonoSingleton<RedPointManager2>
{
[SerializeField]
SGTreeNode mRootNode = null;
/// <summary>
/// Initialize the red dot system
/// </summary>
public new void Init()
{
mRootNode = new SGTreeNode(new RedPointData(RedPointType.Root));
var taskNode = mRootNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.Task)));
var signNode = taskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.SignIn)));
var dailyTaskNode = taskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.DailyTask)));
var weeklyTaskNode = taskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.WeeklyTask)));
var achievementTaskNode = taskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.AchievementTask)));
dailyTaskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.DailyTaskItem)));
weeklyTaskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.WeeklyTaskItem)));
achievementTaskNode.AddChild(new SGTreeNode(new RedPointData(RedPointType.AchievementTaskItem)));
}
/// <summary>
/// Refresh whether the red dot is displayed according to the red dot type . notes : Only the leaf node small red dot data can be refreshed
/// </summary>
/// <param name="redPointType"></param>
/// <param name="isShow"></param>
public void UpdateRedPoint(RedPointType redPointType, bool isShow)
{
var node = SearchNode(redPointType);
if (node != null)
{
if (node.IsLeaf)
{
var data = node.GetData() as RedPointData;
data.mIsShow = isShow;
data.mRedCount = 1;
UpdateParent(node.Parent);
SendRefreshRedPointEvent(node);
}
}
}
/// <summary>
/// Refresh whether the red dot is displayed according to the red dot type . notes : Only the leaf node small red dot data can be refreshed
/// </summary>
/// <param name="redPointType"></param>
/// <param name="extData"> The current red dot type contains multiple red dot data </param>
public void UpdateRedPoint(RedPointType redPointType, Dictionary<string, bool> extData)
{
var node = SearchNode(redPointType);
if (node != null)
{
if (node.IsLeaf)
{
var data = node.GetData() as RedPointData;
var isShow = false;
var showCount = 0;
foreach (var item in extData)
{
isShow = item.Value || isShow;
if (item.Value)
{
showCount++;
}
data.mExtDatas.Remove(item.Key);
data.mExtDatas[item.Key] = item.Value;
}
data.mIsShow = isShow;
data.mRedCount = showCount;
UpdateParent(node.Parent);
SendRefreshRedPointEvent(node);
}
}
}
/// <summary>
/// Get red dot data information
/// </summary>
/// <param name="redPointType"></param>
/// <returns></returns>
public RedPointData GetRedPointData(RedPointType redPointType)
{
var node = SearchNode(redPointType);
if (node != null)
{
return node.GetData() as RedPointData;
}
else
{
return null;
}
}
SGTreeNode SearchNode(RedPointType redPointType)
{
var node = mRootNode.SearchNode(new Predicate<object>(delegate (object nodeData) {
RedPointData data = nodeData as RedPointData;
if (data != null)
{
return data.mType == redPointType;
}
else
{
return false;
}
}));
return node;
}
RedPointData GetNodeData(SGTreeNode node)
{
return node.GetData() as RedPointData;
}
void UpdateParent(SGTreeNode node)
{
if (node != null && !node.IsLeaf)
{
if (node.Children.Count > 0)
{
int redCount = 0;
bool isShow = false;
foreach (var item in node.Children)
{
var dataTemp = GetNodeData(item);
isShow = isShow || dataTemp.mIsShow;
if (GetNodeData(item).mIsShow)
{
redCount = redCount + dataTemp.mRedCount;
}
}
var data = node.GetData() as RedPointData;
data.mIsShow = isShow;
data.mRedCount = redCount;
}
UpdateParent(node.Parent);
}
}
void SendRefreshRedPointEvent(SGTreeNode node)
{
if (node != null)
{
GameEventMgr.Instance.SendEvent(RedPointEvent.RefreshRedPoint, new GameEventMgr.GameEventArgs()
{
data = node.GetData()
});
SendRefreshRedPointEvent(node.Parent);
}
}
}
Examples of use :
void TestRedPoint()
{
GameEventMgr.Instance.RemoveEvent(RedPointEvent.RefreshRedPoint, OnEventUpdateRedPoint);
GameEventMgr.Instance.AddEvent(RedPointEvent.RefreshRedPoint, OnEventUpdateRedPoint);
Debug.Log("=========UpdateRedPoint:SignIn");
RedPointManager2.Instance.UpdateRedPoint(RedPointType.SignIn, true);
Debug.Log("=========UpdateRedPoint:DailyTaskItem 1");
RedPointManager2.Instance.UpdateRedPoint(RedPointType.DailyTaskItem, true);
Debug.Log("=========UpdateRedPoint:DailyTaskItem 2");
Dictionary<string, bool> mData = new Dictionary<string, bool>();
mData["1000"] = true;
mData["1001"] = true;
mData["1002"] = false;
RedPointManager2.Instance.UpdateRedPoint(RedPointType.DailyTaskItem, mData);
}
void OnEventUpdateRedPoint(AEventArgs arg)
{
var temp = arg as GameEventMgr.GameEventArgs;
var data = temp.data as RedPointData;
Debug.Log("===========TestEvent:" + LitJson.JsonMapper.ToJson(data));
}
Output :
边栏推荐
猜你喜欢

【taichi】在太极中画出规整的网格

Tencent cloud hiflow scene connector

Establishment of elk log analysis system

Redis设计规范

In the era of great changes in material enterprises, SRM supplier procurement system helps enterprises build a digital benchmark for property procurement

Custom type: structure, enumeration, union

Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice

The petrochemical industry is facing the tide of rising prices, and the digital dealer distribution system platform enables dealers and stores

小散量化炒股记|量化系统中数据是源头,教你搭建一款普适的数据源框架

石油化工行业迎战涨价大潮,经销商分销系统平台数字化赋能经销商与门店
随机推荐
GBase 8c 事务ID和快照(四)
Lambda expressions and stream streams
Data security and privacy computing summit - provable security: Learning
ue4 unreal NDisplay插件 简易使用 三折幕 详细...
GBase 8c 通用文件访问函数
抓包精灵NetCapture APP抓包教程《齐全》
什么是方法,什么是方法论:了解自我精进提升的底层逻辑
Leetcode high frequency question 128. the longest continuous sequence, which is often tested in interviews with Internet companies
Linux安装mysql8.0.29详细教程
GBase 8c 恢复控制函数
Gbase 8C transaction ID and snapshot (I)
写给去不图床用户的一封信
JS what situations can't use json Parse, json.stringify deep copy and a better deep copy method
Flink 在 讯飞 AI 营销业务的实时数据分析实践
VLAN实验
How tormenting are weekly and monthly reports? Universal report template recommended collection! (template attached)
Zhi Huijun, Huawei's "genius youth", has made a new work, building a "customized" smart keyboard from scratch
Process data and change the name of data
Gbase 8C transaction ID and snapshot (V)
What devices does devicexplorer OPC server support? This article has listed