YIUIYooAssetsLoadComponentSystem.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using YooAsset;
  3. namespace ET.Client
  4. {
  5. /// <summary>
  6. /// YIUI资源管理器 yooasset扩展
  7. /// </summary>
  8. [FriendOf(typeof(YIUIYooAssetsLoadComponent))]
  9. [EntitySystemOf(typeof(YIUIYooAssetsLoadComponent))]
  10. public static partial class YIUIYooAssetsLoadComponentSystem
  11. {
  12. #region ObjectSystem
  13. [EntitySystem]
  14. private static void Awake(this YIUIYooAssetsLoadComponent self)
  15. {
  16. }
  17. [EntitySystem]
  18. private static void Destroy(this YIUIYooAssetsLoadComponent self)
  19. {
  20. self.ReleaseAllAction();
  21. }
  22. #endregion
  23. public static async ETTask<bool> Initialize(this YIUIYooAssetsLoadComponent self, string packageName = "DefaultPackage")
  24. {
  25. self.m_Package = YooAssets.GetPackage(packageName);
  26. if (self.m_Package == null)
  27. {
  28. Log.Error($"YooAsset 加载资源包失败: {packageName}");
  29. return false;
  30. }
  31. //YIUI会用到的各种加载 需要自行实现 当前是YooAsset 根据自己项目的资源管理器实现下面的方法
  32. #if !YIUIMACRO_SYNCLOAD_CLOSE
  33. YIUILoadDI.LoadAssetFunc = self.LoadAssetFunc; //同步加载
  34. #endif
  35. YIUILoadDI.LoadAssetAsyncFunc = self.LoadAssetAsyncFunc; //异步加载
  36. YIUILoadDI.ReleaseAction = self.ReleaseAction; //释放
  37. YIUILoadDI.VerifyAssetValidityFunc = self.VerifyAssetValidityFunc; //检查
  38. YIUILoadDI.ReleaseAllAction = self.ReleaseAllAction; //释放所有
  39. await EventSystem.Instance.PublishAsync(self.Scene(), new YIUIEvent_YooAssetsLoad_Completed
  40. {
  41. YooAssetsLoadRef = self
  42. });
  43. return true;
  44. }
  45. /// <summary>
  46. /// 释放方法
  47. /// </summary>
  48. /// <param name="hashCode">加载时所给到的唯一ID</param>
  49. private static void ReleaseAction(this YIUIYooAssetsLoadComponent self, int hashCode)
  50. {
  51. if (!self.m_AllHandle.TryGetValue(hashCode, out var value))
  52. {
  53. return;
  54. }
  55. value.Release();
  56. self.m_AllHandle.Remove(hashCode);
  57. }
  58. /// <summary>
  59. /// 异步加载
  60. /// </summary>
  61. /// <param name="arg1">包名</param>
  62. /// <param name="arg2">资源名</param>
  63. /// <param name="arg3">类型</param>
  64. /// <returns>返回值(obj资源对象,唯一ID)</returns>
  65. private static async ETTask<(UnityEngine.Object, int)> LoadAssetAsyncFunc(this YIUIYooAssetsLoadComponent self,
  66. string arg1,
  67. string arg2,
  68. Type arg3)
  69. {
  70. EntityRef<YIUIYooAssetsLoadComponent> selfRef = self;
  71. var handle = self.m_Package.LoadAssetAsync(arg2, arg3);
  72. await handle.Task;
  73. self = selfRef;
  74. return self.LoadAssetHandle(handle);
  75. }
  76. #if !YIUIMACRO_SYNCLOAD_CLOSE
  77. /// <summary>
  78. /// 同步加载
  79. /// </summary>
  80. /// <param name="arg1">包名</param>
  81. /// <param name="arg2">资源名</param>
  82. /// <param name="arg3">类型</param>
  83. /// <returns>返回值(obj资源对象,唯一ID)</returns>
  84. private static (UnityEngine.Object, int) LoadAssetFunc(this YIUIYooAssetsLoadComponent self, string arg1, string arg2, Type arg3)
  85. {
  86. var handle = self.m_Package.LoadAssetSync(arg2, arg3);
  87. return self.LoadAssetHandle(handle);
  88. }
  89. #endif
  90. //Demo中对YooAsset加载后的一个简单返回封装
  91. //只有成功加载才返回 否则直接释放
  92. private static (UnityEngine.Object, int) LoadAssetHandle(this YIUIYooAssetsLoadComponent self, AssetHandle handle)
  93. {
  94. if (handle.AssetObject != null)
  95. {
  96. var hashCode = handle.GetHashCode();
  97. self.m_AllHandle.Add(hashCode, handle);
  98. return (handle.AssetObject, hashCode);
  99. }
  100. else
  101. {
  102. handle.Release();
  103. return (null, 0);
  104. }
  105. }
  106. //释放所有
  107. private static void ReleaseAllAction(this YIUIYooAssetsLoadComponent self)
  108. {
  109. foreach (var handle in self.m_AllHandle.Values)
  110. {
  111. handle.Release();
  112. }
  113. self.m_AllHandle.Clear();
  114. }
  115. //检查合法
  116. private static bool VerifyAssetValidityFunc(this YIUIYooAssetsLoadComponent self, string arg1, string arg2)
  117. {
  118. return self.m_Package.CheckLocationValid(arg2);
  119. }
  120. /// <summary>
  121. /// 获取当前的资源包
  122. /// </summary>
  123. public static ResourcePackage GetPackage(this YIUIYooAssetsLoadComponent self)
  124. {
  125. return self.m_Package;
  126. }
  127. /// <summary>
  128. /// 获取资源信息
  129. /// </summary>
  130. /// <param name="location">资源的定位地址</param>
  131. /// <param name="type">资源类型</param>
  132. public static AssetInfo GetAssetInfo(this YIUIYooAssetsLoadComponent self, string location, Type type = null)
  133. {
  134. return self.m_Package?.GetAssetInfo(location, type);
  135. }
  136. /// <summary>
  137. /// 获取资源信息
  138. /// </summary>
  139. /// <param name="assetGUID">资源GUID</param>
  140. /// <param name="type">资源类型</param>
  141. public static AssetInfo GetAssetInfoByGUID(this YIUIYooAssetsLoadComponent self, string assetGUID, Type type = null)
  142. {
  143. return self.m_Package?.GetAssetInfoByGUID(assetGUID, type);
  144. }
  145. }
  146. }