ArtInstanceHandler.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. using XRTool.Util;
  9. namespace ShadowStudio.Model
  10. {
  11. /// <summary>
  12. /// 资源实例化抽象类
  13. /// 此类按照Resource方式进行简单实现
  14. /// </summary>
  15. public abstract class ArtInstanceHandler : ArtInstance
  16. {
  17. private ArtInfo info;
  18. public ArtInfo Info { get => info; set => info = value; }
  19. public UnityEngine.Object LoadCache { get => loadCache; set => loadCache = value; }
  20. private UnityEngine.Object loadCache;
  21. private string localPath;
  22. private Texture artIcon;
  23. private float process;
  24. /// <summary>
  25. /// 网络中下载到本地的文件路径
  26. /// </summary>
  27. public string localExtralPath
  28. {
  29. get
  30. {
  31. if (string.IsNullOrEmpty(localPath))
  32. {
  33. localPath = Path.Combine(BuildConfig.Instance.UserPath, Info.ArtType.ToString());
  34. }
  35. return localPath;
  36. }
  37. }
  38. public float DownProcess { get => process; set => process = value; }
  39. /// <summary>
  40. /// 获取缩略图
  41. /// </summary>
  42. /// <returns></returns>
  43. public virtual Texture GetIcon()
  44. {
  45. if (!artIcon)
  46. {
  47. if (Info != null && (Info.InstaceType == InstaceType.References || Info.InstaceType == InstaceType.ResourceLoad ||
  48. Info.InstaceType == InstaceType.UnityBase))
  49. {
  50. return artIcon = Resources.Load<Texture>(Info.Icon);
  51. }
  52. }
  53. return artIcon;
  54. }
  55. /// <summary>
  56. /// 实例化物体
  57. /// </summary>
  58. /// <returns></returns>
  59. public virtual UnityEngine.Object InstanceArt()
  60. {
  61. if (LoadCache)
  62. {
  63. return GameObject.Instantiate(LoadCache);
  64. }
  65. return null;
  66. }
  67. /// <summary>
  68. /// 同步加载资源
  69. /// </summary>
  70. /// <returns></returns>
  71. public virtual UnityEngine.Object LoadArt()
  72. {
  73. if (Info != null)
  74. {
  75. return LoadCache = Resources.Load(Info.Url);
  76. }
  77. return null;
  78. }
  79. /// <summary>
  80. /// 异步加载资源
  81. /// </summary>
  82. /// <param name="path"></param>
  83. /// <param name="process"></param>
  84. /// <param name="loaded"></param>
  85. public virtual void LoadArtAsyn(string path, Action<float> process, Action<UnityEngine.Object> loaded)
  86. {
  87. if (Info != null)
  88. {
  89. TimerMgr.Instance.StartCoroutine(LoadAsyn(path, process, loaded));
  90. }
  91. else
  92. {
  93. LoadCache = null;
  94. process?.Invoke(1);
  95. loaded?.Invoke(LoadCache);
  96. }
  97. }
  98. /// <summary>
  99. /// 异步加载
  100. /// </summary>
  101. /// <param name="path"></param>
  102. /// <param name="process"></param>
  103. /// <param name="loaded"></param>
  104. /// <returns></returns>
  105. public virtual IEnumerator LoadAsyn(string path, Action<float> process, Action<UnityEngine.Object> loaded)
  106. {
  107. var sync = Resources.LoadAsync(path);
  108. while (!sync.isDone)
  109. {
  110. process?.Invoke(sync.progress);
  111. yield return new WaitForFixedUpdate();
  112. }
  113. LoadCache = sync.asset;
  114. process?.Invoke(1);
  115. loaded?.Invoke(LoadCache);
  116. }
  117. /// <summary>
  118. /// 初始化数据
  119. /// </summary>
  120. /// <param name="info"></param>
  121. public virtual void SetData(ArtInfo info, string containerName = "")
  122. {
  123. this.Info = info;
  124. }
  125. /// <summary>
  126. /// 创建资源对应的组件
  127. /// </summary>
  128. /// <returns></returns>
  129. public virtual ArtComponent InstanceComponent()
  130. {
  131. if (Info != null && !string.IsNullOrEmpty(Info.Component))
  132. {
  133. return ArtInfoMgr.Instance.InstanceComponent(Info.Component);
  134. }
  135. return null;
  136. }
  137. /// <summary>
  138. /// 获取外部资源的缩略图
  139. /// 如果是模型,获取icon的图片资源
  140. /// 如果是图片,直接获取本身url作为缩略图
  141. /// 如果是视频,则自行获取对应的缩略图:使用VideoPlayer读取对应帧,此处不提供方法
  142. /// </summary>
  143. /// <param name="loadTex"></param>
  144. public virtual void GetIcon(Action<Texture> loadTex)
  145. {
  146. if (artIcon)
  147. {
  148. loadTex?.Invoke(artIcon);
  149. return;
  150. }
  151. if (Info.ArtType == ArtType.Model || Info.ArtType == ArtType.Image || Info.ArtType == ArtType.Movies)
  152. {
  153. string url = Info.Icon;
  154. if (string.IsNullOrEmpty(url) && Info.ArtType == ArtType.Image)
  155. {
  156. url = Info.Url;
  157. }
  158. string serverPath = BuildConfig.Instance.ServerUrl + "/" + url;// Path.Combine(ResourcesManager.ArtServerPath, url);
  159. string filePath = Path.Combine(localExtralPath, url);
  160. try
  161. {
  162. ///文件不存在,下载
  163. ///下载后,加载
  164. DownloadHandlerTexture texHander = new DownloadHandlerTexture(true);
  165. if (!File.Exists(filePath))
  166. {
  167. TimerMgr.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (isComplete, handler) =>
  168. {
  169. LoadTexture(loadTex, handler.downloadHandler);
  170. }, texHander));
  171. }
  172. else
  173. {
  174. TimerMgr.Instance.StartCoroutine(DataFileUtil.RequestDownData(@"file://" + filePath, (isComplete, handler) =>
  175. {
  176. LoadTexture(loadTex, handler.downloadHandler);
  177. }, texHander));
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. UnityLog.LogError(JsonConvert.SerializeObject(Info) + "icon error" + ex.ToString());
  183. }
  184. }
  185. else if (Info.ArtType == ArtType.Movies)
  186. {
  187. loadTex?.Invoke(null);
  188. }
  189. }
  190. public virtual void LoadTexture(Action<Texture> loadIcon, DownloadHandler handler)
  191. {
  192. Texture texture = null;
  193. if (handler != null)
  194. {
  195. var texHandler = handler as DownloadHandlerTexture;
  196. if (texHandler != null)
  197. {
  198. texture = texHandler.texture;
  199. }
  200. else
  201. {
  202. UnityLog.LogError(JsonConvert.SerializeObject(Info) + "icon load error");
  203. }
  204. }
  205. else
  206. {
  207. UnityLog.LogError(JsonConvert.SerializeObject(Info) + "icon handler error");
  208. }
  209. if (texture == null)
  210. {
  211. UnityLog.LogError(JsonConvert.SerializeObject(Info) + "no iconread");
  212. }
  213. loadIcon?.Invoke(texture);
  214. artIcon = texture;
  215. }
  216. /// <summary>
  217. /// 判断是否已下载指定文件
  218. /// </summary>
  219. /// <returns></returns>
  220. public bool IsDownLoad()
  221. {
  222. string filePath = Path.Combine(localExtralPath, Info.Url);
  223. return File.Exists(filePath);
  224. }
  225. public string GetFilePath(string url)
  226. {
  227. string[] arr = url.Split('/');
  228. string path = url;
  229. if (arr.Length > 2)
  230. {
  231. path = "";
  232. for (int i = arr.Length - 2; i < arr.Length; i++)
  233. {
  234. if (i != arr.Length -1)
  235. {
  236. path += arr[i] + "/";
  237. }
  238. else
  239. {
  240. path += arr[i];
  241. }
  242. }
  243. }
  244. UnityLog.Log(path);
  245. return path;
  246. }
  247. public void DownLoad(Action<float, float> downProcess, Action<string, byte[]> downComplete)
  248. {
  249. string url = Info.Url;
  250. string serverPath = Path.Combine(BuildConfig.Instance.ServerUrl, url);
  251. string filePath = Path.Combine(localExtralPath, GetFilePath(url));
  252. TimerMgr.Instance.StartCoroutine(DataFileUtil.DownLoadData(serverPath, filePath, (string path, UnityWebRequest handler) =>
  253. {
  254. downComplete?.Invoke(path, handler.downloadHandler.data);
  255. }
  256. ));
  257. }
  258. }
  259. }