feat : add load scene sync method

pull/252/head
hevinci 2024-03-08 11:06:48 +08:00
parent c01adad2a0
commit 81747462b1
4 changed files with 119 additions and 38 deletions

View File

@ -37,6 +37,16 @@ namespace YooAsset
} }
} }
/// <summary>
/// 等待异步执行完毕
/// </summary>
internal void WaitForAsyncComplete()
{
if (IsValidWithWarning == false)
return;
Provider.WaitForAsyncComplete();
}
/// <summary> /// <summary>
/// 场景名称 /// 场景名称
/// </summary> /// </summary>
@ -171,5 +181,7 @@ namespace YooAsset
return operation; return operation;
} }
} }
} }
} }

View File

@ -60,7 +60,15 @@ namespace YooAsset
// 2. 加载场景 // 2. 加载场景
if (_steps == ESteps.Loading) if (_steps == ESteps.Loading)
{ {
// 注意如果场景不存在则返回NULL if (IsWaitForAsyncComplete || IsForceDestroyComplete)
{
LoadSceneParameters parameters = new LoadSceneParameters(SceneMode);
SceneObject = SceneManager.LoadScene(MainAssetInfo.AssetPath, parameters);
}
else
{
// 注意如果场景不存在异步加载方法返回NULL
// 注意:即使是异步加载也要在当帧获取到场景对象
_asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode); _asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
if (_asyncOperation != null) if (_asyncOperation != null)
{ {
@ -76,26 +84,38 @@ namespace YooAsset
InvokeCompletion(error, EOperationStatus.Failed); InvokeCompletion(error, EOperationStatus.Failed);
} }
} }
}
// 3. 检测加载结果 // 3. 检测加载结果
if (_steps == ESteps.Checking) if (_steps == ESteps.Checking)
{ {
Progress = _asyncOperation.progress; if (_asyncOperation != null)
if (_asyncOperation.isDone)
{ {
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
{
// 场景加载无法强制异步转同步
YooLogger.Error("The scene is loading asyn !");
}
else
{
Progress = _asyncOperation.progress;
if (_asyncOperation.isDone == false)
return;
}
}
if (SceneObject.IsValid()) if (SceneObject.IsValid())
{ {
InvokeCompletion(string.Empty, EOperationStatus.Succeed); InvokeCompletion(string.Empty, EOperationStatus.Succeed);
} }
else else
{ {
string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}"; string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
YooLogger.Error(error); YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed); InvokeCompletion(error, EOperationStatus.Failed);
} }
} }
} }
}
/// <summary> /// <summary>
/// 解除场景加载挂起操作 /// 解除场景加载挂起操作

View File

@ -57,8 +57,14 @@ namespace YooAsset
// 2. 加载资源对象 // 2. 加载资源对象
if (_steps == ESteps.Loading) if (_steps == ESteps.Loading)
{ {
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(); if (IsWaitForAsyncComplete || IsForceDestroyComplete)
loadSceneParameters.loadSceneMode = SceneMode; {
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(SceneMode);
SceneObject = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
}
else
{
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(SceneMode);
_asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters); _asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
if (_asyncOperation != null) if (_asyncOperation != null)
{ {
@ -74,13 +80,26 @@ namespace YooAsset
InvokeCompletion(error, EOperationStatus.Failed); InvokeCompletion(error, EOperationStatus.Failed);
} }
} }
}
// 3. 检测加载结果 // 3. 检测加载结果
if (_steps == ESteps.Checking) if (_steps == ESteps.Checking)
{ {
Progress = _asyncOperation.progress; if (_asyncOperation != null)
if (_asyncOperation.isDone)
{ {
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
{
// 场景加载无法强制异步转同步
YooLogger.Error("The scene is loading asyn !");
}
else
{
Progress = _asyncOperation.progress;
if (_asyncOperation.isDone == false)
return;
}
}
if (SceneObject.IsValid()) if (SceneObject.IsValid())
{ {
InvokeCompletion(string.Empty, EOperationStatus.Succeed); InvokeCompletion(string.Empty, EOperationStatus.Succeed);
@ -92,7 +111,6 @@ namespace YooAsset
InvokeCompletion(error, EOperationStatus.Failed); InvokeCompletion(error, EOperationStatus.Failed);
} }
} }
}
#endif #endif
} }

View File

@ -602,6 +602,29 @@ namespace YooAsset
#endregion #endregion
#region 场景加载 #region 场景加载
/// <summary>
/// 同步加载场景
/// </summary>
/// <param name="location">场景的定位地址</param>
/// <param name="sceneMode">场景加载模式</param>
public SceneHandle LoadSceneSync(string location, LoadSceneMode sceneMode = LoadSceneMode.Single)
{
DebugCheckInitialize();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
return LoadSceneInternal(assetInfo, true, sceneMode, false, 0);
}
/// <summary>
/// 同步加载场景
/// </summary>
/// <param name="assetInfo">场景的资源信息</param>
/// <param name="sceneMode">场景加载模式</param>
public SceneHandle LoadSceneSync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single)
{
DebugCheckInitialize();
return LoadSceneInternal(assetInfo, true, sceneMode, false, 0);
}
/// <summary> /// <summary>
/// 异步加载场景 /// 异步加载场景
/// </summary> /// </summary>
@ -613,8 +636,7 @@ namespace YooAsset
{ {
DebugCheckInitialize(); DebugCheckInitialize();
AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null); AssetInfo assetInfo = ConvertLocationToAssetInfo(location, null);
var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); return LoadSceneInternal(assetInfo, false, sceneMode, suspendLoad, priority);
return handle;
} }
/// <summary> /// <summary>
@ -627,7 +649,16 @@ namespace YooAsset
public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0) public SceneHandle LoadSceneAsync(AssetInfo assetInfo, LoadSceneMode sceneMode = LoadSceneMode.Single, bool suspendLoad = false, uint priority = 0)
{ {
DebugCheckInitialize(); DebugCheckInitialize();
return LoadSceneInternal(assetInfo, false, sceneMode, suspendLoad, priority);
}
private SceneHandle LoadSceneInternal(AssetInfo assetInfo, bool waitForAsyncComplete, LoadSceneMode sceneMode, bool suspendLoad, uint priority)
{
DebugCheckAssetLoadMethod(nameof(LoadAssetAsync));
DebugCheckAssetLoadType(assetInfo.AssetType);
var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority); var handle = _resourceMgr.LoadSceneAsync(assetInfo, sceneMode, suspendLoad, priority);
if (waitForAsyncComplete)
handle.WaitForAsyncComplete();
return handle; return handle;
} }
#endregion #endregion