update asset system

优化了资源加载器查询逻辑。
pull/82/head
hevinci 2023-03-03 11:23:15 +08:00
parent ab96f3f28c
commit 06b033ed36
1 changed files with 69 additions and 55 deletions

View File

@ -8,11 +8,16 @@ namespace YooAsset
{ {
internal class AssetSystemImpl internal class AssetSystemImpl
{ {
private readonly List<BundleLoaderBase> _loaders = new List<BundleLoaderBase>(1000); private readonly Dictionary<string, BundleLoaderBase> _loaderDic = new Dictionary<string, BundleLoaderBase>(5000);
private readonly List<ProviderBase> _providers = new List<ProviderBase>(1000); private readonly List<BundleLoaderBase> _loaderList = new List<BundleLoaderBase>(5000);
private readonly Dictionary<string, ProviderBase> _providerDic = new Dictionary<string, ProviderBase>(5000);
private readonly List<ProviderBase> _providerList = new List<ProviderBase>(5000);
private readonly static Dictionary<string, SceneOperationHandle> _sceneHandles = new Dictionary<string, SceneOperationHandle>(100); private readonly static Dictionary<string, SceneOperationHandle> _sceneHandles = new Dictionary<string, SceneOperationHandle>(100);
private static long _sceneCreateCount = 0; private static long _sceneCreateCount = 0;
private bool _isUnloadSafe = true;
private string _packageName; private string _packageName;
private bool _simulationOnEditor; private bool _simulationOnEditor;
private int _loadingMaxNumber; private int _loadingMaxNumber;
@ -39,7 +44,7 @@ namespace YooAsset
public void Update() public void Update()
{ {
// 更新加载器 // 更新加载器
foreach (var loader in _loaders) foreach (var loader in _loaderList)
{ {
loader.Update(); loader.Update();
} }
@ -47,10 +52,11 @@ namespace YooAsset
// 更新资源提供者 // 更新资源提供者
// 注意:循环更新的时候,可能会扩展列表 // 注意:循环更新的时候,可能会扩展列表
// 注意:不能限制场景对象的加载 // 注意:不能限制场景对象的加载
_isUnloadSafe = false;
int loadingCount = 0; int loadingCount = 0;
for (int i = 0; i < _providers.Count; i++) for (int i = 0; i < _providerList.Count; i++)
{ {
var provider = _providers[i]; var provider = _providerList[i];
if (provider.IsSceneProvider()) if (provider.IsSceneProvider())
{ {
provider.Update(); provider.Update();
@ -64,6 +70,7 @@ namespace YooAsset
loadingCount++; loadingCount++;
} }
} }
_isUnloadSafe = true;
} }
/// <summary> /// <summary>
@ -71,17 +78,19 @@ namespace YooAsset
/// </summary> /// </summary>
public void DestroyAll() public void DestroyAll()
{ {
foreach (var provider in _providers) foreach (var provider in _providerList)
{ {
provider.Destroy(); provider.Destroy();
} }
_providers.Clear(); _providerList.Clear();
_providerDic.Clear();
foreach (var loader in _loaders) foreach (var loader in _loaderList)
{ {
loader.Destroy(true); loader.Destroy(true);
} }
_loaders.Clear(); _loaderList.Clear();
_loaderDic.Clear();
ClearSceneHandle(); ClearSceneHandle();
DecryptionServices = null; DecryptionServices = null;
@ -93,6 +102,12 @@ namespace YooAsset
/// </summary> /// </summary>
public void UnloadUnusedAssets() public void UnloadUnusedAssets()
{ {
if (_isUnloadSafe == false)
{
YooLogger.Warning("Can not unload unused assets when processing resource loading !");
return;
}
// 注意:资源包之间可能存在多层深层嵌套,需要多次循环释放。 // 注意:资源包之间可能存在多层深层嵌套,需要多次循环释放。
int loopCount = 10; int loopCount = 10;
for (int i = 0; i < loopCount; i++) for (int i = 0; i < loopCount; i++)
@ -104,29 +119,33 @@ namespace YooAsset
{ {
if (_simulationOnEditor) if (_simulationOnEditor)
{ {
for (int i = _providers.Count - 1; i >= 0; i--) for (int i = _providerList.Count - 1; i >= 0; i--)
{ {
if (_providers[i].CanDestroy()) var provider = _providerList[i];
if (provider.CanDestroy())
{ {
_providers[i].Destroy(); provider.Destroy();
_providers.RemoveAt(i); _providerList.RemoveAt(i);
_providerDic.Remove(provider.ProviderGUID);
} }
} }
} }
else else
{ {
for (int i = _loaders.Count - 1; i >= 0; i--) for (int i = _loaderList.Count - 1; i >= 0; i--)
{ {
BundleLoaderBase loader = _loaders[i]; BundleLoaderBase loader = _loaderList[i];
loader.TryDestroyAllProviders(); loader.TryDestroyAllProviders();
} }
for (int i = _loaders.Count - 1; i >= 0; i--) for (int i = _loaderList.Count - 1; i >= 0; i--)
{ {
BundleLoaderBase loader = _loaders[i]; BundleLoaderBase loader = _loaderList[i];
if (loader.CanDestroy()) if (loader.CanDestroy())
{ {
string bundleName = loader.MainBundleInfo.Bundle.BundleName;
loader.Destroy(false); loader.Destroy(false);
_loaders.RemoveAt(i); _loaderList.RemoveAt(i);
_loaderDic.Remove(bundleName);
} }
} }
} }
@ -137,17 +156,19 @@ namespace YooAsset
/// </summary> /// </summary>
public void ForceUnloadAllAssets() public void ForceUnloadAllAssets()
{ {
foreach (var provider in _providers) foreach (var provider in _providerList)
{ {
provider.Destroy(); provider.Destroy();
} }
foreach (var loader in _loaders) foreach (var loader in _loaderList)
{ {
loader.Destroy(true); loader.Destroy(true);
} }
_providers.Clear(); _providerList.Clear();
_loaders.Clear(); _providerDic.Clear();
_loaderList.Clear();
_loaderDic.Clear();
ClearSceneHandle(); ClearSceneHandle();
// 注意:调用底层接口释放所有资源 // 注意:调用底层接口释放所有资源
@ -182,7 +203,8 @@ namespace YooAsset
else else
provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, activateOnLoad, priority); provider = new BundledSceneProvider(this, providerGUID, assetInfo, sceneMode, activateOnLoad, priority);
provider.InitSpawnDebugInfo(); provider.InitSpawnDebugInfo();
_providers.Add(provider); _providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
} }
var handle = provider.CreateHandle<SceneOperationHandle>(); var handle = provider.CreateHandle<SceneOperationHandle>();
@ -213,7 +235,8 @@ namespace YooAsset
else else
provider = new BundledAssetProvider(this, providerGUID, assetInfo); provider = new BundledAssetProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo(); provider.InitSpawnDebugInfo();
_providers.Add(provider); _providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
} }
return provider.CreateHandle<AssetOperationHandle>(); return provider.CreateHandle<AssetOperationHandle>();
} }
@ -240,7 +263,8 @@ namespace YooAsset
else else
provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo); provider = new BundledSubAssetsProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo(); provider.InitSpawnDebugInfo();
_providers.Add(provider); _providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
} }
return provider.CreateHandle<SubAssetsOperationHandle>(); return provider.CreateHandle<SubAssetsOperationHandle>();
} }
@ -267,7 +291,8 @@ namespace YooAsset
else else
provider = new BundledRawFileProvider(this, providerGUID, assetInfo); provider = new BundledRawFileProvider(this, providerGUID, assetInfo);
provider.InitSpawnDebugInfo(); provider.InitSpawnDebugInfo();
_providers.Add(provider); _providerList.Add(provider);
_providerDic.Add(providerGUID, provider);
} }
return provider.CreateHandle<RawFileOperationHandle>(); return provider.CreateHandle<RawFileOperationHandle>();
} }
@ -338,7 +363,8 @@ namespace YooAsset
{ {
foreach (var provider in providers) foreach (var provider in providers)
{ {
_providers.Remove(provider); _providerList.Remove(provider);
_providerDic.Remove(provider.ProviderGUID);
} }
} }
internal bool CheckBundleDestroyed(int bundleID) internal bool CheckBundleDestroyed(int bundleID)
@ -353,7 +379,8 @@ namespace YooAsset
private BundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo) private BundleLoaderBase CreateAssetBundleLoaderInternal(BundleInfo bundleInfo)
{ {
// 如果加载器已经存在 // 如果加载器已经存在
BundleLoaderBase loader = TryGetAssetBundleLoader(bundleInfo.Bundle.BundleName); string bundleName = bundleInfo.Bundle.BundleName;
BundleLoaderBase loader = TryGetAssetBundleLoader(bundleName);
if (loader != null) if (loader != null)
return loader; return loader;
@ -370,43 +397,30 @@ namespace YooAsset
loader = new AssetBundleFileLoader(this, bundleInfo); loader = new AssetBundleFileLoader(this, bundleInfo);
#endif #endif
_loaders.Add(loader); _loaderList.Add(loader);
_loaderDic.Add(bundleName, loader);
return loader; return loader;
} }
private BundleLoaderBase TryGetAssetBundleLoader(string bundleName) private BundleLoaderBase TryGetAssetBundleLoader(string bundleName)
{ {
BundleLoaderBase loader = null; if (_loaderDic.TryGetValue(bundleName, out BundleLoaderBase value))
for (int i = 0; i < _loaders.Count; i++) return value;
{ else
BundleLoaderBase temp = _loaders[i]; return null;
if (temp.MainBundleInfo.Bundle.BundleName.Equals(bundleName))
{
loader = temp;
break;
}
}
return loader;
} }
private ProviderBase TryGetProvider(string providerGUID) private ProviderBase TryGetProvider(string providerGUID)
{ {
ProviderBase provider = null; if (_providerDic.TryGetValue(providerGUID, out ProviderBase value))
for (int i = 0; i < _providers.Count; i++) return value;
{ else
ProviderBase temp = _providers[i]; return null;
if (temp.ProviderGUID.Equals(providerGUID))
{
provider = temp;
break;
}
}
return provider;
} }
#region 调试信息 #region 调试信息
internal List<DebugProviderInfo> GetDebugReportInfos() internal List<DebugProviderInfo> GetDebugReportInfos()
{ {
List<DebugProviderInfo> result = new List<DebugProviderInfo>(_providers.Count); List<DebugProviderInfo> result = new List<DebugProviderInfo>(_providerList.Count);
foreach (var provider in _providers) foreach (var provider in _providerList)
{ {
DebugProviderInfo providerInfo = new DebugProviderInfo(); DebugProviderInfo providerInfo = new DebugProviderInfo();
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath; providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;
@ -429,9 +443,9 @@ namespace YooAsset
internal List<BundleInfo> GetLoadedBundleInfos() internal List<BundleInfo> GetLoadedBundleInfos()
{ {
List<BundleInfo> result = new List<BundleInfo>(100); List<BundleInfo> result = new List<BundleInfo>(100);
foreach (var bundleLoader in _loaders) foreach (var loader in _loaderList)
{ {
result.Add(bundleLoader.MainBundleInfo); result.Add(loader.MainBundleInfo);
} }
return result; return result;
} }