ArtHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using XRTool.Util;
  6. namespace ShadowStudio.Model
  7. {
  8. /// <summary>
  9. /// 资源的处理类
  10. /// 相同资源只会创建一个实例
  11. /// </summary>
  12. public class ArtHandler : ArtInstance, ArtPlay
  13. {
  14. //public ArtHandler() {
  15. // Debug.Log("ArtHandler:" + UnityUtil.CurTimeString);
  16. //}
  17. private ArtInstanceHandler instancer;
  18. private ArtPlay artDisPlay;
  19. private ArtInfo info;
  20. public ArtInstanceHandler Instancer { get => instancer; set => instancer = value; }
  21. public ArtPlay ArtDisPlay { get => artDisPlay; set => artDisPlay = value; }
  22. public float DownProcess { get => Instancer.DownProcess; set => Instancer.DownProcess = value; }
  23. public event Action<ArtPlay> OnCreateArt;
  24. private Texture artIcon;
  25. //private Action<float> loadProcess;
  26. //private Action<UnityEngine.Object> artLoaded;
  27. /// <summary>
  28. /// 初始化资源
  29. /// </summary>
  30. /// <param name="info"></param>
  31. public virtual void SetData(ArtInfo info, string containerName = "")
  32. {
  33. if (info != null)
  34. {
  35. this.info = info;
  36. if (info.InstaceType == InstaceType.ResourceLoad)
  37. {
  38. Instancer = new ResourcesHandler();
  39. }
  40. else if (info.InstaceType == InstaceType.UnityBase)
  41. {
  42. Instancer = new UnityBaseHandler();
  43. }
  44. else if (info.InstaceType == InstaceType.References)
  45. {
  46. Instancer = new ReferencesLoadHandler();
  47. }
  48. else if (info.InstaceType == InstaceType.AssetBundle)
  49. {
  50. Instancer = new ExtralLoadHandler();
  51. }
  52. else if (info.InstaceType == InstaceType.ExtralURLDownload)
  53. {
  54. Instancer = new ExtralLoadHandler();
  55. }
  56. else if (info.InstaceType == InstaceType.LocalPath)
  57. {
  58. Instancer = new ExtralLoadHandler();
  59. }
  60. if (Instancer != null)
  61. {
  62. Instancer.SetData(info);
  63. }
  64. if (info.ArtType == ArtType.Model)
  65. {
  66. ArtDisPlay = new ModelPlayer();
  67. }
  68. else if (info.ArtType == ArtType.WorldDlg)
  69. {
  70. ArtDisPlay = new WorldDlgPlayer();
  71. }
  72. else if (info.ArtType == ArtType.Image)
  73. {
  74. ArtDisPlay = new PictruePlayer();
  75. }
  76. else if (info.ArtType == ArtType.Movies)
  77. {
  78. ArtDisPlay = new ArtVideoPlayer();
  79. }
  80. else if (info.ArtType == ArtType.Prefab)
  81. {
  82. ArtDisPlay = new PrefabPlayer(containerName);
  83. }
  84. else if (info.ArtType == ArtType.Model2D)
  85. {
  86. ArtDisPlay = new Model2DPlayer();
  87. }
  88. if (ArtDisPlay != null)
  89. {
  90. ArtDisPlay.SetData(info);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 同步加载资源
  96. /// </summary>
  97. /// <returns></returns>
  98. public virtual UnityEngine.Object LoadArt()
  99. {
  100. if (Instancer != null && ArtDisPlay != null)
  101. {
  102. if (IsNeedLoad())
  103. {
  104. return Instancer.LoadArt();
  105. }
  106. }
  107. return null;
  108. }
  109. /// <summary>
  110. /// 异步加载资源
  111. /// </summary>
  112. /// <param name="path"></param>
  113. /// <param name="process"></param>
  114. /// <param name="loaded"></param>
  115. public virtual void LoadArtAsyn(string path, Action<float> process, Action<UnityEngine.Object> loaded)
  116. {
  117. if (Instancer != null && ArtDisPlay != null)
  118. {
  119. if (IsNeedLoad())
  120. {
  121. Instancer.LoadArtAsyn(path, process, loaded);
  122. return;
  123. }
  124. process?.Invoke(1);
  125. loaded?.Invoke(null);
  126. }
  127. }
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. /// <returns></returns>
  132. public virtual UnityEngine.Object InstanceArt()
  133. {
  134. if (Instancer != null && ArtDisPlay != null)
  135. {
  136. if (IsNeedInstance())
  137. {
  138. return Instancer.InstanceArt();
  139. }
  140. }
  141. return null;
  142. }
  143. /// <summary>
  144. /// 添加到容器中
  145. /// </summary>
  146. /// <param name="obj"></param>
  147. public void AddToNode(UnityEngine.Object obj)
  148. {
  149. if (ArtDisPlay != null)
  150. {
  151. ArtDisPlay.AddToNode(obj);
  152. }
  153. }
  154. /// <summary>
  155. /// 获取图标
  156. /// </summary>
  157. /// <returns></returns>
  158. public Texture GetIcon()
  159. {
  160. if (artIcon)
  161. {
  162. return artIcon;
  163. }
  164. if ((info.InstaceType == InstaceType.References || info.InstaceType == InstaceType.ResourceLoad ||
  165. info.InstaceType == InstaceType.UnityBase) && Instancer != null)
  166. {
  167. if (info.ArtType == ArtType.Image)
  168. {
  169. var tex = Instancer.LoadArt();
  170. return artIcon = (tex as Texture);
  171. }
  172. else if (info.ArtType == ArtType.Movies)
  173. {
  174. return artIcon = Instancer.GetIcon();
  175. }
  176. else
  177. {
  178. return artIcon = Instancer.GetIcon();
  179. }
  180. }
  181. return artIcon;
  182. }
  183. /// <summary>
  184. /// 获取缩略图
  185. /// </summary>
  186. /// <param name="loadTex"></param>
  187. public void GetIcon(Action<Texture> loadTex)
  188. {
  189. if (artIcon)
  190. {
  191. loadTex?.Invoke(artIcon);
  192. return;
  193. }
  194. Instancer.GetIcon(loadTex);
  195. loadTex += (tex) => { artIcon = tex; };
  196. }
  197. /// <summary>
  198. /// 创建对应的组件
  199. /// </summary>
  200. /// <returns></returns>
  201. public ArtComponent InstanceComponent()
  202. {
  203. if (Instancer != null)
  204. {
  205. return Instancer.InstanceComponent();
  206. }
  207. return null;
  208. }
  209. public bool IsNeedLoad()
  210. {
  211. if (ArtDisPlay != null)
  212. {
  213. return ArtDisPlay.IsNeedLoad();
  214. }
  215. return false;
  216. }
  217. public bool IsNeedInstance()
  218. {
  219. if (ArtDisPlay != null)
  220. {
  221. return ArtDisPlay.IsNeedInstance();
  222. }
  223. return false;
  224. }
  225. /// <summary>
  226. /// 创建容器
  227. /// </summary>
  228. /// <returns></returns>
  229. public ArtContainer CreateContainer()
  230. {
  231. if (ArtDisPlay != null)
  232. {
  233. return ArtDisPlay.CreateContainer();
  234. }
  235. return null;
  236. }
  237. /// <summary>
  238. /// 创建一个美术展示容器
  239. /// </summary>
  240. public virtual ArtContainer CreateArt()
  241. {
  242. ///创建容器
  243. var container = CreateContainer();
  244. if (container == null)
  245. {
  246. UnityLog.LogError(info.ArtId + "__" + info.ArtType + "容器不存在!");
  247. return null;
  248. }
  249. AddToNode(container.GetInstace());
  250. ///资源的加载走异步处理
  251. if ((info.InstaceType == InstaceType.References || info.InstaceType == InstaceType.ResourceLoad ||
  252. info.InstaceType == InstaceType.UnityBase))
  253. {
  254. UnityEngine.Object obj = null;
  255. if (IsNeedLoad())
  256. {
  257. obj = LoadArt();
  258. }
  259. if (IsNeedInstance())
  260. {
  261. obj = InstanceArt();
  262. }
  263. if (obj is GameObject)
  264. {
  265. if (!(obj as GameObject).activeSelf)
  266. {
  267. (obj as GameObject).SetActive(true);
  268. }
  269. }
  270. container.AddToContainer(obj, info);
  271. var artComponent = InstanceComponent();
  272. container.AddArtComponent(artComponent);
  273. if (artComponent != null)
  274. {
  275. artComponent.SetContainer(container as ArtContainerHandler, obj, info);
  276. }
  277. }
  278. else
  279. {
  280. if (IsNeedLoad())
  281. {
  282. LoadArtAsyn("", container.UpdateProcess, (obj) =>
  283. {
  284. if (IsNeedInstance())
  285. {
  286. obj = InstanceArt();
  287. }
  288. if (obj is GameObject)
  289. {
  290. if (!(obj as GameObject).activeSelf)
  291. {
  292. (obj as GameObject).SetActive(true);
  293. }
  294. }
  295. container.AddToContainer(obj, info);
  296. var artComponent = InstanceComponent();
  297. container.AddArtComponent(artComponent);
  298. if (artComponent != null)
  299. {
  300. artComponent.SetContainer(container as ArtContainerHandler, obj, info);
  301. }
  302. });
  303. }
  304. }
  305. container.SetArtHandler(this);
  306. OnCreateArt?.Invoke(ArtDisPlay);
  307. return container;
  308. }
  309. /// <summary>
  310. /// 异步创建容器
  311. /// </summary>
  312. /// <param name="createAction"></param>
  313. public virtual void CreateArt(Action<ArtContainer> createAction)
  314. {
  315. }
  316. /// <summary>
  317. /// 是否已下载
  318. /// </summary>
  319. /// <returns></returns>
  320. public bool IsDownLoad()
  321. {
  322. if ((info.InstaceType == InstaceType.References || info.InstaceType == InstaceType.ResourceLoad ||
  323. info.InstaceType == InstaceType.UnityBase))
  324. {
  325. Instancer.DownProcess = 1;
  326. return true;
  327. }
  328. return Instancer.IsDownLoad();
  329. }
  330. public void DownLoad(Action<float, float> downProcess, Action<string, byte[]> downComplete)
  331. {
  332. Instancer.DownLoad(downProcess, downComplete);
  333. }
  334. }
  335. }