From 3fd24f6f199e28cb3f54083bdff1f536eb99d153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Mon, 16 Dec 2024 18:38:46 +0800 Subject: [PATCH] fix #391 --- .../Runtime/LoadGameObjectOperation.cs | 102 ++++++++++++++++++ .../Runtime/LoadGameObjectOperation.cs.meta | 11 ++ 2 files changed, 113 insertions(+) create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs.meta diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs new file mode 100644 index 00000000..fa4ef821 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using YooAsset; + +public static class YooAssetsExtension +{ + public static LoadGameObjectOperation LoadGameObjectAsync(this ResourcePackage resourcePackage, string location, Vector3 position, Quaternion rotation, Transform parent, bool destroyGoOnRelease = false) + { + var operation = new LoadGameObjectOperation(location, position, rotation, parent, destroyGoOnRelease); + YooAssets.StartOperation(operation); + return operation; + } +} + +public class LoadGameObjectOperation : GameAsyncOperation +{ + private enum ESteps + { + None, + LoadAsset, + Done, + } + + private readonly string _location; + private readonly Vector3 _positon; + private readonly Quaternion _rotation; + private readonly Transform _parent; + private readonly bool _destroyGoOnRelease; + private AssetHandle _handle; + private ESteps _steps = ESteps.None; + + /// + /// 加载的游戏对象 + /// + public GameObject Go { private set; get; } + + + public LoadGameObjectOperation(string location, Vector3 position, Quaternion rotation, Transform parent, bool destroyGoOnRelease = false) + { + _location = location; + _positon = position; + _rotation = rotation; + _parent = parent; + _destroyGoOnRelease = destroyGoOnRelease; + } + protected override void OnStart() + { + _steps = ESteps.LoadAsset; + } + protected override void OnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.LoadAsset) + { + if (_handle == null) + { + _handle = YooAssets.LoadAssetAsync(_location); + } + + Progress = _handle.Progress; + if (_handle.IsDone == false) + return; + + if (_handle.Status != EOperationStatus.Succeed) + { + Error = _handle.LastError; + Status = EOperationStatus.Failed; + _steps = ESteps.Done; + } + else + { + Go = _handle.InstantiateSync(_positon, _rotation, _parent); + Status = EOperationStatus.Succeed; + _steps = ESteps.Done; + } + } + } + protected override void OnAbort() + { + } + + /// + /// 释放资源句柄 + /// + public void ReleaseHandle() + { + if (_handle != null) + { + _handle.Release(); + + if (_destroyGoOnRelease) + { + if (Go != null) + GameObject.Destroy(Go); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs.meta new file mode 100644 index 00000000..88493c79 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/LoadGameObjectOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 55478908ad5dc5541af1529a3faccee7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: