ExtralLoadHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using TriLib;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. using XRTool.Util;
  8. using Newtonsoft.Json;
  9. namespace ShadowStudio.Model
  10. {
  11. public enum ArtState
  12. {
  13. UnLoad = -1,
  14. Loading = 0,
  15. Loaded = 1
  16. }
  17. /// <summary>
  18. /// 外部资源加载
  19. /// 可能是模型 zip文件
  20. /// 可能是图片
  21. /// 可能是视频,视频可在线播放,无需下载
  22. /// </summary>
  23. public class ExtralLoadHandler : ArtInstanceHandler
  24. {
  25. private ArtState artState = ArtState.UnLoad;
  26. private Action<float> loadPorcess;
  27. private Action<UnityEngine.Object> artLoaded;
  28. public override UnityEngine.Object InstanceArt()
  29. {
  30. return LoadCache;
  31. }
  32. /// <summary>
  33. /// 异步加载资源
  34. /// 此函数仅加载资源,不负责下载和解压
  35. /// 与异步获取icon不同,异步获取icon,当不存在文件时,会先下载资源再加载
  36. /// </summary>
  37. /// <param name="path"></param>
  38. /// <param name="process"></param>
  39. /// <param name="loaded"></param>
  40. /// <returns></returns>
  41. public override IEnumerator LoadAsyn(string path, Action<float> process, Action<UnityEngine.Object> loaded)
  42. {
  43. if (Info.ArtType == ArtType.Movies)
  44. {
  45. process?.Invoke(1);
  46. loaded(null);
  47. }
  48. else if (Info.ArtType == ArtType.Image)
  49. {
  50. //Texture icon = GetIcon();
  51. //yield return icon;
  52. //if (icon)
  53. //{
  54. // LoadCache = icon;
  55. // process?.Invoke(1);
  56. // loaded?.Invoke(LoadCache);
  57. //}
  58. //else
  59. //{
  60. // GetIcon((tex) =>
  61. // {
  62. // LoadCache = tex;
  63. // process?.Invoke(1);
  64. // loaded?.Invoke(LoadCache);
  65. // });
  66. //}
  67. if (LoadCache)
  68. {
  69. process?.Invoke(1);
  70. loaded?.Invoke(LoadCache);
  71. yield break;
  72. }
  73. else
  74. {
  75. string url = Info.Url;
  76. if (string.IsNullOrEmpty(url))
  77. {
  78. url = Info.Icon;
  79. }
  80. string serverPath = ResourcesManager.ArtServerPath + "/" + url;// Path.Combine(ResourcesManager.ArtServerPath, url);
  81. string filePath = Path.Combine(localExtralPath, url);
  82. try
  83. {
  84. ///文件不存在,下载
  85. ///下载后,加载
  86. DownloadHandlerTexture texHander = new DownloadHandlerTexture(true);
  87. if (!File.Exists(filePath))
  88. {
  89. GameSession.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) =>
  90. {
  91. LoadTexture((tex) =>
  92. {
  93. LoadCache = tex;
  94. process?.Invoke(1);
  95. loaded?.Invoke(LoadCache);
  96. }, handler.downloadHandler);
  97. }, texHander));
  98. }
  99. else
  100. {
  101. GameSession.Instance.StartCoroutine(DataFileUtil.RequestDownData(@"file://" + filePath, (isComplete, handler) =>
  102. {
  103. LoadTexture((tex) =>
  104. {
  105. LoadCache = tex;
  106. process?.Invoke(1);
  107. loaded?.Invoke(LoadCache);
  108. }, handler.downloadHandler);
  109. }, texHander));
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. UnityLog.Instance.LogError(JsonConvert.SerializeObject(Info) + "icon error" + ex.ToString());
  115. }
  116. }
  117. }
  118. else if (Info.ArtType == ArtType.Model)
  119. {
  120. if (LoadCache)
  121. {
  122. string cloneName = LoadCache.name;
  123. LoadCache = GameObject.Instantiate(LoadCache);
  124. LoadCache.name = cloneName;
  125. loaded?.Invoke(LoadCache);
  126. yield break;
  127. }
  128. if (artState == ArtState.Loading)
  129. {
  130. loadPorcess += (p) => process?.Invoke(p);
  131. artLoaded += (obj) =>
  132. {
  133. string cloneName = LoadCache.name;
  134. LoadCache = GameObject.Instantiate(LoadCache);
  135. LoadCache.name = cloneName;
  136. loaded?.Invoke(LoadCache);
  137. artLoaded = null;
  138. loadPorcess = null;
  139. };
  140. yield break;
  141. }
  142. if (artState == ArtState.UnLoad)
  143. {
  144. artState = ArtState.Loading;
  145. }
  146. string url = Info.Url;
  147. string serverPath = Path.Combine(ResourcesManager.ArtServerPath, url);
  148. string filePath = Path.Combine(localExtralPath, url);
  149. try
  150. {
  151. ///文件不存在,下载
  152. ///模型的图例?是否下载呢?是个问题
  153. if (!File.Exists(filePath))
  154. {
  155. GameSession.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) =>
  156. {
  157. if (handler != null)
  158. {
  159. LoadInternal(filePath, loaded, handler.downloadHandler.data);
  160. }
  161. else
  162. {
  163. UnityLog.Instance.LogError(JsonConvert.SerializeObject(Info) + "frist load error!");
  164. }
  165. }, null, (all, cur) =>
  166. {
  167. DownProcess = cur;
  168. process?.Invoke(cur);
  169. loadPorcess?.Invoke(cur);
  170. }));
  171. }
  172. else
  173. {
  174. GameSession.Instance.StartCoroutine(DataFileUtil.RequestDownData(filePath, (tmppath, handler) =>
  175. {
  176. LoadInternal(filePath, loaded, handler.downloadHandler != null ? handler.downloadHandler.data : null);
  177. }, null, (all, cur) =>
  178. {
  179. DownProcess = cur;
  180. process?.Invoke(cur);
  181. loadPorcess?.Invoke(cur);
  182. }));
  183. }
  184. }
  185. catch
  186. {
  187. loaded?.Invoke(null);
  188. UnityLog.Instance.LogError(Info.ArtId + "load exception!");
  189. }
  190. }
  191. yield return null;
  192. }
  193. /// <summary>
  194. /// 加载模型
  195. /// </summary>
  196. /// <param name="filePath"></param>
  197. /// <param name="loaded"></param>
  198. private void LoadModel(string filePath, Action<UnityEngine.Object> loaded)
  199. {
  200. using (var assetLoader = new AssetLoaderAsync())
  201. {
  202. try
  203. {
  204. var assetLoaderOptions = AssetLoaderOptions.CreateInstance();
  205. assetLoaderOptions.RotationAngles = new Vector3(90f, 180f, 0f);
  206. assetLoaderOptions.AutoPlayAnimations = true;
  207. assetLoader.LoadFromFile(filePath, assetLoaderOptions, null, (loadedGameObject) =>
  208. {
  209. LoadCache = loadedGameObject;
  210. loaded?.Invoke(loadedGameObject);
  211. });
  212. }
  213. catch (Exception e)
  214. {
  215. Debug.LogError(e.ToString());
  216. loaded?.Invoke(null);
  217. }
  218. }
  219. }
  220. private AssetLoaderOptions GetAssetLoaderOptions()
  221. {
  222. var assetLoaderOptions = AssetLoaderOptions.CreateInstance();
  223. assetLoaderOptions.DontLoadCameras = false;
  224. assetLoaderOptions.DontLoadLights = false;
  225. assetLoaderOptions.UseCutoutMaterials = false;
  226. assetLoaderOptions.AddAssetUnloader = true;
  227. return assetLoaderOptions;
  228. }
  229. private void LoadInternal(string filename, Action<UnityEngine.Object> loaded, byte[] fileBytes = null)
  230. {
  231. //_loadingTimer.Reset();
  232. //_loadingTimer.Start();
  233. //PreLoadSetup();
  234. var assetLoaderOptions = GetAssetLoaderOptions();
  235. using (var assetLoader = new AssetLoaderAsync())
  236. {
  237. //assetLoader.OnMetadataProcessed += AssetLoader_OnMetadataProcessed;
  238. try
  239. {
  240. if (fileBytes != null && fileBytes.Length > 0)
  241. {
  242. var extension = FileUtils.GetFileExtension(filename);
  243. assetLoader.LoadFromMemoryWithTextures(fileBytes, extension, assetLoaderOptions, null, delegate (GameObject loadedGameObject)
  244. {
  245. LoadCache = loadedGameObject;
  246. loaded?.Invoke(LoadCache);
  247. artLoaded?.Invoke(LoadCache);
  248. artState = ArtState.Loaded;
  249. });
  250. }
  251. else if (!string.IsNullOrEmpty(filename))
  252. {
  253. assetLoader.LoadFromFileWithTextures(filename, assetLoaderOptions, null, delegate (GameObject loadedGameObject)
  254. {
  255. LoadCache = loadedGameObject;
  256. loaded?.Invoke(LoadCache);
  257. artLoaded?.Invoke(LoadCache);
  258. artState = ArtState.Loaded;
  259. });
  260. }
  261. else
  262. {
  263. loaded?.Invoke(null);
  264. artLoaded?.Invoke(null);
  265. artState = ArtState.UnLoad;
  266. throw new Exception("File not selected");
  267. }
  268. }
  269. catch
  270. {
  271. loaded?.Invoke(null);
  272. artLoaded?.Invoke(null);
  273. artState = ArtState.UnLoad;
  274. UnityLog.Instance.LogError(Info.ArtId + "资源加载失败");
  275. }
  276. }
  277. }
  278. }
  279. }