mirror of https://github.com/tuyoogame/YooAsset
feat : add load scene sync method
parent
c01adad2a0
commit
81747462b1
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -60,40 +60,60 @@ namespace YooAsset
|
||||||
// 2. 加载场景
|
// 2. 加载场景
|
||||||
if (_steps == ESteps.Loading)
|
if (_steps == ESteps.Loading)
|
||||||
{
|
{
|
||||||
// 注意:如果场景不存在则返回NULL
|
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||||
_asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
|
|
||||||
if (_asyncOperation != null)
|
|
||||||
{
|
{
|
||||||
_asyncOperation.allowSceneActivation = !_suspendLoad;
|
LoadSceneParameters parameters = new LoadSceneParameters(SceneMode);
|
||||||
_asyncOperation.priority = 100;
|
SceneObject = SceneManager.LoadScene(MainAssetInfo.AssetPath, parameters);
|
||||||
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
|
||||||
_steps = ESteps.Checking;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
|
// 注意:如果场景不存在异步加载方法返回NULL
|
||||||
YooLogger.Error(error);
|
// 注意:即使是异步加载也要在当帧获取到场景对象
|
||||||
InvokeCompletion(error, EOperationStatus.Failed);
|
_asyncOperation = SceneManager.LoadSceneAsync(MainAssetInfo.AssetPath, SceneMode);
|
||||||
|
if (_asyncOperation != null)
|
||||||
|
{
|
||||||
|
_asyncOperation.allowSceneActivation = !_suspendLoad;
|
||||||
|
_asyncOperation.priority = 100;
|
||||||
|
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||||
|
_steps = ESteps.Checking;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
|
||||||
|
YooLogger.Error(error);
|
||||||
|
InvokeCompletion(error, EOperationStatus.Failed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 检测加载结果
|
// 3. 检测加载结果
|
||||||
if (_steps == ESteps.Checking)
|
if (_steps == ESteps.Checking)
|
||||||
{
|
{
|
||||||
Progress = _asyncOperation.progress;
|
if (_asyncOperation != null)
|
||||||
if (_asyncOperation.isDone)
|
|
||||||
{
|
{
|
||||||
if (SceneObject.IsValid())
|
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||||
{
|
{
|
||||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
// 场景加载无法强制异步转同步
|
||||||
|
YooLogger.Error("The scene is loading asyn !");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string error = $"The load scene is invalid : {MainAssetInfo.AssetPath}";
|
Progress = _asyncOperation.progress;
|
||||||
YooLogger.Error(error);
|
if (_asyncOperation.isDone == false)
|
||||||
InvokeCompletion(error, EOperationStatus.Failed);
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SceneObject.IsValid())
|
||||||
|
{
|
||||||
|
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
|
||||||
|
YooLogger.Error(error);
|
||||||
|
InvokeCompletion(error, EOperationStatus.Failed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,41 +57,59 @@ namespace YooAsset
|
||||||
// 2. 加载资源对象
|
// 2. 加载资源对象
|
||||||
if (_steps == ESteps.Loading)
|
if (_steps == ESteps.Loading)
|
||||||
{
|
{
|
||||||
LoadSceneParameters loadSceneParameters = new LoadSceneParameters();
|
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||||
loadSceneParameters.loadSceneMode = SceneMode;
|
|
||||||
_asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
|
|
||||||
if (_asyncOperation != null)
|
|
||||||
{
|
{
|
||||||
_asyncOperation.allowSceneActivation = !_suspendLoad;
|
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(SceneMode);
|
||||||
_asyncOperation.priority = 100;
|
SceneObject = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
|
||||||
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
|
||||||
_steps = ESteps.Checking;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
|
LoadSceneParameters loadSceneParameters = new LoadSceneParameters(SceneMode);
|
||||||
YooLogger.Error(error);
|
_asyncOperation = UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(MainAssetInfo.AssetPath, loadSceneParameters);
|
||||||
InvokeCompletion(error, EOperationStatus.Failed);
|
if (_asyncOperation != null)
|
||||||
|
{
|
||||||
|
_asyncOperation.allowSceneActivation = !_suspendLoad;
|
||||||
|
_asyncOperation.priority = 100;
|
||||||
|
SceneObject = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
|
||||||
|
_steps = ESteps.Checking;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string error = $"Failed to load scene : {MainAssetInfo.AssetPath}";
|
||||||
|
YooLogger.Error(error);
|
||||||
|
InvokeCompletion(error, EOperationStatus.Failed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 检测加载结果
|
// 3. 检测加载结果
|
||||||
if (_steps == ESteps.Checking)
|
if (_steps == ESteps.Checking)
|
||||||
{
|
{
|
||||||
Progress = _asyncOperation.progress;
|
if (_asyncOperation != null)
|
||||||
if (_asyncOperation.isDone)
|
|
||||||
{
|
{
|
||||||
if (SceneObject.IsValid())
|
if (IsWaitForAsyncComplete || IsForceDestroyComplete)
|
||||||
{
|
{
|
||||||
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
// 场景加载无法强制异步转同步
|
||||||
|
YooLogger.Error("The scene is loading asyn !");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
|
Progress = _asyncOperation.progress;
|
||||||
YooLogger.Error(error);
|
if (_asyncOperation.isDone == false)
|
||||||
InvokeCompletion(error, EOperationStatus.Failed);
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SceneObject.IsValid())
|
||||||
|
{
|
||||||
|
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string error = $"The loaded scene is invalid : {MainAssetInfo.AssetPath}";
|
||||||
|
YooLogger.Error(error);
|
||||||
|
InvokeCompletion(error, EOperationStatus.Failed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -419,7 +419,7 @@ namespace YooAsset
|
||||||
public GetAllCacheFileInfosOperation GetAllCacheFileInfosAsync(string packageVersion)
|
public GetAllCacheFileInfosOperation GetAllCacheFileInfosAsync(string packageVersion)
|
||||||
{
|
{
|
||||||
DebugCheckInitialize();
|
DebugCheckInitialize();
|
||||||
|
|
||||||
var operation = new GetAllCacheFileInfosOperation(_persistentMgr, _cacheMgr, packageVersion);
|
var operation = new GetAllCacheFileInfosOperation(_persistentMgr, _cacheMgr, packageVersion);
|
||||||
OperationSystem.StartOperation(PackageName, operation);
|
OperationSystem.StartOperation(PackageName, operation);
|
||||||
return operation;
|
return operation;
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue