RedDotMgr.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //------------------------------------------------------------
  2. // Author: 亦亦
  3. // Mail: 379338943@qq.com
  4. // Data: 2023年2月12日
  5. //------------------------------------------------------------
  6. using ET;
  7. using System;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityObject = UnityEngine.Object;
  11. namespace YIUIFramework
  12. {
  13. /// <summary>
  14. /// 红点 管理器
  15. /// 文档: https://lib9kmxvq7k.feishu.cn/wiki/XzyawmryHitNVNk9QVtcDAftn5O
  16. /// </summary>
  17. [YIUISingleton(1000)]
  18. public partial class RedDotMgr : YIUISingleton<RedDotMgr>
  19. {
  20. //实时修改红点还是异步脏标定时修改
  21. private static readonly bool SyncSetCount = false; //默认异步
  22. private const string RedDotConfigAssetName = "RedDotConfigAsset";
  23. private readonly Dictionary<int, RedDotData> m_AllRedDotData = new();
  24. public IReadOnlyDictionary<int, RedDotData> AllRedDotData => m_AllRedDotData;
  25. private RedDotConfigAsset m_RedDotConfigAsset;
  26. protected override void OnDispose()
  27. {
  28. DisposeDirty();
  29. }
  30. protected override async ETTask<bool> MgrAsyncInit()
  31. {
  32. var resultConfig = await LoadConfigAsset();
  33. if (!resultConfig) return false;
  34. #if UNITY_EDITOR || YIUIMACRO_REDDOT_STACK
  35. var resultKey = await LoadKeyAsset();
  36. if (!resultKey) return false;
  37. #endif
  38. if (!SyncSetCount)
  39. {
  40. InitAsyncDirty();
  41. }
  42. return true;
  43. }
  44. /// <summary>
  45. /// 加载config
  46. /// </summary>
  47. private async ETTask<bool> LoadConfigAsset()
  48. {
  49. var loadResult = await ET.EventSystem.Instance?.YIUIInvokeEntityAsyncSafety<YIUIInvokeEntity_Load, ETTask<UnityObject>>(Entity, new YIUIInvokeEntity_Load
  50. {
  51. LoadType = typeof(RedDotConfigAsset),
  52. ResName = RedDotConfigAssetName
  53. });
  54. if (loadResult == null)
  55. {
  56. Debug.LogError($"初始化失败 没有加载到目标数据 {RedDotConfigAssetName}");
  57. return false;
  58. }
  59. m_RedDotConfigAsset = (RedDotConfigAsset)loadResult;
  60. InitNewAllData();
  61. InitLinkData();
  62. ET.EventSystem.Instance?.YIUIInvokeEntitySyncSafety(Entity, new YIUIInvokeEntity_Release
  63. {
  64. obj = loadResult
  65. });
  66. return true;
  67. }
  68. /// <summary>
  69. /// 初始化创建所有数据
  70. /// </summary>
  71. private void InitNewAllData()
  72. {
  73. m_AllRedDotData.Clear();
  74. foreach (int key in RedDotKeyHelper.GetKeys(true))
  75. {
  76. //有配置则使用配置 没有则使用默认配置
  77. var config = m_RedDotConfigAsset.GetConfigData(key) ?? new RedDotConfigData { Key = key };
  78. var data = new RedDotData(config);
  79. m_AllRedDotData.Add(key, data);
  80. }
  81. }
  82. /// <summary>
  83. /// 初始化红点关联数据
  84. /// </summary>
  85. private void InitLinkData()
  86. {
  87. foreach (var config in m_RedDotConfigAsset.AllRedDotConfigDic.Values)
  88. {
  89. var data = GetData(config.Key);
  90. if (data == null) continue;
  91. foreach (var parentId in config.ParentList)
  92. {
  93. var parentData = GetData(parentId);
  94. if (parentData != null)
  95. {
  96. data.AddParent(parentData);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }