基于资源框架实现对标签WEBGL_PRELOAD、PRELOAD的预加载。 接口GameModule.Resource.GetPreLoadAsset<T>(location)

基于资源框架实现对标签WEBGL_PRELOAD、PRELOAD的预加载。 接口GameModule.Resource.GetPreLoadAsset<T>(location)
This commit is contained in:
ALEXTANG
2023-11-02 13:16:05 +08:00
parent 04bfaeccc8
commit 8e9047d3a3
6 changed files with 133 additions and 23 deletions

View File

@@ -42,8 +42,16 @@ public class ConfigSystem : Singleton<ConfigSystem>
/// <returns>ByteBuf</returns>
private ByteBuf LoadByteBuf(string file)
{
var textAssets = GameModule.Resource.LoadAsset<TextAsset>(file);
byte[] ret = textAssets.bytes;
return new ByteBuf(ret);
TextAsset textAsset = null;
textAsset = GameModule.Resource.GetPreLoadAsset<TextAsset>(file);
if (textAsset != null)
{
return new ByteBuf(textAsset.bytes);
}
else
{
textAsset = GameModule.Resource.LoadAsset<TextAsset>(file);
return new ByteBuf(textAsset.bytes);
}
}
}

View File

@@ -16,23 +16,23 @@ namespace GameMain
{
private float _progress = 0f;
private Dictionary<string, bool> m_LoadedFlag = new Dictionary<string, bool>();
private readonly Dictionary<string, bool> _loadedFlag = new Dictionary<string, bool>();
public override bool UseNativeDialog => true;
private bool m_needProLoadConfig = false;
private readonly bool _needProLoadConfig = true;
private bool m_InitConfigXml = false;
private bool _hadInitConfigXml = false;
protected override void OnEnter(ProcedureOwner procedureOwner)
{
base.OnEnter(procedureOwner);
m_LoadedFlag.Clear();
_loadedFlag.Clear();
if (GameModule.Resource.PlayMode == EPlayMode.EditorSimulateMode)
{
m_InitConfigXml = true;
_hadInitConfigXml = true;
}
UILoadMgr.Show(UIDefine.UILoadUpdate, Utility.Text.Format(LoadText.Instance.Label_Load_Load_Progress, 0));
@@ -46,11 +46,11 @@ namespace GameMain
{
base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
var totalCount = m_LoadedFlag.Count <= 0 ? 1 : m_LoadedFlag.Count;
var totalCount = _loadedFlag.Count <= 0 ? 1 : _loadedFlag.Count;
var loadCount = m_LoadedFlag.Count <= 0 ? 1 : 0;
var loadCount = _loadedFlag.Count <= 0 ? 1 : 0;
foreach (KeyValuePair<string, bool> loadedFlag in m_LoadedFlag)
foreach (KeyValuePair<string, bool> loadedFlag in _loadedFlag)
{
if (!loadedFlag.Value)
{
@@ -62,7 +62,7 @@ namespace GameMain
}
}
if (m_LoadedFlag.Count != 0)
if (_loadedFlag.Count != 0)
{
UILoadMgr.Show(UIDefine.UILoadUpdate, Utility.Text.Format(LoadText.Instance.Label_Load_Load_Progress, (float)loadCount / totalCount * 100));
}
@@ -87,7 +87,7 @@ namespace GameMain
return;
}
if (m_InitConfigXml == false)
if (_hadInitConfigXml == false)
{
return;
}
@@ -116,13 +116,13 @@ namespace GameMain
await UniTask.Delay(TimeSpan.FromSeconds(2.5f));
if (m_needProLoadConfig)
if (_needProLoadConfig)
{
LoadAllConfig();
}
else
{
m_InitConfigXml = true;
_hadInitConfigXml = true;
}
}
@@ -130,14 +130,27 @@ namespace GameMain
{
if (GameModule.Resource.PlayMode == EPlayMode.EditorSimulateMode)
{
m_InitConfigXml = true;
_hadInitConfigXml = true;
return;
}
AssetInfo[] assetInfos = GameModule.Resource.GetAssetInfos("PRELOAD");
foreach (var assetInfo in assetInfos)
{
LoadConfig(assetInfo.Address);
}
#if UNITY_WEBGL
AssetInfo[] assetInfos = GameModule.Resource.GetAssetInfos("WEBGL_PRELOAD");
foreach (var assetInfo in assetInfos)
{
LoadConfig(assetInfo.Address);
}
#endif
_hadInitConfigXml = true;
}
private void LoadConfig(string configName)
{
m_LoadedFlag.Add(configName, false);
_loadedFlag.Add(configName, false);
GameModule.Resource.LoadAssetAsync<TextAsset>(configName, OnLoadSuccess);
}
@@ -147,10 +160,11 @@ namespace GameMain
{
return;
}
var name = assetOperationHandle.GetAssetInfo().Address;
m_LoadedFlag[name] = true;
Log.Info("Load config '{0}' OK.", name);
var location = assetOperationHandle.GetAssetInfo().Address;
_loadedFlag[location] = true;
GameModule.Resource.PushPreLoadAsset(location, assetOperationHandle.AssetObject);
Log.Info("Load config '{0}' OK.", location);
assetOperationHandle.Dispose();
}
}
}

View File

@@ -14,6 +14,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
ShowPackageView: 0
EnableAddressable: 1
LocationToLower: 0
IncludeAssetGUID: 0
UniqueBundleName: 0
ShowEditorAlias: 0
Packages:
@@ -78,7 +80,7 @@ MonoBehaviour:
AddressRuleName: AddressByFileName
PackRuleName: PackDirectory
FilterRuleName: CollectAll
AssetTags:
AssetTags: WEBGL_PRELOAD
UserData:
- GroupName: DLL
GroupDesc: "\u4EE3\u7801"

View File

@@ -304,5 +304,20 @@ namespace TEngine
/// <typeparam name="T">资源实例类型。</typeparam>
/// <returns>原生文件资源实例。</returns>
UniTask<T[]> LoadAllSubAssetAsync<T>(string location, CancellationToken cancellationToken = default) where T : Object;
/// <summary>
/// 放入预加载对象。
/// </summary>
/// <param name="location">资源定位地址。</param>
/// <param name="assetObject">预加载对象。</param>
public void PushPreLoadAsset(string location, Object assetObject);
/// <summary>
/// 获取预加载的实例对象。
/// </summary>
/// <param name="location">资源定位地址。</param>
/// <typeparam name="T">资源实例类型。</typeparam>
/// <returns>预加载对象。</returns>
public T GetPreLoadAsset<T>(string location) where T : Object;
}
}

View File

@@ -101,6 +101,7 @@ namespace TEngine
internal override void Shutdown()
{
ReleasePreLoadAssets();
#if !UNITY_WEBGL
YooAssets.Destroy();
#endif
@@ -878,5 +879,53 @@ namespace TEngine
return cancelOrFailed ? null : handle.GetSubAssetObjects<T>();
}
#region
private readonly Dictionary<string, Object> _preLoadMaps = new Dictionary<string, Object>();
/// <summary>
/// 放入预加载对象。
/// </summary>
/// <param name="location">资源定位地址。</param>
/// <param name="assetObject">预加载对象。</param>
public void PushPreLoadAsset(string location, Object assetObject)
{
if (_preLoadMaps.ContainsKey(location))
{
return;
}
_preLoadMaps.Add(location, assetObject);
}
/// <summary>
/// 获取预加载的实例对象。
/// </summary>
/// <param name="location">资源定位地址。</param>
/// <typeparam name="T">资源实例类型。</typeparam>
/// <returns>预加载对象。</returns>
public T GetPreLoadAsset<T>(string location) where T : Object
{
if (_preLoadMaps.TryGetValue(location,out Object assetObject))
{
return assetObject as T;
}
return default;
}
private void ReleasePreLoadAssets()
{
using var iter = _preLoadMaps.GetEnumerator();
while (iter.MoveNext())
{
var assetObject = iter.Current.Value;
if (assetObject != null)
{
UnityEngine.Object.Destroy(assetObject);
}
}
_preLoadMaps.Clear();
}
#endregion
}
}

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using YooAsset;
namespace TEngine
@@ -658,5 +657,28 @@ namespace TEngine
{
return await m_ResourceManager.LoadAllSubAssetAsync<T>(location, cancellationToken);
}
#region
/// <summary>
/// 放入预加载对象。
/// </summary>
/// <param name="location">资源定位地址。</param>
/// <param name="assetObject">预加载对象。</param>
public void PushPreLoadAsset(string location, UnityEngine.Object assetObject)
{
m_ResourceManager.PushPreLoadAsset(location, assetObject);
}
/// <summary>
/// 获取预加载的实例对象。
/// </summary>
/// <param name="location">资源定位地址。</param>
/// <typeparam name="T">资源实例类型。</typeparam>
/// <returns>预加载对象。</returns>
public T GetPreLoadAsset<T>(string location) where T : UnityEngine.Object
{
return m_ResourceManager.GetPreLoadAsset<T>(location);
}
#endregion
}
}