TemplateModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using TriLibCore;
  6. using UnityEngine;
  7. public class TemplateModel : BaseTemPlate
  8. {
  9. private GameObject m_ModelObj = null;
  10. protected override void OnEnable()
  11. {
  12. base.OnEnable();
  13. if (GameManager.Instance.IsRuning && m_ModelObj == null)
  14. {
  15. if (IsAB())
  16. LoadABModel();
  17. else
  18. TriLibModel();
  19. }
  20. }
  21. private IEnumerator LoadModel()
  22. {
  23. yield return new WaitForSeconds(0.01f);
  24. if (GameManager.Instance.IsRuning && m_ModelObj == null)
  25. {
  26. //Debug.Log(Data.name);
  27. Debug.Log("DGJ ==>> "+Data.localLoadPath);
  28. var ab = AssetBundle.LoadFromFileAsync(Data.localLoadPath);
  29. yield return ab;
  30. if (ab == null)
  31. {
  32. Debug.Log("Failed to load AssetBundle!");
  33. }
  34. else
  35. {
  36. var allab = ab.assetBundle.LoadAllAssetsAsync<GameObject>();
  37. yield return allab;
  38. var prefab = ab.assetBundle.LoadAssetAsync<GameObject>((allab.allAssets[0] as GameObject).name);
  39. yield return prefab;
  40. switch (prefab.asset.name)
  41. {
  42. case "GHZ_OOBE_230721":
  43. var objload = Resources.LoadAsync<GameObject>("GongYe");
  44. yield return objload;
  45. GameObject obj = null;
  46. if (objload.asset is GameObject)
  47. {
  48. obj = objload.asset as GameObject;
  49. }
  50. if (obj == null)
  51. {
  52. Debug.LogError(" GHZ_OOBE_230721 物体加载失败,请查看是否导入相关Packages ");
  53. obj = new GameObject();
  54. }
  55. m_ModelObj = GameObject.Instantiate(obj,transform);
  56. m_ModelObj.name = "GongYe";
  57. if (GameObject.Find("AudioManager") ==null)
  58. {
  59. GameObject audio = Resources.Load<GameObject>("AudioManager");
  60. if(audio ==null)
  61. {
  62. Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失");
  63. audio = new GameObject();
  64. }
  65. GameObject Audio = GameObject.Instantiate(audio, transform);
  66. Audio.name = "AudioManager";
  67. }
  68. break;
  69. case "GHZ_XXXXXX_230721":
  70. break;
  71. default:
  72. m_ModelObj = Instantiate(prefab.asset as GameObject, transform);
  73. break;
  74. }
  75. //if (ab.LoadAllAssets<GameObject>()[0].name == "MRVideo")
  76. //m_ModelObj.AddComponent<MovieScreen>();
  77. gameObject.AddComponent<ManipulationHandler>();
  78. gameObject.AddComponent<BoundingBox>();
  79. gameObject.AddComponent<Patch_ModelController>();
  80. yield return new WaitForSeconds(0.3f);
  81. ab.assetBundle.Unload(false);
  82. HideCollider();
  83. }
  84. //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath));
  85. //if (obj != null)
  86. //{
  87. // m_ModelObj = Instantiate(obj, transform);
  88. // m_ModelObj.transform.localPosition = Vector3.zero;
  89. // m_ModelObj.gameObject.SetActive(true);
  90. //}
  91. }
  92. }
  93. public void LoadModel(AssetBundle ab)
  94. {
  95. GameManager.Instance.StartCoroutine(LoadABModel(ab));
  96. //LoadABModel(ab);
  97. }
  98. private IEnumerator LoadABModel(AssetBundle ab)
  99. {
  100. if (ab == null)
  101. {
  102. Debug.Log("Failed to load AssetBundle!");
  103. }
  104. else
  105. {
  106. var allab = ab.LoadAllAssetsAsync<GameObject>();
  107. yield return allab;
  108. var prefab = ab.LoadAssetAsync<GameObject>((allab.allAssets[0] as GameObject).name);
  109. switch (prefab.asset.name)
  110. {
  111. case "GHZ_OOBE_230721":
  112. var objload = Resources.LoadAsync<GameObject>("GongYe");
  113. yield return objload;
  114. GameObject obj = null;
  115. if (objload.asset is GameObject)
  116. {
  117. obj = objload.asset as GameObject;
  118. }
  119. m_ModelObj = GameObject.Instantiate(obj, transform);
  120. m_ModelObj.name = "GongYe";
  121. if (GameObject.Find("AudioManager") == null)
  122. {
  123. GameObject audio = Resources.Load<GameObject>("AudioManager");
  124. if (audio == null)
  125. {
  126. Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失");
  127. audio = new GameObject();
  128. }
  129. GameObject Audio = GameObject.Instantiate(audio, null);
  130. Audio.name = "AudioManager";
  131. }
  132. break;
  133. case "GHZ_XXXXXX_230721":
  134. break;
  135. default:
  136. m_ModelObj = Instantiate(prefab.asset as GameObject, transform);
  137. break;
  138. }
  139. //if (ab.LoadAllAssets<GameObject>()[0].name == "MRVideo")
  140. //m_ModelObj.AddComponent<MovieScreen>();
  141. gameObject.AddComponent<ManipulationHandler>();
  142. gameObject.AddComponent<BoundingBox>();
  143. // yield return new WaitForSeconds(0.3f);
  144. ab.Unload(false);
  145. HideCollider();
  146. ModelItem.isLoad = false;
  147. }
  148. }
  149. protected override void OnAwake()
  150. {
  151. base.OnAwake();
  152. }
  153. public override void SetData(MaterialObjValue value, int updateTime)
  154. {
  155. base.SetData(value, updateTime);
  156. //if (gameObject.GetComponent<Collider>() != null)
  157. //{
  158. // Destroy(gameObject.GetComponent<Collider>());
  159. //}
  160. //下载列表中加入数据
  161. if (!GameManager.Instance.IsRuning|| Data == null)
  162. {
  163. // DownloadManager.Instance.AddDownloadData(Data);
  164. DownLoadMaterial data = new DownLoadMaterial();
  165. this.Data = data;
  166. data.downLoadPath = value.DownloadPath;
  167. data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  168. data.updataTime = GameManager.Instance.m_SceneValue.updateTime;
  169. data.type = "3";
  170. MsgHandler.AddListener(value.DownloadPath, HandleMsg);
  171. DownloadResManager.Instance.DownLoad(data);
  172. Debug.Log(" SetData " + updateTime);
  173. }
  174. else
  175. {
  176. if (IsAB())
  177. LoadABModel();
  178. else
  179. TriLibModel();
  180. //AssetBundle ab = AssetBundle.LoadFromFile(Data.localLoadPath);
  181. //if (ab == null)
  182. //{
  183. // Debug.Log("Failed to load AssetBundle!");
  184. // return;
  185. //}
  186. //var prefab = ab.LoadAsset<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
  187. //m_ModelObj = Instantiate(prefab, transform);
  188. //ab.Unload(false);
  189. //gameObject.AddComponent<ManipulationHandler>();
  190. //gameObject.AddComponent<BoundingBox>();
  191. //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath));
  192. //if (obj != null)
  193. //{
  194. // m_ModelObj = Instantiate(obj, transform);
  195. // m_ModelObj.transform.localPosition = Vector3.zero;
  196. // m_ModelObj.gameObject.SetActive(true);
  197. //}
  198. }
  199. }
  200. private void HandleMsg(Msg msg)
  201. {
  202. Debug.Log("DGJ TemplateModel ");
  203. if(msg.Value!=null)
  204. {
  205. if (IsAB())
  206. LoadABModel();
  207. else
  208. TriLibModel();
  209. }
  210. else
  211. {
  212. InstantiateCommand Command = new InstantiateCommand(
  213. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  214. InstantiateSystem.Instance.BlueObject.NetErrorText);
  215. CommandSystem.Instance.Send(Command);
  216. Debug.LogError(" 模型下载失败 ");
  217. }
  218. }
  219. private bool IsAB()
  220. {
  221. bool finish = false;
  222. string fileName = Path.GetFileName(Data.localLoadPath);
  223. Debug.Log("HJJ fileName===>"+ fileName);
  224. if(fileName.Contains(".ghb") ||!fileName.Contains("."))
  225. {
  226. finish = true;
  227. }
  228. return finish;
  229. }
  230. private void LoadABModel()
  231. {
  232. AssetBundle ab = AssetBundle.LoadFromFile(Data.localLoadPath);
  233. if (ab == null)
  234. {
  235. Debug.Log("Failed to load AssetBundle!");
  236. return;
  237. }
  238. var prefab = ab.LoadAsset<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
  239. m_ModelObj = Instantiate(prefab, transform);
  240. ab.Unload(false);
  241. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  242. manipulation.enabled = false;
  243. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  244. boundingBox.enabled = false;
  245. }
  246. public void TriLibModel()
  247. {
  248. Debug.Log("模型加载完成TriLibModel" + Data.localLoadPath);
  249. TriLibModelLoad.Load(Data.localLoadPath, (AssetLoaderContext ac) => {
  250. Debug.Log("模型加载完成");
  251. }, (AssetLoaderContext ac) => {
  252. Debug.Log("载材质加完成");
  253. // ac.RootGameObject.transform.position = Camera.main.transform.forward;
  254. m_ModelObj = Instantiate(ac.RootGameObject, transform);
  255. ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
  256. manipulation.enabled = false;
  257. BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
  258. boundingBox.enabled = false;
  259. ac.RootGameObject.SetActive(false);
  260. }, (AssetLoaderContext ac, float f) => {
  261. Debug.Log("加载中==》" + f);
  262. }, (IContextualizedError error) => {
  263. Debug.Log("加载失败" + error);
  264. });
  265. }
  266. public override void HideCollider()
  267. {
  268. base.HideCollider();
  269. }
  270. }