LoadPrefabManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //这个类是用来加载视频和MR的
  5. public class LoadPrefabManager : MonoBehaviour {
  6. public static LoadPrefabManager Instance;
  7. [SerializeField]
  8. private Transform WorldParent;
  9. [SerializeField]
  10. private GameObject LoadingObj;//加载
  11. private GameObject curObj;//当前放置在场景里的预制件
  12. private void Awake()
  13. {
  14. Instance = this;
  15. }
  16. void Start () {
  17. if (LoadingObj != null)
  18. LoadingObj.gameObject.SetActive(false);
  19. }
  20. // Update is called once per frame
  21. void Update () {
  22. RefreshLoading();
  23. }
  24. private void RefreshLoading()
  25. {
  26. if(Time.realtimeSinceStartup < startLoadingTime + minLoadigTime)
  27. {
  28. if (LoadingObj != null && !LoadingObj.activeSelf)
  29. {
  30. LoadingObj.gameObject.SetActive(true);
  31. }
  32. }
  33. else
  34. {
  35. if (LoadingObj != null && LoadingObj.activeSelf)
  36. {
  37. LoadingObj.gameObject.SetActive(false);
  38. }
  39. if(!isLoading && curObj != null && !curObj.gameObject.activeSelf)
  40. {
  41. curObj.gameObject.SetActive(true);
  42. //同步播放数据
  43. TimelinePlayable timeObj = curObj.GetComponentInChildren<TimelinePlayable>();
  44. if(timeObj != null)
  45. {
  46. CDebug.Log("TimelinePlayable 找到 同步播放数据");
  47. timeObj.InitPlayableData(objData as ItemClickData);
  48. }
  49. GameBaseController gameControlObj = curObj.GetComponentInChildren<GameBaseController>();
  50. if (gameControlObj != null)
  51. {
  52. gameControlObj.InitData(objData as MRData);
  53. }
  54. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_PREFAB_LOAD_FINISH_SHOW, objData);//加载完成 显示UI
  55. }
  56. }
  57. }
  58. public void ResetObj()
  59. {
  60. if (curObj != null)
  61. {
  62. GameObject.Destroy(curObj);//直接删除
  63. }
  64. }
  65. private System.Object objData;
  66. public void LoadPrefab(ItemClickData data)
  67. {
  68. if(isLoading)
  69. {
  70. return;
  71. }
  72. /*
  73. if (LoadingObj != null)
  74. LoadingObj.gameObject.SetActive(true);
  75. */
  76. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_COURSE);
  77. objData = data;
  78. //float test_time = Time.realtimeSinceStartup;
  79. StartCoroutine(AddPrefab(GetItemClickDataStr(data)));
  80. //CDebug.Log("测试时间" + (Time.realtimeSinceStartup - test_time));
  81. }
  82. public void LoadPrefab(MRData data)
  83. {
  84. if (isLoading)
  85. {
  86. return;
  87. }
  88. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_COURSE);
  89. objData = data;
  90. StartCoroutine(AddPrefab(GetMRDataStr(data.mrName)));
  91. }
  92. private bool isLoading = false;
  93. private float startLoadingTime = float.MinValue;//开始加载的时间点
  94. private float minLoadigTime = 4.5f;//最低的加载时间 在这个时间内播放加载的动画
  95. private string GetItemClickDataStr(ItemClickData data)
  96. {
  97. string format_str = GameEnum.ResourcePath + "{0}/{1}_{2}_{3}_{4}";
  98. return string.Format(format_str, data.firstId, data.firstId, data.secondId, data.thirdId, data.forthId + 1);
  99. }
  100. private string GetMRDataStr(string data)
  101. {
  102. string format_str = "Biology/" + "{0}";
  103. return string.Format(format_str, data);
  104. }
  105. IEnumerator AddPrefab(string obj_path)
  106. {
  107. yield return null;
  108. isLoading = true;
  109. if (curObj != null && curObj.name.Equals(obj_path))
  110. {
  111. }
  112. else
  113. {
  114. if (LoadingObj != null && LoadingObj.activeSelf)
  115. {
  116. LoadingObj.gameObject.SetActive(false);
  117. }
  118. startLoadingTime = Time.realtimeSinceStartup;
  119. if (curObj != null)
  120. {
  121. GameObject.Destroy(curObj);//直接删除
  122. }
  123. float start_time = Time.realtimeSinceStartup;
  124. Object res_obj = Resources.Load(obj_path);
  125. if (res_obj == null)
  126. {
  127. Debug.Log("资源不存在" + obj_path);
  128. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_MSG_POP, obj_path + "资源不存在");
  129. MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_PREFAB_LOAD_FINISH_SHOW, objData);//放弃加载 显示UI
  130. }
  131. else
  132. {
  133. Debug.Log("加载资源的时间 " + (Time.realtimeSinceStartup - start_time) + " path " + obj_path);
  134. start_time = Time.realtimeSinceStartup;
  135. curObj = GameObject.Instantiate(res_obj) as GameObject;
  136. curObj.transform.parent = WorldParent;
  137. curObj.transform.localEulerAngles = Vector3.one;//不允许有默认的位置
  138. curObj.transform.localPosition = Vector3.zero;//不允许有默认的位置
  139. curObj.name = obj_path;
  140. curObj.gameObject.SetActive(false);
  141. Debug.Log("实例化对象的时间 " + (Time.realtimeSinceStartup - start_time));
  142. }
  143. }
  144. isLoading = false;
  145. /*
  146. if(LoadingObj != null)
  147. LoadingObj.gameObject.SetActive(false);
  148. */
  149. }
  150. }