mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
yoo2.2,9
yoo2.2,9
This commit is contained in:
@@ -7,7 +7,9 @@ namespace YooAsset
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Clone,
|
||||
LoadObject,
|
||||
CloneSync,
|
||||
CloneAsync,
|
||||
Done,
|
||||
}
|
||||
|
||||
@@ -17,15 +19,21 @@ namespace YooAsset
|
||||
private readonly Quaternion _rotation;
|
||||
private readonly Transform _parent;
|
||||
private readonly bool _worldPositionStays;
|
||||
private readonly bool _actived;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
#if UNITY_2023_3_OR_NEWER
|
||||
private AsyncInstantiateOperation _instantiateAsync;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 实例化的游戏对象
|
||||
/// </summary>
|
||||
public GameObject Result = null;
|
||||
|
||||
|
||||
internal InstantiateOperation(AssetHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
internal InstantiateOperation(AssetHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation,
|
||||
Transform parent, bool worldPositionStays, bool actived)
|
||||
{
|
||||
_handle = handle;
|
||||
_setPositionAndRotation = setPositionAndRotation;
|
||||
@@ -33,17 +41,18 @@ namespace YooAsset
|
||||
_rotation = rotation;
|
||||
_parent = parent;
|
||||
_worldPositionStays = worldPositionStays;
|
||||
_actived = actived;
|
||||
}
|
||||
internal override void InternalOnStart()
|
||||
{
|
||||
_steps = ESteps.Clone;
|
||||
_steps = ESteps.LoadObject;
|
||||
}
|
||||
internal override void InternalOnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.Clone)
|
||||
if (_steps == ESteps.LoadObject)
|
||||
{
|
||||
if (_handle.IsValidWithWarning == false)
|
||||
{
|
||||
@@ -64,12 +73,83 @@ namespace YooAsset
|
||||
return;
|
||||
}
|
||||
|
||||
#if UNITY_2023_3_OR_NEWER
|
||||
//TODO 官方BUG
|
||||
// BUG环境:Windows平台,Unity2022.3.41f1版本,编辑器模式。
|
||||
// BUG描述:异步实例化Prefab预制体,有概率丢失Mono脚本里序列化的数组里某个成员!
|
||||
//_steps = ESteps.CloneAsync;
|
||||
_steps = ESteps.CloneSync;
|
||||
#else
|
||||
_steps = ESteps.CloneSync;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (_steps == ESteps.CloneSync)
|
||||
{
|
||||
// 实例化游戏对象
|
||||
Result = InstantiateInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays);
|
||||
if (_actived == false)
|
||||
Result.SetActive(false);
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
|
||||
#if UNITY_2023_3_OR_NEWER
|
||||
if (_steps == ESteps.CloneAsync)
|
||||
{
|
||||
if (_instantiateAsync == null)
|
||||
{
|
||||
_instantiateAsync = InstantiateAsyncInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays);
|
||||
}
|
||||
|
||||
if (IsWaitForAsyncComplete)
|
||||
_instantiateAsync.WaitForCompletion();
|
||||
|
||||
if (_instantiateAsync.isDone == false)
|
||||
return;
|
||||
|
||||
if (_instantiateAsync.Result != null && _instantiateAsync.Result.Length > 0)
|
||||
{
|
||||
Result = _instantiateAsync.Result[0] as GameObject;
|
||||
if (Result != null)
|
||||
{
|
||||
if (_actived == false)
|
||||
Result.SetActive(false);
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"Instantiate game object is null !";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"Instantiate async results is null !";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
internal override void InternalWaitForAsyncComplete()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// 等待句柄完成
|
||||
if (_handle != null)
|
||||
_handle.WaitForAsyncComplete();
|
||||
|
||||
if (ExecuteWhileDone())
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -77,25 +157,17 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
public void Cancel()
|
||||
{
|
||||
if (IsDone == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"User cancelled !";
|
||||
}
|
||||
#if UNITY_2023_3_OR_NEWER
|
||||
if (_instantiateAsync != null && _instantiateAsync.isDone == false)
|
||||
_instantiateAsync.Cancel();
|
||||
#endif
|
||||
|
||||
SetAbort();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等待异步实例化结束
|
||||
/// 同步实例化
|
||||
/// </summary>
|
||||
public void WaitForAsyncComplete()
|
||||
{
|
||||
if (_steps == ESteps.Done)
|
||||
return;
|
||||
_handle.WaitForAsyncComplete();
|
||||
InternalOnUpdate();
|
||||
}
|
||||
|
||||
internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
{
|
||||
if (assetObject == null)
|
||||
@@ -104,29 +176,42 @@ namespace YooAsset
|
||||
if (setPositionAndRotation)
|
||||
{
|
||||
if (parent != null)
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent);
|
||||
return clone;
|
||||
}
|
||||
return UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent);
|
||||
else
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation);
|
||||
return clone;
|
||||
}
|
||||
return UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (parent != null)
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays);
|
||||
return clone;
|
||||
}
|
||||
return UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays);
|
||||
else
|
||||
{
|
||||
GameObject clone = UnityEngine.Object.Instantiate(assetObject as GameObject);
|
||||
return clone;
|
||||
}
|
||||
return UnityEngine.Object.Instantiate(assetObject as GameObject);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_2023_3_OR_NEWER
|
||||
/// <summary>
|
||||
/// 异步实例化
|
||||
/// 注意:Unity2022.3.20f1及以上版本生效
|
||||
/// https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Object.InstantiateAsync.html
|
||||
/// </summary>
|
||||
internal static AsyncInstantiateOperation InstantiateAsyncInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
|
||||
{
|
||||
if (setPositionAndRotation)
|
||||
{
|
||||
if (parent != null)
|
||||
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, parent, position, rotation);
|
||||
else
|
||||
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, position, rotation);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (parent != null)
|
||||
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, parent);
|
||||
else
|
||||
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user