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(); if(timeObj != null) { CDebug.Log("TimelinePlayable 找到 同步播放数据"); timeObj.InitPlayableData(objData as ItemClickData); } GameBaseController gameControlObj = curObj.GetComponentInChildren(); 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); */ } }