Files
TaiWan/Assets/Roaming/Scripts/Loader/SceneLoader.cs
2025-10-31 15:20:38 +08:00

181 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>
/// 当前展厅索引
/// </summary>
public static int currentIndex = -1;
public Slider slider; // 滑动条
int currentProgress; // 当前进度
int targetProgress; // 目标进度
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>
/// 异步加载场景
/// </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);
// 不允许场景立即激活
// 异步进度在 allowSceneActivation= false时会卡在0.89999的一个值
asyncOperation.allowSceneActivation = false;
// 当异步加载小于0.9f的时候
while (asyncOperation.progress < 0.9f)
{
targetProgress = (int)(asyncOperation.progress * 100);
yield return LoadProgress();
}
// 循环后当前进度已经为90了所以需要设置目标进度到100继续循环
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>
/// 由于需要两次调用,在这里进行简单封装
/// </summary>
/// <returns>等一帧</returns>
private IEnumerator LoadProgress()
{
yield return new WaitForEndOfFrame();
// 当前进度 < 目标进度时
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)));
}
}