using SC.XR.Unity.Module_InputSystem; using System.Collections; using System.Collections.Generic; using System.IO; using TriLibCore; using UnityEngine; public class TemplateModel : BaseTemPlate { private GameObject m_ModelObj = null; protected override void OnEnable() { base.OnEnable(); if (GameManager.Instance.IsRuning && m_ModelObj == null) { if (IsAB()) LoadABModel(); else TriLibModel(); } } private IEnumerator LoadModel() { yield return new WaitForSeconds(0.01f); if (GameManager.Instance.IsRuning && m_ModelObj == null) { //Debug.Log(Data.name); Debug.Log("DGJ ==>> "+Data.localLoadPath); var ab = AssetBundle.LoadFromFileAsync(Data.localLoadPath); yield return ab; if (ab == null) { Debug.Log("Failed to load AssetBundle!"); } else { var allab = ab.assetBundle.LoadAllAssetsAsync(); yield return allab; var prefab = ab.assetBundle.LoadAssetAsync((allab.allAssets[0] as GameObject).name); yield return prefab; switch (prefab.asset.name) { case "GHZ_OOBE_230721": var objload = Resources.LoadAsync("GongYe"); yield return objload; GameObject obj = null; if (objload.asset is GameObject) { obj = objload.asset as GameObject; } if (obj == null) { Debug.LogError(" GHZ_OOBE_230721 物体加载失败,请查看是否导入相关Packages "); obj = new GameObject(); } m_ModelObj = GameObject.Instantiate(obj,transform); m_ModelObj.name = "GongYe"; if (GameObject.Find("AudioManager") ==null) { GameObject audio = Resources.Load("AudioManager"); if(audio ==null) { Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失"); audio = new GameObject(); } GameObject Audio = GameObject.Instantiate(audio, transform); Audio.name = "AudioManager"; } break; case "GHZ_XXXXXX_230721": break; default: m_ModelObj = Instantiate(prefab.asset as GameObject, transform); break; } //if (ab.LoadAllAssets()[0].name == "MRVideo") //m_ModelObj.AddComponent(); gameObject.AddComponent(); gameObject.AddComponent(); gameObject.AddComponent(); yield return new WaitForSeconds(0.3f); ab.assetBundle.Unload(false); HideCollider(); } //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath)); //if (obj != null) //{ // m_ModelObj = Instantiate(obj, transform); // m_ModelObj.transform.localPosition = Vector3.zero; // m_ModelObj.gameObject.SetActive(true); //} } } public void LoadModel(AssetBundle ab) { GameManager.Instance.StartCoroutine(LoadABModel(ab)); //LoadABModel(ab); } private IEnumerator LoadABModel(AssetBundle ab) { if (ab == null) { Debug.Log("Failed to load AssetBundle!"); } else { var allab = ab.LoadAllAssetsAsync(); yield return allab; var prefab = ab.LoadAssetAsync((allab.allAssets[0] as GameObject).name); switch (prefab.asset.name) { case "GHZ_OOBE_230721": var objload = Resources.LoadAsync("GongYe"); yield return objload; GameObject obj = null; if (objload.asset is GameObject) { obj = objload.asset as GameObject; } m_ModelObj = GameObject.Instantiate(obj, transform); m_ModelObj.name = "GongYe"; if (GameObject.Find("AudioManager") == null) { GameObject audio = Resources.Load("AudioManager"); if (audio == null) { Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失"); audio = new GameObject(); } GameObject Audio = GameObject.Instantiate(audio, null); Audio.name = "AudioManager"; } break; case "GHZ_XXXXXX_230721": break; default: m_ModelObj = Instantiate(prefab.asset as GameObject, transform); break; } //if (ab.LoadAllAssets()[0].name == "MRVideo") //m_ModelObj.AddComponent(); gameObject.AddComponent(); gameObject.AddComponent(); // yield return new WaitForSeconds(0.3f); ab.Unload(false); HideCollider(); ModelItem.isLoad = false; } } protected override void OnAwake() { base.OnAwake(); } public override void SetData(MaterialObjValue value, int updateTime) { base.SetData(value, updateTime); //if (gameObject.GetComponent() != null) //{ // Destroy(gameObject.GetComponent()); //} //下载列表中加入数据 if (!GameManager.Instance.IsRuning|| Data == null) { // DownloadManager.Instance.AddDownloadData(Data); DownLoadMaterial data = new DownLoadMaterial(); this.Data = data; data.downLoadPath = value.DownloadPath; data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath); data.updataTime = GameManager.Instance.m_SceneValue.updateTime; data.type = "3"; MsgHandler.AddListener(value.DownloadPath, HandleMsg); DownloadResManager.Instance.DownLoad(data); Debug.Log(" SetData " + updateTime); } else { if (IsAB()) LoadABModel(); else TriLibModel(); //AssetBundle ab = AssetBundle.LoadFromFile(Data.localLoadPath); //if (ab == null) //{ // Debug.Log("Failed to load AssetBundle!"); // return; //} //var prefab = ab.LoadAsset(ab.LoadAllAssets()[0].name); //m_ModelObj = Instantiate(prefab, transform); //ab.Unload(false); //gameObject.AddComponent(); //gameObject.AddComponent(); //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath)); //if (obj != null) //{ // m_ModelObj = Instantiate(obj, transform); // m_ModelObj.transform.localPosition = Vector3.zero; // m_ModelObj.gameObject.SetActive(true); //} } } private void HandleMsg(Msg msg) { Debug.Log("DGJ TemplateModel "); if(msg.Value!=null) { if (IsAB()) LoadABModel(); else TriLibModel(); } else { InstantiateCommand Command = new InstantiateCommand( InstantiateSystem.Instance.BlueObject.WarningPopUp, InstantiateSystem.Instance.BlueObject.NetErrorText); CommandSystem.Instance.Send(Command); Debug.LogError(" 模型下载失败 "); } } private bool IsAB() { bool finish = false; string fileName = Path.GetFileName(Data.localLoadPath); Debug.Log("HJJ fileName===>"+ fileName); if(fileName.Contains(".ghb") ||!fileName.Contains(".")) { finish = true; } return finish; } private void LoadABModel() { AssetBundle ab = AssetBundle.LoadFromFile(Data.localLoadPath); if (ab == null) { Debug.Log("Failed to load AssetBundle!"); return; } var prefab = ab.LoadAsset(ab.LoadAllAssets()[0].name); m_ModelObj = Instantiate(prefab, transform); ab.Unload(false); ManipulationHandler manipulation = gameObject.AddComponent(); manipulation.enabled = false; BoundingBox boundingBox = gameObject.AddComponent(); boundingBox.enabled = false; } public void TriLibModel() { Debug.Log("模型加载完成TriLibModel" + Data.localLoadPath); TriLibModelLoad.Load(Data.localLoadPath, (AssetLoaderContext ac) => { Debug.Log("模型加载完成"); }, (AssetLoaderContext ac) => { Debug.Log("载材质加完成"); // ac.RootGameObject.transform.position = Camera.main.transform.forward; m_ModelObj = Instantiate(ac.RootGameObject, transform); ManipulationHandler manipulation = gameObject.AddComponent(); manipulation.enabled = false; BoundingBox boundingBox = gameObject.AddComponent(); boundingBox.enabled = false; ac.RootGameObject.SetActive(false); }, (AssetLoaderContext ac, float f) => { Debug.Log("加载中==》" + f); }, (IContextualizedError error) => { Debug.Log("加载失败" + error); }); } public override void HideCollider() { base.HideCollider(); } }