Update asset system

支持场景文件跨资源包配置。
pull/51/head
何冠峰 2022-11-16 16:25:43 +08:00
parent d5f601b0eb
commit c00594738f
2 changed files with 28 additions and 24 deletions

View File

@ -8,13 +8,11 @@ namespace YooAsset
{ {
internal class AssetSystemImpl internal class AssetSystemImpl
{ {
private static string SceneRunningPackage = string.Empty;
private readonly List<AssetBundleLoaderBase> _loaders = new List<AssetBundleLoaderBase>(1000); private readonly List<AssetBundleLoaderBase> _loaders = new List<AssetBundleLoaderBase>(1000);
private readonly List<ProviderBase> _providers = new List<ProviderBase>(1000); private readonly List<ProviderBase> _providers = new List<ProviderBase>(1000);
private readonly 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 long _sceneCreateCount = 0;
private bool _simulationOnEditor; private bool _simulationOnEditor;
private int _loadingMaxNumber; private int _loadingMaxNumber;
public IDecryptionServices DecryptionServices { private set; get; } public IDecryptionServices DecryptionServices { private set; get; }
@ -71,9 +69,9 @@ namespace YooAsset
/// </summary> /// </summary>
public void DestroyAll() public void DestroyAll()
{ {
_loaders.Clear();
_providers.Clear(); _providers.Clear();
_sceneHandles.Clear(); _loaders.Clear();
ClearSceneHandle();
DecryptionServices = null; DecryptionServices = null;
BundleServices = null; BundleServices = null;
@ -123,15 +121,14 @@ namespace YooAsset
{ {
provider.Destroy(); provider.Destroy();
} }
_providers.Clear();
foreach (var loader in _loaders) foreach (var loader in _loaders)
{ {
loader.Destroy(true); loader.Destroy(true);
} }
_loaders.Clear();
_sceneHandles.Clear(); _providers.Clear();
_loaders.Clear();
ClearSceneHandle();
// 注意:调用底层接口释放所有资源 // 注意:调用底层接口释放所有资源
Resources.UnloadUnusedAssets(); Resources.UnloadUnusedAssets();
@ -150,19 +147,6 @@ namespace YooAsset
return completedProvider.CreateHandle<SceneOperationHandle>(); return completedProvider.CreateHandle<SceneOperationHandle>();
} }
// 注意:场景只允许运行在一个资源包内
if (string.IsNullOrEmpty(SceneRunningPackage))
{
SceneRunningPackage = BundleServices.GetPackageName();
}
if (BundleServices.GetPackageName() != SceneRunningPackage)
{
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
string error = $"Scene are allowed to running within only {SceneRunningPackage}";
completedProvider.SetCompleted(error);
return completedProvider.CreateHandle<SceneOperationHandle>();
}
// 如果加载的是主场景,则卸载所有缓存的场景 // 如果加载的是主场景,则卸载所有缓存的场景
if (sceneMode == LoadSceneMode.Single) if (sceneMode == LoadSceneMode.Single)
{ {
@ -182,6 +166,7 @@ namespace YooAsset
} }
var handle = provider.CreateHandle<SceneOperationHandle>(); var handle = provider.CreateHandle<SceneOperationHandle>();
handle.PackageName = BundleServices.GetPackageName();
_sceneHandles.Add(providerGUID, handle); _sceneHandles.Add(providerGUID, handle);
return handle; return handle;
} }
@ -265,6 +250,24 @@ namespace YooAsset
// 卸载未被使用的资源(包括场景) // 卸载未被使用的资源(包括场景)
UnloadUnusedAssets(); UnloadUnusedAssets();
} }
internal void ClearSceneHandle()
{
// 释放资源包下的所有场景
string packageName = BundleServices.GetPackageName();
List<string> removeList = new List<string>();
foreach (var valuePair in _sceneHandles)
{
if (valuePair.Value.PackageName == packageName)
{
removeList.Add(valuePair.Key);
}
}
foreach (var key in removeList)
{
_sceneHandles.Remove(key);
}
}
internal AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo) internal AssetBundleLoaderBase CreateOwnerAssetBundleLoader(AssetInfo assetInfo)
{ {

View File

@ -5,6 +5,7 @@ namespace YooAsset
public class SceneOperationHandle : OperationHandleBase public class SceneOperationHandle : OperationHandleBase
{ {
private System.Action<SceneOperationHandle> _callback; private System.Action<SceneOperationHandle> _callback;
internal string PackageName { set; get; }
internal SceneOperationHandle(ProviderBase provider) : base(provider) internal SceneOperationHandle(ProviderBase provider) : base(provider)
{ {