144 lines
4.0 KiB
C#
144 lines
4.0 KiB
C#
using Cysharp.Threading.Tasks;
|
|
|
|
using PMaker.DependencyInjection;
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using UnityEngine.ResourceManagement.ResourceProviders;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class SceneLoader : SingletonMonobehaviour
|
|
{
|
|
public LoadSceneMode loadSceneMode;
|
|
public AssetReference[] sceneReferences;
|
|
public SceneInstance sceneInstance;
|
|
public LightProbes lightProbes;
|
|
public LightmapData[] lightmapDatas;
|
|
public int last = -1;
|
|
#if UNITY_EDITOR
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
#endif
|
|
private Dictionary<int, AsyncOperationHandle> _cache;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
_cache = new Dictionary<int, AsyncOperationHandle>();
|
|
}
|
|
|
|
[Button]
|
|
[HideInEditorMode]
|
|
public async UniTask LoadScene(int index)
|
|
{
|
|
var key = last != index;
|
|
//var key = true;
|
|
if (key == true)
|
|
{
|
|
IoC.GetSingleton<Loading>().Show();
|
|
Canvas.ForceUpdateCanvases();
|
|
await UniTask.Delay(100);
|
|
}
|
|
if (key == true && last != -1)
|
|
{
|
|
LightmapSettings.lightProbes = null;
|
|
LightmapSettings.lightmaps = null;
|
|
await UnLoadScene(last);
|
|
last = -1;
|
|
}
|
|
if (_cache.TryGetValue(index, out var value))
|
|
{
|
|
sceneInstance = await value.Convert<SceneInstance>();
|
|
//SceneManager.SetActiveScene(sceneInstance.Scene);
|
|
}
|
|
else
|
|
{
|
|
var handle = sceneReferences[index].LoadSceneAsync(loadSceneMode, true);
|
|
_cache[index] = handle;
|
|
await handle;
|
|
sceneInstance = await handle;
|
|
//SceneManager.SetActiveScene(sceneInstance.Scene);
|
|
last = index;
|
|
}
|
|
await UniTask.WaitUntil(() => IoC.GetSingleton<SceneLoadMark>() == true);
|
|
SceneManager.SetActiveScene(sceneInstance.Scene);
|
|
lightProbes = LightmapSettings.lightProbes;
|
|
lightmapDatas = LightmapSettings.lightmaps;
|
|
|
|
if (key == true)
|
|
{
|
|
UniTask.Create(async () => {
|
|
await UniTask.Delay(100);
|
|
IoC.GetSingleton<Loading>().Hide();
|
|
}).Forget();
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
[HideInEditorMode]
|
|
public async UniTask ResetLoadScene(int index)
|
|
{
|
|
var key = true;
|
|
//var key = true;
|
|
if (key == true)
|
|
{
|
|
IoC.GetSingleton<Loading>().Show();
|
|
Canvas.ForceUpdateCanvases();
|
|
await UniTask.Delay(100);
|
|
}
|
|
if (key == true && last != -1)
|
|
{
|
|
await UnLoadScene(last);
|
|
last = -1;
|
|
}
|
|
if (_cache.TryGetValue(index, out var value))
|
|
{
|
|
await value;
|
|
}
|
|
else
|
|
{
|
|
var handle = sceneReferences[index].LoadSceneAsync(loadSceneMode, true);
|
|
_cache[index] = handle;
|
|
var sceneInstance = await handle;
|
|
SceneManager.SetActiveScene(sceneInstance.Scene);
|
|
last = index;
|
|
}
|
|
await UniTask.WaitUntil(() => IoC.GetSingleton<SceneLoadMark>() == true);
|
|
|
|
if (key == true)
|
|
{
|
|
UniTask.Create(async () => {
|
|
await UniTask.Delay(100);
|
|
IoC.GetSingleton<Loading>().Hide();
|
|
}).Forget();
|
|
}
|
|
}
|
|
|
|
|
|
[Button]
|
|
[HideInEditorMode]
|
|
public async UniTask UnLoadScene(int index)
|
|
{
|
|
if (IoC.GetSingleton<SceneLoadMark>() == true)
|
|
{
|
|
IoC.RemoveSingleton<SceneLoadMark>();
|
|
}
|
|
await sceneReferences[index].UnLoadScene();
|
|
//AssetBundle.Unload(true);
|
|
_cache.Remove(index);
|
|
last = -1;
|
|
}
|
|
|
|
[Button]
|
|
[HideInEditorMode]
|
|
public void LoadLight()
|
|
{
|
|
LightmapSettings.lightProbes = lightProbes;
|
|
LightmapSettings.lightmaps = lightmapDatas;
|
|
}
|
|
} |