181 lines
5.0 KiB
C#
181 lines
5.0 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.AI;
|
|||
|
|
using UnityEngine.Events;
|
|||
|
|
using UnityEngine.SceneManagement;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
|
|||
|
|
public class SceneLoader : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public static SceneLoader Instance;
|
|||
|
|
|
|||
|
|
public List<Vector3> ztPositions;
|
|||
|
|
public List<Vector3> ztRotations;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ǰչ<C7B0><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static int currentIndex = -1;
|
|||
|
|
|
|||
|
|
public Slider slider; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
int currentProgress; // <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
|||
|
|
int targetProgress; // Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
public bool isAllLoaded;
|
|||
|
|
|
|||
|
|
void Awake()
|
|||
|
|
{
|
|||
|
|
Instance = this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Start()
|
|||
|
|
{
|
|||
|
|
currentProgress = 0;
|
|||
|
|
targetProgress = 0;
|
|||
|
|
|
|||
|
|
GameObject obj = GameObject.Find("BtnExLoader" + currentIndex);
|
|||
|
|
if (obj != null)
|
|||
|
|
{
|
|||
|
|
obj.GetComponent<Image>().color = Color.cyan;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
AsyncOperation asyncOperation;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>첽<EFBFBD><ECB2BD><EFBFBD>س<EFBFBD><D8B3><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public IEnumerator LoadScene(int index)
|
|||
|
|
{
|
|||
|
|
if (index != currentIndex)
|
|||
|
|
{
|
|||
|
|
if (isAllLoaded)
|
|||
|
|
{
|
|||
|
|
SetCameraPosition(index);
|
|||
|
|
AudioManager.Instance.PlayBg(index);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
slider.gameObject.SetActive(true);
|
|||
|
|
|
|||
|
|
asyncOperation = SceneManager.LoadSceneAsync("ZT" + index);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// <20>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> allowSceneActivation= falseʱ<65><CAB1><EFBFBD>Ῠ<EFBFBD><E1BFA8>0.89999<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ֵ
|
|||
|
|
asyncOperation.allowSceneActivation = false;
|
|||
|
|
// <20><><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>0.9f<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
|
|||
|
|
while (asyncOperation.progress < 0.9f)
|
|||
|
|
{
|
|||
|
|
targetProgress = (int)(asyncOperation.progress * 100);
|
|||
|
|
yield return LoadProgress();
|
|||
|
|
}
|
|||
|
|
// ѭ<><D1AD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD>Ϊ90<39>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD>100<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|||
|
|
targetProgress = 100;
|
|||
|
|
|
|||
|
|
yield return LoadProgress();
|
|||
|
|
|
|||
|
|
currentIndex = index;
|
|||
|
|
asyncOperation.allowSceneActivation = true;
|
|||
|
|
asyncOperation.completed += AsyncOperation_completed;
|
|||
|
|
slider.gameObject.SetActive(false);
|
|||
|
|
currentProgress = 0;
|
|||
|
|
targetProgress = 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void AsyncOperation_completed(AsyncOperation obj)
|
|||
|
|
{
|
|||
|
|
AudioManager.Instance.PlayBg(currentIndex);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetCameraPosition(int index)
|
|||
|
|
{
|
|||
|
|
NavMeshAgent agent = FindObjectOfType<NavMeshAgent>();
|
|||
|
|
if (agent != null)
|
|||
|
|
agent.enabled = false;
|
|||
|
|
|
|||
|
|
if (ztPositions.Count > index)
|
|||
|
|
{
|
|||
|
|
CommonData.MainCamera.transform.localPosition = ztPositions[index];
|
|||
|
|
}
|
|||
|
|
if (ztRotations.Count > index)
|
|||
|
|
{
|
|||
|
|
CommonData.MainCamera.transform.localRotation = Quaternion.Euler(ztRotations[index]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (agent != null)
|
|||
|
|
agent.enabled = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ShowPoint currentSPInfo;
|
|||
|
|
public void SetCameraPosition(ShowPoint info)
|
|||
|
|
{
|
|||
|
|
//if (currentIndex != info.ZT_Index)
|
|||
|
|
//{
|
|||
|
|
// currentIndex = info.ZT_Index;
|
|||
|
|
// AudioManager.Instance.PlayBg(currentIndex);
|
|||
|
|
//}
|
|||
|
|
currentSPInfo = info;
|
|||
|
|
LoadSceneData(info.ZT_Index, SetPostionOnLoaded );
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SetPostionOnLoaded()
|
|||
|
|
{
|
|||
|
|
SetCameraPosition(currentSPInfo.Position, currentSPInfo.Rotation);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void SetCameraPosition(Vector3 pos, Vector3 rot)
|
|||
|
|
{
|
|||
|
|
NavMeshAgent agent = FindObjectOfType<NavMeshAgent>();
|
|||
|
|
if (agent != null)
|
|||
|
|
agent.enabled = false;
|
|||
|
|
|
|||
|
|
CommonData.MainCamera.transform.localPosition = pos;
|
|||
|
|
CommonData.MainCamera.transform.localRotation = Quaternion.Euler(rot);
|
|||
|
|
|
|||
|
|
if (agent != null)
|
|||
|
|
agent.enabled = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ε<EFBFBD><CEB5>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<D0BC>װ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns><3E><>һ֡</returns>
|
|||
|
|
private IEnumerator LoadProgress()
|
|||
|
|
{
|
|||
|
|
yield return new WaitForEndOfFrame();
|
|||
|
|
// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD> < Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
|
while (currentProgress < targetProgress)
|
|||
|
|
{
|
|||
|
|
++currentProgress;
|
|||
|
|
slider.value = (float)currentProgress / 100;
|
|||
|
|
yield return new WaitForEndOfFrame();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void LoadSceneData(int newIndex, UnityAction onDataLoaded)
|
|||
|
|
{
|
|||
|
|
if (newIndex != SceneLoader.currentIndex)
|
|||
|
|
{
|
|||
|
|
SceneLoader.currentIndex = newIndex;
|
|||
|
|
LoadData(newIndex, onDataLoaded);
|
|||
|
|
AudioManager.Instance.PlayBg(newIndex);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
if (onDataLoaded != null)
|
|||
|
|
{
|
|||
|
|
onDataLoaded();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void LoadData(int index, UnityAction onDataLoaded)
|
|||
|
|
{
|
|||
|
|
StartCoroutine(DataLoader<Data>.LoadDataDictionary(string.Format("SpotsZT{0}.json", index), onDataLoaded));
|
|||
|
|
StartCoroutine(DataLoader<Data>.LoadDataList(string.Format("Data_ZT{0}.json", index)));
|
|||
|
|
}
|
|||
|
|
}
|