141 lines
3.5 KiB
C#
141 lines
3.5 KiB
C#
using cfg.RedPoint;
|
||
using Framework;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
using Debug = DebugUtil;
|
||
|
||
public partial class RedPointManager : Singlenton<RedPointManager>, IInitable
|
||
{
|
||
const string RED_POINT_NODE_TYPE_PREFIX = "RedPointNode";
|
||
public const int RED_POINT_NODE_ROOT_FLAG = 0;
|
||
|
||
private List<RedPointNode> _treeRoots = new();
|
||
private Dictionary<int, RedPointNode> _nodeDic = new();
|
||
|
||
public void Init()
|
||
{
|
||
RegisterEvent();
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
UnRegisterEvent();
|
||
DisposeAllNodes();
|
||
_treeRoots = null;
|
||
_nodeDic = null;
|
||
}
|
||
|
||
private void RegisterEvent()
|
||
{
|
||
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnConfigLoad);
|
||
}
|
||
|
||
private void UnRegisterEvent()
|
||
{
|
||
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnConfigLoad);
|
||
}
|
||
|
||
private void OnConfigLoad()
|
||
{
|
||
ConstructTreeFromConfig();
|
||
InitAllNodes();
|
||
}
|
||
|
||
private void ConstructTreeFromConfig()
|
||
{
|
||
ConstructAllNode();
|
||
ConstructNodeRelationship();
|
||
}
|
||
|
||
private void ConstructAllNode()
|
||
{
|
||
var config = TableManager.Instance.Tables.RedPointConfig.DataMap;
|
||
|
||
foreach (var data in config.Values)
|
||
{
|
||
var id = data.Id;
|
||
var node = GetANode(id, data.Type);
|
||
if (node != null)
|
||
{
|
||
_nodeDic.Add(id, node);
|
||
if (data.ParentID == RED_POINT_NODE_ROOT_FLAG)
|
||
{
|
||
_treeRoots.Add(node);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"转换Node失败,nodeID:{data.Id}");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ConstructNodeRelationship()
|
||
{
|
||
var config = TableManager.Instance.Tables.RedPointConfig.DataMap;
|
||
|
||
foreach (var data in config.Values)
|
||
{
|
||
if (data.ParentID != RED_POINT_NODE_ROOT_FLAG)
|
||
{
|
||
var node = GetNodeByID(data.Id);
|
||
if (node != null)
|
||
{
|
||
var parentNode = GetNodeByID(data.ParentID);
|
||
if (parentNode != null)
|
||
{
|
||
parentNode.AddChild(node);
|
||
node.parent = parentNode;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private void InitAllNodes()
|
||
{
|
||
foreach (var node in _nodeDic.Values)
|
||
{
|
||
node.Init();
|
||
}
|
||
}
|
||
|
||
private void DisposeAllNodes()
|
||
{
|
||
foreach (var node in _nodeDic.Values)
|
||
{
|
||
node.Dispose();
|
||
}
|
||
}
|
||
|
||
private RedPointNode GetANode(int id, NodeType nodeType)
|
||
{
|
||
if (nodeType == NodeType.InValid)
|
||
{
|
||
Debug.LogError($"RedPoint Type非法,请修改配置!id:{id}");
|
||
return null;
|
||
}
|
||
RedPointNode result = null;
|
||
var nodeTypeStr = RED_POINT_NODE_TYPE_PREFIX + nodeType.ToString();
|
||
Type type = Type.GetType(nodeTypeStr);
|
||
if (type != null)
|
||
{
|
||
result = Activator.CreateInstance(type, id) as RedPointNode;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"枚举NodeType转Type失败!请检查是否有对应类型!nodeType:{nodeType.ToString()}");
|
||
}
|
||
return result;
|
||
}
|
||
|
||
#region API
|
||
public RedPointNode GetNodeByID(int nodeID)
|
||
{
|
||
return _nodeDic.GetValueOrDefault(nodeID, null);
|
||
}
|
||
#endregion
|
||
}
|