TemplateModel.cs 11 KB

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