EntryLoadCfg_Client.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using YooAsset;
  6. using ET.EventType;
  7. using System.Threading.Tasks;
  8. namespace ET
  9. {
  10. [Event(SceneType.Process)]
  11. public class EntryLoadCfg_Client : AEvent<ET.EventType.EntryLoadCfg>
  12. {
  13. protected override async ETTask Run(Scene scene, EntryLoadCfg args)
  14. {
  15. //加载战斗相关资源
  16. Root.Instance.Scene.AddComponent<BattleResComponent>();
  17. Dictionary<Type, byte[]> configBytes = new Dictionary<Type, byte[]>();
  18. HashSet<Type> configTypes = EventSystem.Instance.GetTypes(typeof(ConfigAttribute));
  19. foreach (Type configType in configTypes)
  20. {
  21. var op = await YooAssetProxy.GetRawFileAsync(configType.Name);
  22. var cfg = op.GetRawBytes();
  23. if (cfg != null)
  24. {
  25. configBytes[configType] = cfg;
  26. Log.Debug($"load config {configType} ok.");
  27. }
  28. else
  29. {
  30. Log.Error($"load config {configType} failed.");
  31. }
  32. }
  33. var listTasks = ListComponent<Task>.Create();
  34. foreach (Type type in configBytes.Keys)
  35. {
  36. byte[] oneConfigBytes = configBytes[type];
  37. Task task = Task.Run(() => LoadOneInThread(type, oneConfigBytes));
  38. listTasks.Add(task);
  39. }
  40. await Task.WhenAll(listTasks.ToArray());
  41. Log.Debug("register all config ok");
  42. }
  43. private void LoadOneInThread(Type configType, byte[] oneConfigBytes)
  44. {
  45. object category = SerializeHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  46. (category as ISingleton).Register();
  47. Log.Debug($"Deserialize: {configType} ok");
  48. }
  49. }
  50. }