ResourcesComponent.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using YooAsset;
  6. namespace ET
  7. {
  8. /// <summary>
  9. /// 资源文件查询服务类
  10. /// </summary>
  11. public class GameQueryServices : IQueryServices
  12. {
  13. public bool QueryStreamingAssets(string packageName, string fileName)
  14. {
  15. // 注意:fileName包含文件格式
  16. string filePath = Path.Combine(YooAssetSettings.DefaultYooFolderName, packageName, fileName);
  17. return BetterStreamingAssets.FileExists(filePath);
  18. }
  19. }
  20. /// <summary>
  21. /// 远端资源地址查询服务类
  22. /// </summary>
  23. public class RemoteServices : IRemoteServices
  24. {
  25. private readonly string _defaultHostServer;
  26. private readonly string _fallbackHostServer;
  27. public RemoteServices(string defaultHostServer, string fallbackHostServer)
  28. {
  29. _defaultHostServer = defaultHostServer;
  30. _fallbackHostServer = fallbackHostServer;
  31. }
  32. string IRemoteServices.GetRemoteMainURL(string fileName)
  33. {
  34. return $"{_defaultHostServer}/{fileName}";
  35. }
  36. string IRemoteServices.GetRemoteFallbackURL(string fileName)
  37. {
  38. return $"{_fallbackHostServer}/{fileName}";
  39. }
  40. }
  41. public class ResourcesComponent: Singleton<ResourcesComponent>, ISingletonAwake
  42. {
  43. public void Awake()
  44. {
  45. YooAssets.Initialize();
  46. BetterStreamingAssets.Initialize();
  47. }
  48. protected override void Destroy()
  49. {
  50. YooAssets.Destroy();
  51. }
  52. public async ETTask CreatePackageAsync(string packageName, bool isDefault = false)
  53. {
  54. ResourcePackage package = YooAssets.CreatePackage(packageName);
  55. if (isDefault)
  56. {
  57. YooAssets.SetDefaultPackage(package);
  58. }
  59. GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  60. EPlayMode ePlayMode = globalConfig.EPlayMode;
  61. // 编辑器下的模拟模式
  62. switch (ePlayMode)
  63. {
  64. case EPlayMode.EditorSimulateMode:
  65. {
  66. EditorSimulateModeParameters createParameters = new();
  67. createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(packageName);
  68. await package.InitializeAsync(createParameters).Task;
  69. break;
  70. }
  71. case EPlayMode.OfflinePlayMode:
  72. {
  73. OfflinePlayModeParameters createParameters = new();
  74. await package.InitializeAsync(createParameters).Task;
  75. break;
  76. }
  77. case EPlayMode.HostPlayMode:
  78. {
  79. string defaultHostServer = GetHostServerURL();
  80. string fallbackHostServer = GetHostServerURL();
  81. HostPlayModeParameters createParameters = new();
  82. createParameters.QueryServices = new GameQueryServices();
  83. createParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
  84. await package.InitializeAsync(createParameters).Task;
  85. break;
  86. }
  87. default:
  88. throw new ArgumentOutOfRangeException();
  89. }
  90. return;
  91. string GetHostServerURL()
  92. {
  93. //string hostServerIP = "http://10.0.2.2"; //安卓模拟器地址
  94. string hostServerIP = "http://127.0.0.1";
  95. string appVersion = "v1.0";
  96. #if UNITY_EDITOR
  97. if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
  98. return $"{hostServerIP}/CDN/Android/{appVersion}";
  99. else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
  100. return $"{hostServerIP}/CDN/IPhone/{appVersion}";
  101. else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
  102. return $"{hostServerIP}/CDN/WebGL/{appVersion}";
  103. else
  104. return $"{hostServerIP}/CDN/PC/{appVersion}";
  105. #else
  106. if (Application.platform == RuntimePlatform.Android)
  107. return $"{hostServerIP}/CDN/Android/{appVersion}";
  108. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  109. return $"{hostServerIP}/CDN/IPhone/{appVersion}";
  110. else if (Application.platform == RuntimePlatform.WebGLPlayer)
  111. return $"{hostServerIP}/CDN/WebGL/{appVersion}";
  112. else
  113. return $"{hostServerIP}/CDN/PC/{appVersion}";
  114. #endif
  115. }
  116. }
  117. public void DestroyPackage(string packageName)
  118. {
  119. ResourcePackage package = YooAssets.GetPackage(packageName);
  120. package.UnloadUnusedAssets();
  121. }
  122. /// <summary>
  123. /// 主要用来加载dll config aotdll,因为这时候纤程还没创建,无法使用ResourcesLoaderComponent。
  124. /// 游戏中的资源应该使用ResourcesLoaderComponent来加载
  125. /// </summary>
  126. public async ETTask<T> LoadAssetAsync<T>(string location) where T: UnityEngine.Object
  127. {
  128. AssetOperationHandle handle = YooAssets.LoadAssetAsync<T>(location);
  129. await handle.Task;
  130. T t = (T)handle.AssetObject;
  131. handle.Release();
  132. return t;
  133. }
  134. /// <summary>
  135. /// 主要用来加载dll config aotdll,因为这时候纤程还没创建,无法使用ResourcesLoaderComponent。
  136. /// 游戏中的资源应该使用ResourcesLoaderComponent来加载
  137. /// </summary>
  138. public async ETTask<Dictionary<string, T>> LoadAllAssetsAsync<T>(string location) where T: UnityEngine.Object
  139. {
  140. AllAssetsOperationHandle allAssetsOperationHandle = YooAssets.LoadAllAssetsAsync<T>(location);
  141. await allAssetsOperationHandle.Task;
  142. Dictionary<string, T> dictionary = new Dictionary<string, T>();
  143. foreach(UnityEngine.Object assetObj in allAssetsOperationHandle.AllAssetObjects)
  144. {
  145. T t = assetObj as T;
  146. dictionary.Add(t.name, t);
  147. }
  148. allAssetsOperationHandle.Release();
  149. return dictionary;
  150. }
  151. }
  152. }