123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- //这个类是用来加载视频和MR的
- public class LoadPrefabManager : MonoBehaviour {
- public static LoadPrefabManager Instance;
- [SerializeField]
- private Transform WorldParent;
- [SerializeField]
- private GameObject LoadingObj;//加载
- private GameObject curObj;//当前放置在场景里的预制件
- private void Awake()
- {
- Instance = this;
- }
- void Start () {
- if (LoadingObj != null)
- LoadingObj.gameObject.SetActive(false);
- }
-
- // Update is called once per frame
- void Update () {
- RefreshLoading();
- }
- private void RefreshLoading()
- {
- if(Time.realtimeSinceStartup < startLoadingTime + minLoadigTime)
- {
- if (LoadingObj != null && !LoadingObj.activeSelf)
- {
- LoadingObj.gameObject.SetActive(true);
- }
- }
- else
- {
- if (LoadingObj != null && LoadingObj.activeSelf)
- {
- LoadingObj.gameObject.SetActive(false);
- }
- if(!isLoading && curObj != null && !curObj.gameObject.activeSelf)
- {
- curObj.gameObject.SetActive(true);
- //同步播放数据
- TimelinePlayable timeObj = curObj.GetComponentInChildren<TimelinePlayable>();
- if(timeObj != null)
- {
- CDebug.Log("TimelinePlayable 找到 同步播放数据");
- timeObj.InitPlayableData(objData as ItemClickData);
- }
- GameBaseController gameControlObj = curObj.GetComponentInChildren<GameBaseController>();
- if (gameControlObj != null)
- {
- gameControlObj.InitData(objData as MRData);
- }
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_PREFAB_LOAD_FINISH_SHOW, objData);//加载完成 显示UI
- }
- }
- }
- public void ResetObj()
- {
- if (curObj != null)
- {
- GameObject.Destroy(curObj);//直接删除
- }
- }
- private System.Object objData;
- public void LoadPrefab(ItemClickData data)
- {
- if(isLoading)
- {
- return;
- }
- /*
- if (LoadingObj != null)
- LoadingObj.gameObject.SetActive(true);
- */
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_COURSE);
- objData = data;
- //float test_time = Time.realtimeSinceStartup;
- StartCoroutine(AddPrefab(GetItemClickDataStr(data)));
- //CDebug.Log("测试时间" + (Time.realtimeSinceStartup - test_time));
- }
- public void LoadPrefab(MRData data)
- {
- if (isLoading)
- {
- return;
- }
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_HIDE_COURSE);
- objData = data;
- StartCoroutine(AddPrefab(GetMRDataStr(data.mrName)));
- }
- private bool isLoading = false;
- private float startLoadingTime = float.MinValue;//开始加载的时间点
- private float minLoadigTime = 4.5f;//最低的加载时间 在这个时间内播放加载的动画
- private string GetItemClickDataStr(ItemClickData data)
- {
- string format_str = GameEnum.ResourcePath + "{0}/{1}_{2}_{3}_{4}";
- return string.Format(format_str, data.firstId, data.firstId, data.secondId, data.thirdId, data.forthId + 1);
- }
- private string GetMRDataStr(string data)
- {
- string format_str = "Biology/" + "{0}";
- return string.Format(format_str, data);
- }
- IEnumerator AddPrefab(string obj_path)
- {
- yield return null;
- isLoading = true;
- if (curObj != null && curObj.name.Equals(obj_path))
- {
- }
- else
- {
- if (LoadingObj != null && LoadingObj.activeSelf)
- {
- LoadingObj.gameObject.SetActive(false);
- }
- startLoadingTime = Time.realtimeSinceStartup;
- if (curObj != null)
- {
- GameObject.Destroy(curObj);//直接删除
- }
- float start_time = Time.realtimeSinceStartup;
- Object res_obj = Resources.Load(obj_path);
- if (res_obj == null)
- {
- Debug.Log("资源不存在" + obj_path);
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_SHOW_MSG_POP, obj_path + "资源不存在");
- MessageCenterController.Instance.Broadcast(GameEnum.MESSAGE_PREFAB_LOAD_FINISH_SHOW, objData);//放弃加载 显示UI
- }
- else
- {
- Debug.Log("加载资源的时间 " + (Time.realtimeSinceStartup - start_time) + " path " + obj_path);
- start_time = Time.realtimeSinceStartup;
- curObj = GameObject.Instantiate(res_obj) as GameObject;
- curObj.transform.parent = WorldParent;
- curObj.transform.localEulerAngles = Vector3.one;//不允许有默认的位置
- curObj.transform.localPosition = Vector3.zero;//不允许有默认的位置
- curObj.name = obj_path;
- curObj.gameObject.SetActive(false);
- Debug.Log("实例化对象的时间 " + (Time.realtimeSinceStartup - start_time));
- }
- }
- isLoading = false;
- /*
- if(LoadingObj != null)
- LoadingObj.gameObject.SetActive(false);
- */
- }
- }
|