diff --git a/Configs/GameConfig/CustomTemplate/ConfigSystem.cs b/Configs/GameConfig/CustomTemplate/ConfigSystem.cs index ebf1ba6f..7bfc52ed 100644 --- a/Configs/GameConfig/CustomTemplate/ConfigSystem.cs +++ b/Configs/GameConfig/CustomTemplate/ConfigSystem.cs @@ -42,8 +42,16 @@ public class ConfigSystem : Singleton /// ByteBuf private ByteBuf LoadByteBuf(string file) { - var textAssets = GameModule.Resource.LoadAsset(file); - byte[] ret = textAssets.bytes; - return new ByteBuf(ret); + TextAsset textAsset = null; + textAsset = GameModule.Resource.GetPreLoadAsset(file); + if (textAsset != null) + { + return new ByteBuf(textAsset.bytes); + } + else + { + textAsset = GameModule.Resource.LoadAsset(file); + return new ByteBuf(textAsset.bytes); + } } } \ No newline at end of file diff --git a/UnityProject/Assets/GameScripts/Main/Procedure/ProcedurePreload.cs b/UnityProject/Assets/GameScripts/Main/Procedure/ProcedurePreload.cs index c7c3132b..7d65ed4b 100644 --- a/UnityProject/Assets/GameScripts/Main/Procedure/ProcedurePreload.cs +++ b/UnityProject/Assets/GameScripts/Main/Procedure/ProcedurePreload.cs @@ -16,23 +16,23 @@ namespace GameMain { private float _progress = 0f; - private Dictionary m_LoadedFlag = new Dictionary(); + private readonly Dictionary _loadedFlag = new Dictionary(); 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 loadedFlag in m_LoadedFlag) + foreach (KeyValuePair 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(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(); } } } \ No newline at end of file diff --git a/UnityProject/Assets/TEngine/AssetSetting/AssetBundleCollectorSetting.asset b/UnityProject/Assets/TEngine/AssetSetting/AssetBundleCollectorSetting.asset index eca189d4..2766729e 100644 --- a/UnityProject/Assets/TEngine/AssetSetting/AssetBundleCollectorSetting.asset +++ b/UnityProject/Assets/TEngine/AssetSetting/AssetBundleCollectorSetting.asset @@ -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" diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs index 7ca0723b..67a4d78b 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs @@ -304,5 +304,20 @@ namespace TEngine /// 资源实例类型。 /// 原生文件资源实例。 UniTask LoadAllSubAssetAsync(string location, CancellationToken cancellationToken = default) where T : Object; + + /// + /// 放入预加载对象。 + /// + /// 资源定位地址。 + /// 预加载对象。 + public void PushPreLoadAsset(string location, Object assetObject); + + /// + /// 获取预加载的实例对象。 + /// + /// 资源定位地址。 + /// 资源实例类型。 + /// 预加载对象。 + public T GetPreLoadAsset(string location) where T : Object; } } \ No newline at end of file diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs index 45a28b50..8e273267 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs @@ -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(); } + + #region 预加载 + + private readonly Dictionary _preLoadMaps = new Dictionary(); + + /// + /// 放入预加载对象。 + /// + /// 资源定位地址。 + /// 预加载对象。 + public void PushPreLoadAsset(string location, Object assetObject) + { + if (_preLoadMaps.ContainsKey(location)) + { + return; + } + _preLoadMaps.Add(location, assetObject); + } + + /// + /// 获取预加载的实例对象。 + /// + /// 资源定位地址。 + /// 资源实例类型。 + /// 预加载对象。 + public T GetPreLoadAsset(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 } } \ No newline at end of file diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs index ebaf84d6..9b4ed110 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs @@ -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(location, cancellationToken); } + + #region 预加载 + /// + /// 放入预加载对象。 + /// + /// 资源定位地址。 + /// 预加载对象。 + public void PushPreLoadAsset(string location, UnityEngine.Object assetObject) + { + m_ResourceManager.PushPreLoadAsset(location, assetObject); + } + + /// + /// 获取预加载的实例对象。 + /// + /// 资源定位地址。 + /// 资源实例类型。 + /// 预加载对象。 + public T GetPreLoadAsset(string location) where T : UnityEngine.Object + { + return m_ResourceManager.GetPreLoadAsset(location); + } + #endregion } } \ No newline at end of file