TemplateModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. public class TemplateModel : BaseTemPlate
  7. {
  8. private GameObject m_ModelObj = null;
  9. protected override void OnEnable()
  10. {
  11. base.OnEnable();
  12. StartCoroutine(LoadModel());
  13. }
  14. private IEnumerator LoadModel()
  15. {
  16. yield return new WaitForSeconds(0.01f);
  17. if (GameManager.Instance.IsRuning && m_ModelObj == null)
  18. {
  19. Debug.Log(Data.name);
  20. Debug.Log(Data.localSavePath);
  21. AssetBundle ab = AssetBundle.LoadFromFile(Data.localSavePath);
  22. if (ab == null)
  23. {
  24. Debug.Log("Failed to load AssetBundle!");
  25. }
  26. else
  27. {
  28. var prefab = ab.LoadAsset<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
  29. switch (prefab.name)
  30. {
  31. case "GHZ_OOBE_230721":
  32. GameObject obj = Resources.Load<GameObject>("GongYe");
  33. if(obj == null)
  34. {
  35. Debug.LogError(" GHZ_OOBE_230721 物体加载失败,请查看是否导入相关Packages ");
  36. obj = new GameObject();
  37. }
  38. m_ModelObj = GameObject.Instantiate(obj,transform);
  39. m_ModelObj.name = "GongYe";
  40. if (GameObject.Find("AudioManager") ==null)
  41. {
  42. GameObject audio = Resources.Load<GameObject>("AudioManager");
  43. if(audio ==null)
  44. {
  45. Debug.LogError(" GHZ_OOBE_230721 Packages内AudioManager组件丢失");
  46. audio = new GameObject();
  47. }
  48. GameObject Audio = GameObject.Instantiate(audio, transform);
  49. Audio.name = "AudioManager";
  50. }
  51. break;
  52. case "GHZ_XXXXXX_230721":
  53. break;
  54. default:
  55. m_ModelObj = Instantiate(prefab, transform);
  56. break;
  57. }
  58. //if (ab.LoadAllAssets<GameObject>()[0].name == "MRVideo")
  59. //m_ModelObj.AddComponent<MovieScreen>();
  60. gameObject.AddComponent<ManipulationHandler>();
  61. gameObject.AddComponent<BoundingBox>();
  62. gameObject.AddComponent<Patch_ModelController>();
  63. yield return new WaitForSeconds(0.3f);
  64. ab.Unload(false);
  65. HideCollider();
  66. }
  67. //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath));
  68. //if (obj != null)
  69. //{
  70. // m_ModelObj = Instantiate(obj, transform);
  71. // m_ModelObj.transform.localPosition = Vector3.zero;
  72. // m_ModelObj.gameObject.SetActive(true);
  73. //}
  74. }
  75. }
  76. protected override void OnAwake()
  77. {
  78. base.OnAwake();
  79. }
  80. public override void SetData(MaterialObjValue value, int updateTime)
  81. {
  82. base.SetData(value, updateTime);
  83. //if (gameObject.GetComponent<Collider>() != null)
  84. //{
  85. // Destroy(gameObject.GetComponent<Collider>());
  86. //}
  87. //下载列表中加入数据
  88. if (!GameManager.Instance.IsRuning)
  89. {
  90. DownloadManager.Instance.AddDownloadData(Data);
  91. Debug.Log(" SetData " + updateTime);
  92. }
  93. else
  94. {
  95. AssetBundle ab = AssetBundle.LoadFromFile(Data.localSavePath);
  96. if (ab == null)
  97. {
  98. Debug.Log("Failed to load AssetBundle!");
  99. return;
  100. }
  101. var prefab = ab.LoadAsset<GameObject>(ab.LoadAllAssets<GameObject>()[0].name);
  102. m_ModelObj = Instantiate(prefab, transform);
  103. ab.Unload(false);
  104. gameObject.AddComponent<ManipulationHandler>();
  105. gameObject.AddComponent<BoundingBox>();
  106. //var obj = DownloadManager.Instance.GetAbObj(Util.MD5Encrypt(Data.downloadPath));
  107. //if (obj != null)
  108. //{
  109. // m_ModelObj = Instantiate(obj, transform);
  110. // m_ModelObj.transform.localPosition = Vector3.zero;
  111. // m_ModelObj.gameObject.SetActive(true);
  112. //}
  113. }
  114. }
  115. public override void HideCollider()
  116. {
  117. base.HideCollider();
  118. }
  119. }