ArtHandler.cs 14 KB

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