AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/Manager/AppSceneLoader.cs

90 lines
2.5 KiB
C#
Raw Normal View History

2024-08-29 18:31:36 +08:00
using AxibugEmuOnline.Client.ClientCore;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace AxibugEmuOnline.Client
{
public class AppSceneLoader
{
public LoadTask CurrentTask { get; private set; }
2024-09-11 16:33:48 +08:00
2024-08-29 18:31:36 +08:00
Queue<LoadTask> m_tasks = new Queue<LoadTask>();
Coroutine m_coroutine;
public void BeginLoad(string scenePath, Action callback)
{
m_tasks.Enqueue(new LoadTask(scenePath, callback));
if (m_coroutine == null)
2024-09-11 18:31:25 +08:00
m_coroutine = App.StartCoroutine(TaskFlow());
2024-08-29 18:31:36 +08:00
}
private IEnumerator TaskFlow()
{
while (m_tasks.Count > 0)
{
CurrentTask = m_tasks.Dequeue();
while (CurrentTask.MoveNext()) yield return null;
CurrentTask.Callback();
}
CurrentTask = null;
m_coroutine = null;
}
public class LoadTask
{
public EnumTaskState State { get; private set; } = EnumTaskState.Idle;
public float Progress => m_loadingOp != null ? m_loadingOp.progress : 0;
private AsyncOperation m_loadingOp;
private string m_scenePath;
private Action m_callback;
public LoadTask(string scenePath, Action callback)
{
m_scenePath = scenePath;
m_callback = callback;
}
public bool MoveNext()
{
if (State == EnumTaskState.Idle)
{
State = EnumTaskState.Running;
2024-08-30 16:23:27 +08:00
m_loadingOp = SceneManager.LoadSceneAsync(m_scenePath, LoadSceneMode.Single);
2024-08-29 18:31:36 +08:00
return true;
}
else if (State == EnumTaskState.Running)
{
m_loadingOp.allowSceneActivation = true;
if (m_loadingOp.isDone)
{
State = EnumTaskState.Complete;
}
2024-09-11 16:33:48 +08:00
2024-08-29 18:31:36 +08:00
return true;
}
else
{
return false;
}
}
public void Callback()
{
m_callback?.Invoke();
}
}
public enum EnumTaskState
{
Idle,
Running,
Complete
}
}
}