mirror of https://github.com/tuyoogame/YooAsset
Update AssetSystem
parent
3e8b8123e1
commit
829b4fca76
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 937392854b37d5043808598f2d0e07ec
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -53,18 +53,29 @@ namespace YooAsset
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化的游戏对象(只限于请求的资源对象类型为GameObject)
|
||||
/// 同步初始化游戏对象
|
||||
/// </summary>
|
||||
public GameObject InstantiateObject
|
||||
public GameObject InstantiateSync(Vector3 position, Quaternion rotation, Transform parent = null)
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
if (_provider.AssetObject == null)
|
||||
return null;
|
||||
return UnityEngine.Object.Instantiate(_provider.AssetObject as GameObject);
|
||||
}
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
if (_provider.AssetObject == null)
|
||||
return null;
|
||||
|
||||
if (parent == null)
|
||||
return UnityEngine.Object.Instantiate(_provider.AssetObject as GameObject, position, rotation);
|
||||
else
|
||||
return UnityEngine.Object.Instantiate(_provider.AssetObject as GameObject, position, rotation, parent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步初始化游戏对象
|
||||
/// </summary>
|
||||
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent = null)
|
||||
{
|
||||
InstantiateOperation operation = new InstantiateOperation(this, position, rotation, parent);
|
||||
OperationSystem.ProcessOperaiton(operation);
|
||||
return operation;
|
||||
}
|
||||
|
||||
/// <summary>
|
|
@ -1,114 +1,9 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class SceneOperationHandle : OperationHandleBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 场景卸载异步操作类
|
||||
/// </summary>
|
||||
public class UnloadSceneOperation : AsyncOperationBase
|
||||
{
|
||||
private enum EFlag
|
||||
{
|
||||
Normal,
|
||||
Error,
|
||||
Skip,
|
||||
}
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
UnLoad,
|
||||
Checking,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly EFlag _flag;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private Scene _scene;
|
||||
private AsyncOperation _asyncOp;
|
||||
|
||||
/// <summary>
|
||||
/// 场景卸载进度
|
||||
/// </summary>
|
||||
public float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_asyncOp == null)
|
||||
return 0;
|
||||
return _asyncOp.progress;
|
||||
}
|
||||
}
|
||||
|
||||
internal UnloadSceneOperation()
|
||||
{
|
||||
_flag = EFlag.Skip;
|
||||
}
|
||||
internal UnloadSceneOperation(string error)
|
||||
{
|
||||
_flag = EFlag.Error;
|
||||
Error = error;
|
||||
}
|
||||
internal UnloadSceneOperation(Scene scene)
|
||||
{
|
||||
_flag = EFlag.Normal;
|
||||
_scene = scene;
|
||||
}
|
||||
internal override void Start()
|
||||
{
|
||||
if (_flag == EFlag.Normal)
|
||||
{
|
||||
_steps = ESteps.UnLoad;
|
||||
}
|
||||
else if (_flag == EFlag.Skip)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else if (_flag == EFlag.Error)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(_flag.ToString());
|
||||
}
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.UnLoad)
|
||||
{
|
||||
if (_scene.IsValid() && _scene.isLoaded)
|
||||
{
|
||||
_asyncOp = SceneManager.UnloadSceneAsync(_scene);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = "Scene is invalid or is not loaded.";
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_asyncOp.isDone == false)
|
||||
return;
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private System.Action<SceneOperationHandle> _callback;
|
||||
|
||||
internal SceneOperationHandle(ProviderBase provider) : base(provider)
|
|
@ -47,7 +47,7 @@ namespace YooAsset
|
|||
{
|
||||
if (IsValid == false)
|
||||
return null;
|
||||
return _provider.AllAssets;
|
||||
return _provider.AllAssetObjects;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ namespace YooAsset
|
|||
if (IsValid == false)
|
||||
return null;
|
||||
|
||||
foreach (var asset in _provider.AllAssets)
|
||||
foreach (var asset in _provider.AllAssetObjects)
|
||||
{
|
||||
if (asset.name == assetName)
|
||||
return asset as TObject;
|
|
@ -0,0 +1,67 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
public class InstantiateOperation : AsyncOperationBase
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Clone,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly AssetOperationHandle _handle;
|
||||
private readonly Vector3 _position;
|
||||
private readonly Quaternion _rotation;
|
||||
private readonly Transform _parent;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
/// 实例化的游戏对象
|
||||
/// </summary>
|
||||
public GameObject Result = null;
|
||||
|
||||
|
||||
internal InstantiateOperation(AssetOperationHandle handle, Vector3 position, Quaternion rotation, Transform parent)
|
||||
{
|
||||
_handle = handle;
|
||||
_position = position;
|
||||
_rotation = rotation;
|
||||
_parent = parent;
|
||||
}
|
||||
internal override void Start()
|
||||
{
|
||||
_steps = ESteps.Clone;
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.Clone)
|
||||
{
|
||||
if (_handle.IsValid == false)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"{nameof(AssetOperationHandle)} is invalid.";
|
||||
}
|
||||
if (_handle.AssetObject == null)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
Error = $"{nameof(AssetOperationHandle.AssetObject)} is null.";
|
||||
}
|
||||
|
||||
if (_parent == null)
|
||||
Result = Object.Instantiate(_handle.AssetObject as GameObject, _position, _rotation);
|
||||
else
|
||||
Result = Object.Instantiate(_handle.AssetObject as GameObject, _position, _rotation, _parent);
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8e7d3b16d9b01f548b4654e958d43a37
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,108 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 场景卸载异步操作类
|
||||
/// </summary>
|
||||
public class UnloadSceneOperation : AsyncOperationBase
|
||||
{
|
||||
private enum EFlag
|
||||
{
|
||||
Normal,
|
||||
Error,
|
||||
Skip,
|
||||
}
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
UnLoad,
|
||||
Checking,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly EFlag _flag;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private Scene _scene;
|
||||
private AsyncOperation _asyncOp;
|
||||
|
||||
/// <summary>
|
||||
/// 场景卸载进度
|
||||
/// </summary>
|
||||
public float Progress
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_asyncOp == null)
|
||||
return 0;
|
||||
return _asyncOp.progress;
|
||||
}
|
||||
}
|
||||
|
||||
internal UnloadSceneOperation()
|
||||
{
|
||||
_flag = EFlag.Skip;
|
||||
}
|
||||
internal UnloadSceneOperation(string error)
|
||||
{
|
||||
_flag = EFlag.Error;
|
||||
Error = error;
|
||||
}
|
||||
internal UnloadSceneOperation(Scene scene)
|
||||
{
|
||||
_flag = EFlag.Normal;
|
||||
_scene = scene;
|
||||
}
|
||||
internal override void Start()
|
||||
{
|
||||
if (_flag == EFlag.Normal)
|
||||
{
|
||||
_steps = ESteps.UnLoad;
|
||||
}
|
||||
else if (_flag == EFlag.Skip)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
else if (_flag == EFlag.Error)
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException(_flag.ToString());
|
||||
}
|
||||
}
|
||||
internal override void Update()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.UnLoad)
|
||||
{
|
||||
if (_scene.IsValid() && _scene.isLoaded)
|
||||
{
|
||||
_asyncOp = SceneManager.UnloadSceneAsync(_scene);
|
||||
_steps = ESteps.Checking;
|
||||
}
|
||||
else
|
||||
{
|
||||
Error = "Scene is invalid or is not loaded.";
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Failed;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Checking)
|
||||
{
|
||||
if (_asyncOp.isDone == false)
|
||||
return;
|
||||
|
||||
_steps = ESteps.Done;
|
||||
Status = EOperationStatus.Succeed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5299512de5ab5c141ae37dac6ee1721e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -62,9 +62,9 @@ namespace YooAsset
|
|||
if (IsWaitForAsyncComplete)
|
||||
{
|
||||
if (AssetType == null)
|
||||
AllAssets = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName);
|
||||
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName);
|
||||
else
|
||||
AllAssets = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName, AssetType);
|
||||
AllAssetObjects = OwnerBundle.CacheBundle.LoadAssetWithSubAssets(AssetName, AssetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -85,17 +85,17 @@ namespace YooAsset
|
|||
{
|
||||
// 强制挂起主线程(注意:该操作会很耗时)
|
||||
YooLogger.Warning("Suspend the main thread to load unity asset.");
|
||||
AllAssets = _cacheRequest.allAssets;
|
||||
AllAssetObjects = _cacheRequest.allAssets;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_cacheRequest.isDone == false)
|
||||
return;
|
||||
AllAssets = _cacheRequest.allAssets;
|
||||
AllAssetObjects = _cacheRequest.allAssets;
|
||||
}
|
||||
}
|
||||
|
||||
Status = AllAssets == null ? EStatus.Fail : EStatus.Success;
|
||||
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
|
||||
if (Status == EStatus.Fail)
|
||||
YooLogger.Warning($"Failed to load sub assets : {AssetName} from bundle : {OwnerBundle.BundleFileInfo.BundleName}");
|
||||
InvokeCompletion();
|
||||
|
|
|
@ -52,7 +52,7 @@ namespace YooAsset
|
|||
{
|
||||
if (AssetType == null)
|
||||
{
|
||||
AllAssets = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
|
||||
AllAssetObjects = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ namespace YooAsset
|
|||
if (findAsset.GetType() == AssetType)
|
||||
result.Add(findAsset);
|
||||
}
|
||||
AllAssets = result.ToArray();
|
||||
AllAssetObjects = result.ToArray();
|
||||
}
|
||||
Status = EStatus.Checking;
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ namespace YooAsset
|
|||
// 2. 检测加载结果
|
||||
if (Status == EStatus.Checking)
|
||||
{
|
||||
Status = AllAssets == null ? EStatus.Fail : EStatus.Success;
|
||||
Status = AllAssetObjects == null ? EStatus.Fail : EStatus.Success;
|
||||
if (Status == EStatus.Fail)
|
||||
YooLogger.Warning($"Failed to load sub assets : {AssetName}");
|
||||
InvokeCompletion();
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace YooAsset
|
|||
/// <summary>
|
||||
/// 获取的资源对象集合
|
||||
/// </summary>
|
||||
public UnityEngine.Object[] AllAssets { protected set; get; }
|
||||
public UnityEngine.Object[] AllAssetObjects { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取的场景对象
|
||||
|
@ -210,7 +210,7 @@ namespace YooAsset
|
|||
}
|
||||
}
|
||||
|
||||
// 异步操作相关
|
||||
#region 异步编程相关
|
||||
private System.Threading.EventWaitHandle _waitHandle;
|
||||
private System.Threading.WaitHandle WaitHandle
|
||||
{
|
||||
|
@ -230,5 +230,6 @@ namespace YooAsset
|
|||
}
|
||||
_waitHandle?.Set();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -53,9 +53,10 @@ namespace YooAsset
|
|||
internal void Finish()
|
||||
{
|
||||
_callback?.Invoke(this);
|
||||
_waitHandle?.Set();
|
||||
}
|
||||
|
||||
#region 异步相关
|
||||
#region 异步编程相关
|
||||
public bool MoveNext()
|
||||
{
|
||||
return !IsDone;
|
||||
|
@ -64,6 +65,29 @@ namespace YooAsset
|
|||
{
|
||||
}
|
||||
public object Current => null;
|
||||
|
||||
private System.Threading.EventWaitHandle _waitHandle;
|
||||
private System.Threading.WaitHandle WaitHandle
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_waitHandle == null)
|
||||
_waitHandle = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.ManualReset);
|
||||
_waitHandle.Reset();
|
||||
return _waitHandle;
|
||||
}
|
||||
}
|
||||
public System.Threading.Tasks.Task Task
|
||||
{
|
||||
get
|
||||
{
|
||||
var handle = WaitHandle;
|
||||
return System.Threading.Tasks.Task.Factory.StartNew(o =>
|
||||
{
|
||||
handle.WaitOne();
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue