123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- 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<GameObject>();
- yield return allab;
- var prefab = ab.assetBundle.LoadAssetAsync<GameObject>((allab.allAssets[0] as GameObject).name);
- yield return prefab;
- switch (prefab.asset.name)
- {
- case "GHZ_OOBE_230721":
- var objload = Resources.LoadAsync<GameObject>("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<GameObject>("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<GameObject>()[0].name == "MRVideo")
- //m_ModelObj.AddComponent<MovieScreen>();
- gameObject.AddComponent<ManipulationHandler>();
- gameObject.AddComponent<BoundingBox>();
- gameObject.AddComponent<Patch_ModelController>();
- 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<GameObject>();
- yield return allab;
- var prefab = ab.LoadAssetAsync<GameObject>((allab.allAssets[0] as GameObject).name);
- switch (prefab.asset.name)
- {
- case "GHZ_OOBE_230721":
- var objload = Resources.LoadAsync<GameObject>("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<GameObject>("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<GameObject>()[0].name == "MRVideo")
- //m_ModelObj.AddComponent<MovieScreen>();
- gameObject.AddComponent<ManipulationHandler>();
- gameObject.AddComponent<BoundingBox>();
- // 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<Collider>() != null)
- //{
- // Destroy(gameObject.GetComponent<Collider>());
- //}
- //下载列表中加入数据
- 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<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
- //m_ModelObj = Instantiate(prefab, transform);
- //ab.Unload(false);
- //gameObject.AddComponent<ManipulationHandler>();
- //gameObject.AddComponent<BoundingBox>();
- //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<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
- m_ModelObj = Instantiate(prefab, transform);
- ab.Unload(false);
- ManipulationHandler manipulation = gameObject.AddComponent<ManipulationHandler>();
- manipulation.enabled = false;
- BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
- 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<ManipulationHandler>();
- manipulation.enabled = false;
- BoundingBox boundingBox = gameObject.AddComponent<BoundingBox>();
- 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();
- }
- }
|