diff --git a/Assets/YooAsset/Runtime/AssetSystem/Handles.meta b/Assets/YooAsset/Runtime/AssetSystem/Handles.meta new file mode 100644 index 0000000..d3aa0af --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 937392854b37d5043808598f2d0e07ec +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/AssetOperationHandle.cs similarity index 65% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs rename to Assets/YooAsset/Runtime/AssetSystem/Handles/AssetOperationHandle.cs index eabbed6..97ba1ac 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/AssetOperationHandle.cs @@ -53,18 +53,29 @@ namespace YooAsset } /// - /// 初始化的游戏对象(只限于请求的资源对象类型为GameObject) + /// 同步初始化游戏对象 /// - 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); + } + + /// + /// 异步初始化游戏对象 + /// + public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent = null) + { + InstantiateOperation operation = new InstantiateOperation(this, position, rotation, parent); + OperationSystem.ProcessOperaiton(operation); + return operation; } /// diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Handles/AssetOperationHandle.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/AssetOperationHandle.cs.meta rename to Assets/YooAsset/Runtime/AssetSystem/Handles/AssetOperationHandle.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/OperationHandleBase.cs similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs rename to Assets/YooAsset/Runtime/AssetSystem/Handles/OperationHandleBase.cs diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Handles/OperationHandleBase.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/OperationHandleBase.cs.meta rename to Assets/YooAsset/Runtime/AssetSystem/Handles/OperationHandleBase.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs similarity index 60% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs rename to Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs index cfe9ae0..0281f88 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs @@ -1,114 +1,9 @@ -using UnityEngine; -using UnityEngine.SceneManagement; +using UnityEngine.SceneManagement; namespace YooAsset { public class SceneOperationHandle : OperationHandleBase { - /// - /// 场景卸载异步操作类 - /// - 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; - - /// - /// 场景卸载进度 - /// - 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 _callback; internal SceneOperationHandle(ProviderBase provider) : base(provider) diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/SceneOperationHandle.cs.meta rename to Assets/YooAsset/Runtime/AssetSystem/Handles/SceneOperationHandle.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/SubAssetsOperationHandle.cs b/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs similarity index 95% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/SubAssetsOperationHandle.cs rename to Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs index 0956310..9443f6b 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Operations/SubAssetsOperationHandle.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/SubAssetsOperationHandle.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/AssetSystem/Operations/SubAssetsOperationHandle.cs.meta rename to Assets/YooAsset/Runtime/AssetSystem/Handles/SubAssetsOperationHandle.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/InstantiateOperation.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/InstantiateOperation.cs new file mode 100644 index 0000000..6c8bae7 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/InstantiateOperation.cs @@ -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; + + /// + /// 实例化的游戏对象 + /// + 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; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/InstantiateOperation.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Operations/InstantiateOperation.cs.meta new file mode 100644 index 0000000..99282ee --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/InstantiateOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8e7d3b16d9b01f548b4654e958d43a37 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/UnloadSceneOperation.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/UnloadSceneOperation.cs new file mode 100644 index 0000000..909e28b --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/UnloadSceneOperation.cs @@ -0,0 +1,108 @@ +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace YooAsset +{ + /// + /// 场景卸载异步操作类 + /// + 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; + + /// + /// 场景卸载进度 + /// + 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; + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/UnloadSceneOperation.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Operations/UnloadSceneOperation.cs.meta new file mode 100644 index 0000000..3ce7aeb --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/UnloadSceneOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5299512de5ab5c141ae37dac6ee1721e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs index 849cb5e..e70d94a 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/BundledSubAssetsProvider.cs @@ -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(); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs index 4bfe499..9b1fd54 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/DatabaseSubAssetsProvider.cs @@ -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(); diff --git a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs index 19f8c07..2688acd 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Provider/ProviderBase.cs @@ -38,7 +38,7 @@ namespace YooAsset /// /// 获取的资源对象集合 /// - public UnityEngine.Object[] AllAssets { protected set; get; } + public UnityEngine.Object[] AllAssetObjects { protected set; get; } /// /// 获取的场景对象 @@ -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 } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index 2514f93..71f7b18 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -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 } } \ No newline at end of file