refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-16 10:28:01 +08:00
parent 2b6e7856e7
commit b0e85017d2
100 changed files with 1042 additions and 1582 deletions
@@ -76,62 +76,48 @@ namespace YooAsset
/// </summary>
public GameObject InstantiateSync()
{
return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, null, false);
var options = new InstantiateOptions(true);
return InstantiateSyncInternal(options);
}
public GameObject InstantiateSync(Transform parent)
/// <summary>
/// 同步初始化游戏对象
/// </summary>
public GameObject InstantiateSync(InstantiateOptions options)
{
return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, false);
}
public GameObject InstantiateSync(Transform parent, bool worldPositionStays)
{
return InstantiateSyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays);
}
public GameObject InstantiateSync(Vector3 position, Quaternion rotation)
{
return InstantiateSyncInternal(true, position, rotation, null, false);
}
public GameObject InstantiateSync(Vector3 position, Quaternion rotation, Transform parent)
{
return InstantiateSyncInternal(true, position, rotation, parent, false);
return InstantiateSyncInternal(options);
}
/// <summary>
/// 异步初始化游戏对象
/// </summary>
public InstantiateOperation InstantiateAsync(bool actived = true)
public InstantiateOperation InstantiateAsync()
{
return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, null, false, actived);
}
public InstantiateOperation InstantiateAsync(Transform parent, bool actived = true)
{
return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, false, actived);
}
public InstantiateOperation InstantiateAsync(Transform parent, bool worldPositionStays, bool actived = true)
{
return InstantiateAsyncInternal(false, Vector3.zero, Quaternion.identity, parent, worldPositionStays, actived);
}
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, bool actived = true)
{
return InstantiateAsyncInternal(true, position, rotation, null, false, actived);
}
public InstantiateOperation InstantiateAsync(Vector3 position, Quaternion rotation, Transform parent, bool actived = true)
{
return InstantiateAsyncInternal(true, position, rotation, parent, false, actived);
var options = new InstantiateOptions(true);
return InstantiateAsyncInternal(options);
}
private GameObject InstantiateSyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
/// <summary>
/// 异步初始化游戏对象
/// </summary>
public InstantiateOperation InstantiateAsync(InstantiateOptions options)
{
return InstantiateAsyncInternal(options);
}
private GameObject InstantiateSyncInternal(InstantiateOptions options)
{
if (IsValidWithWarning == false)
return null;
if (Provider.AssetObject == null)
return null;
return InstantiateOperation.InstantiateInternal(Provider.AssetObject, setPositionAndRotation, position, rotation, parent, worldPositionStays);
return InstantiateOperation.InstantiateInternal(Provider.AssetObject, options);
}
private InstantiateOperation InstantiateAsyncInternal(bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays, bool actived)
private InstantiateOperation InstantiateAsyncInternal(InstantiateOptions options)
{
string packageName = GetAssetInfo().PackageName;
InstantiateOperation operation = new InstantiateOperation(this, setPositionAndRotation, position, rotation, parent, worldPositionStays, actived);
InstantiateOperation operation = new InstantiateOperation(this, options);
OperationSystem.StartOperation(packageName, operation);
return operation;
}
@@ -116,7 +116,7 @@ namespace YooAsset
/// 异步卸载场景对象
/// 注意:场景卸载成功后,会自动释放该handle的引用计数!
/// </summary>
public UnloadSceneOperation UnloadAsync()
public UnloadSceneOperation UnloadSceneAsync()
{
string packageName = GetAssetInfo().PackageName;
@@ -14,12 +14,7 @@ namespace YooAsset
}
private readonly AssetHandle _handle;
private readonly bool _setPositionAndRotation;
private readonly Vector3 _position;
private readonly Quaternion _rotation;
private readonly Transform _parent;
private readonly bool _worldPositionStays;
private readonly bool _actived;
private readonly InstantiateOptions _options;
private ESteps _steps = ESteps.None;
#if UNITY_2023_3_OR_NEWER
@@ -32,16 +27,10 @@ namespace YooAsset
public GameObject Result = null;
internal InstantiateOperation(AssetHandle handle, bool setPositionAndRotation, Vector3 position, Quaternion rotation,
Transform parent, bool worldPositionStays, bool actived)
internal InstantiateOperation(AssetHandle handle, InstantiateOptions options)
{
_handle = handle;
_setPositionAndRotation = setPositionAndRotation;
_position = position;
_rotation = rotation;
_parent = parent;
_worldPositionStays = worldPositionStays;
_actived = actived;
_options = options;
}
internal override void InternalStart()
{
@@ -90,8 +79,8 @@ namespace YooAsset
if (_steps == ESteps.CloneSync)
{
// 实例化游戏对象
Result = InstantiateInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays);
if (_actived == false)
Result = InstantiateInternal(_handle.AssetObject, _options);
if (_options.Actived == false)
Result.SetActive(false);
_steps = ESteps.Done;
@@ -103,7 +92,7 @@ namespace YooAsset
{
if (_instantiateAsync == null)
{
_instantiateAsync = InstantiateAsyncInternal(_handle.AssetObject, _setPositionAndRotation, _position, _rotation, _parent, _worldPositionStays);
_instantiateAsync = InstantiateAsyncInternal(_handle.AssetObject, _options);
}
if (IsWaitForAsyncComplete)
@@ -117,7 +106,7 @@ namespace YooAsset
Result = _instantiateAsync.Result[0] as GameObject;
if (Result != null)
{
if (_actived == false)
if (_options.Actived == false)
Result.SetActive(false);
_steps = ESteps.Done;
@@ -165,22 +154,22 @@ namespace YooAsset
/// <summary>
/// 同步实例化
/// </summary>
internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, bool setPositionAndRotation, Vector3 position, Quaternion rotation, Transform parent, bool worldPositionStays)
internal static GameObject InstantiateInternal(UnityEngine.Object assetObject, InstantiateOptions options)
{
if (assetObject == null)
return null;
if (setPositionAndRotation)
if (options.SetPositionAndRotation)
{
if (parent != null)
return UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation, parent);
if (options.Parent != null)
return UnityEngine.Object.Instantiate(assetObject as GameObject, options.Position, options.Rotation, options.Parent);
else
return UnityEngine.Object.Instantiate(assetObject as GameObject, position, rotation);
return UnityEngine.Object.Instantiate(assetObject as GameObject, options.Position, options.Rotation);
}
else
{
if (parent != null)
return UnityEngine.Object.Instantiate(assetObject as GameObject, parent, worldPositionStays);
if (options.Parent != null)
return UnityEngine.Object.Instantiate(assetObject as GameObject, options.Parent, options.InWorldSpace);
else
return UnityEngine.Object.Instantiate(assetObject as GameObject);
}
@@ -192,19 +181,19 @@ namespace YooAsset
/// 注意: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)
internal static AsyncInstantiateOperation InstantiateAsyncInternal(UnityEngine.Object assetObject, InstantiateOptions options)
{
if (setPositionAndRotation)
if (options.SetPositionAndRotation)
{
if (parent != null)
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, parent, position, rotation);
if (options.Parent != null)
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, options.Parent, options.Position, options.Rotation);
else
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, position, rotation);
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, options.Position, options.Rotation);
}
else
{
if (parent != null)
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, parent);
if (options.Parent != null)
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject, options.Parent);
else
return UnityEngine.Object.InstantiateAsync(assetObject as GameObject);
}
@@ -0,0 +1,77 @@
using UnityEngine;
namespace YooAsset
{
public struct InstantiateOptions
{
/// <summary>
/// 是否激活实例化对象
/// </summary>
public bool Actived { private set; get; }
/// <summary>
/// 将指定给新对象的父对象
/// </summary>
public Transform Parent { private set; get; }
/// <summary>
/// 分配父对象时, 定位新对象关系。
/// true 在世界空间中定位新对象。
/// false 相对于父对象来设置新对象。
/// </summary>
public bool InWorldSpace { private set; get; }
/// <summary>
/// 新对象的位置
/// </summary>
public Vector3 Position { private set; get; }
/// <summary>
/// 新对象的方向
/// </summary>
public Quaternion Rotation { private set; get; }
internal bool SetPositionAndRotation { private set; get; }
public InstantiateOptions(bool actived)
{
Actived = actived;
Parent = null;
InWorldSpace = false;
SetPositionAndRotation = false;
Position = Vector3.zero;
Rotation = Quaternion.identity;
}
public InstantiateOptions(bool actived, Transform parent, bool inWorldSpace)
{
Actived = actived;
Parent = parent;
InWorldSpace = inWorldSpace;
SetPositionAndRotation = false;
Position = Vector3.zero;
Rotation = Quaternion.identity;
}
public InstantiateOptions(bool actived, Transform parent, Vector3 position, Quaternion rotation)
{
Actived = actived;
Parent = parent;
InWorldSpace = false;
SetPositionAndRotation = true;
Position = position;
Rotation = rotation;
}
public InstantiateOptions(bool actived, Vector3 position, Quaternion rotation)
{
Actived = actived;
Parent = null;
InWorldSpace = false;
SetPositionAndRotation = true;
Position = position;
Rotation = rotation;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7432581e3bde71648adef94499c7a398
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace YooAsset
{
internal class LoadBundleFileOperation : AsyncOperationBase
internal class LoadBundleOperation : AsyncOperationBase
{
private enum ESteps
{
@@ -51,7 +51,7 @@ namespace YooAsset
public BundleResult Result { set; get; }
internal LoadBundleFileOperation(ResourceManager resourceManager, BundleInfo bundleInfo)
internal LoadBundleOperation(ResourceManager resourceManager, BundleInfo bundleInfo)
{
_resManager = resourceManager;
LoadBundleInfo = bundleInfo;
@@ -85,7 +85,7 @@ namespace YooAsset
{
// 统计计数增加
_resManager.BundleLoadingCounter++;
_loadBundleOp = LoadBundleInfo.LoadBundleFile();
_loadBundleOp = LoadBundleInfo.CreateBundleLoader();
_loadBundleOp.StartOperation();
AddChildOperation(_loadBundleOp);
}
@@ -1,6 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
@@ -14,19 +13,19 @@ namespace YooAsset
}
private readonly ResourceManager _resManager;
private readonly int _loopCount;
private readonly UnloadUnusedAssetsOptions _options;
private int _loopCounter = 0;
private ESteps _steps = ESteps.None;
internal UnloadUnusedAssetsOperation(ResourceManager resourceManager, int loopCount)
internal UnloadUnusedAssetsOperation(ResourceManager resourceManager, UnloadUnusedAssetsOptions options)
{
_resManager = resourceManager;
_loopCount = loopCount;
_options = options;
}
internal override void InternalStart()
{
_steps = ESteps.UnloadUnused;
_loopCounter = _loopCount;
_loopCounter = _options.LoopCount;
}
internal override void InternalUpdate()
{
@@ -57,7 +56,7 @@ namespace YooAsset
}
internal override string InternalGetDesc()
{
return $"LoopCount : {_loopCount}";
return $"LoopCount : {_options.LoopCount}";
}
/// <summary>
@@ -65,7 +64,7 @@ namespace YooAsset
/// </summary>
private void LoopUnloadUnused()
{
var removeList = new List<LoadBundleFileOperation>(_resManager.LoaderDic.Count);
var removeList = new List<LoadBundleOperation>(_resManager.LoaderDic.Count);
// 注意:优先销毁资源提供者
foreach (var loader in _resManager.LoaderDic.Values)
@@ -0,0 +1,16 @@
namespace YooAsset
{
public struct UnloadUnusedAssetsOptions
{
/// <summary>
/// 循环迭代次数
/// </summary>
public int LoopCount { private set; get; }
public UnloadUnusedAssetsOptions(int loopCount)
{
LoopCount = loopCount;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 33a033398461486429728fc87b8b9840
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -80,8 +80,8 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
protected readonly ResourceManager _resManager;
private readonly LoadBundleFileOperation _mainBundleLoader;
private readonly List<LoadBundleFileOperation> _bundleLoaders = new List<LoadBundleFileOperation>(10);
private readonly LoadBundleOperation _mainBundleLoader;
private readonly List<LoadBundleOperation> _bundleLoaders = new List<LoadBundleOperation>(10);
private readonly HashSet<HandleBase> _handles = new HashSet<HandleBase>();
public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
@@ -10,12 +10,12 @@ namespace YooAsset
internal class ResourceManager
{
internal readonly Dictionary<string, ProviderOperation> ProviderDic = new Dictionary<string, ProviderOperation>(5000);
internal readonly Dictionary<string, LoadBundleFileOperation> LoaderDic = new Dictionary<string, LoadBundleFileOperation>(5000);
internal readonly Dictionary<string, LoadBundleOperation> LoaderDic = new Dictionary<string, LoadBundleOperation>(5000);
internal readonly List<SceneHandle> SceneHandles = new List<SceneHandle>(100);
private FileSystemHost _fileSystemHost;
private long _sceneCreateIndex = 0;
private IBundleQuery _bundleQuery;
private int _bundleLoadingMaxConcurrency;
// 开发者配置选项
public bool AutoUnloadBundleWhenUnused { private set; get; }
public bool WebGLForceSyncLoadAsset { private set; get; }
@@ -44,12 +44,12 @@ namespace YooAsset
/// <summary>
/// 初始化
/// </summary>
public void Initialize(InitializePackageOptions options, IBundleQuery bundleServices)
public void Initialize(InitializePackageOptions options, FileSystemHost host)
{
_fileSystemHost = host;
_bundleLoadingMaxConcurrency = options.BundleLoadingMaxConcurrency;
AutoUnloadBundleWhenUnused = options.AutoUnloadBundleWhenUnused;
WebGLForceSyncLoadAsset = options.WebGLForceSyncLoadAsset;
_bundleQuery = bundleServices;
SceneManager.sceneUnloaded += OnSceneUnloaded;
}
@@ -82,7 +82,7 @@ namespace YooAsset
loopCount--;
// 卸载主资源包加载器
string mainBundleName = _bundleQuery.GetMainBundleName(assetInfo.Asset.BundleID);
string mainBundleName = _fileSystemHost.GetMainBundleName(assetInfo.Asset.BundleID);
var mainLoader = TryGetBundleFileLoader(mainBundleName);
if (mainLoader != null)
{
@@ -97,7 +97,7 @@ namespace YooAsset
// 卸载依赖资源包加载器
foreach (var dependID in assetInfo.Asset.DependBundleIDs)
{
string dependBundleName = _bundleQuery.GetMainBundleName(dependID);
string dependBundleName = _fileSystemHost.GetMainBundleName(dependID);
var dependLoader = TryGetBundleFileLoader(dependBundleName);
if (dependLoader != null)
{
@@ -296,15 +296,15 @@ namespace YooAsset
return provider.CreateHandle<RawFileHandle>();
}
internal LoadBundleFileOperation CreateMainBundleFileLoader(AssetInfo assetInfo)
internal LoadBundleOperation CreateMainBundleFileLoader(AssetInfo assetInfo)
{
BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo);
BundleInfo bundleInfo = _fileSystemHost.GetMainBundleInfo(assetInfo);
return CreateBundleFileLoaderInternal(bundleInfo);
}
internal List<LoadBundleFileOperation> CreateDependBundleFileLoaders(AssetInfo assetInfo)
internal List<LoadBundleOperation> CreateDependBundleFileLoaders(AssetInfo assetInfo)
{
List<BundleInfo> bundleInfos = _bundleQuery.GetDependBundleInfos(assetInfo);
List<LoadBundleFileOperation> result = new List<LoadBundleFileOperation>(bundleInfos.Count);
List<BundleInfo> bundleInfos = _fileSystemHost.GetDependBundleInfos(assetInfo);
List<LoadBundleOperation> result = new List<LoadBundleOperation>(bundleInfos.Count);
foreach (var bundleInfo in bundleInfos)
{
var bundleLoader = CreateBundleFileLoaderInternal(bundleInfo);
@@ -321,7 +321,7 @@ namespace YooAsset
}
internal bool CheckBundleDestroyed(int bundleID)
{
string bundleName = _bundleQuery.GetMainBundleName(bundleID);
string bundleName = _fileSystemHost.GetMainBundleName(bundleID);
var bundleFileLoader = TryGetBundleFileLoader(bundleName);
if (bundleFileLoader == null)
return true;
@@ -329,7 +329,7 @@ namespace YooAsset
}
internal bool CheckBundleReleasable(int bundleID)
{
string bundleName = _bundleQuery.GetMainBundleName(bundleID);
string bundleName = _fileSystemHost.GetMainBundleName(bundleID);
var bundleFileLoader = TryGetBundleFileLoader(bundleName);
if (bundleFileLoader == null)
return true;
@@ -344,22 +344,22 @@ namespace YooAsset
return BundleLoadingCounter >= _bundleLoadingMaxConcurrency;
}
private LoadBundleFileOperation CreateBundleFileLoaderInternal(BundleInfo bundleInfo)
private LoadBundleOperation CreateBundleFileLoaderInternal(BundleInfo bundleInfo)
{
// 如果加载器已经存在
string bundleName = bundleInfo.Bundle.BundleName;
LoadBundleFileOperation loaderOperation = TryGetBundleFileLoader(bundleName);
LoadBundleOperation loaderOperation = TryGetBundleFileLoader(bundleName);
if (loaderOperation != null)
return loaderOperation;
// 新增下载需求
loaderOperation = new LoadBundleFileOperation(this, bundleInfo);
loaderOperation = new LoadBundleOperation(this, bundleInfo);
LoaderDic.Add(bundleName, loaderOperation);
return loaderOperation;
}
private LoadBundleFileOperation TryGetBundleFileLoader(string bundleName)
private LoadBundleOperation TryGetBundleFileLoader(string bundleName)
{
if (LoaderDic.TryGetValue(bundleName, out LoadBundleFileOperation value))
if (LoaderDic.TryGetValue(bundleName, out LoadBundleOperation value))
return value;
else
return null;