diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs index 2833d28e..d72b0dd9 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/IResourceManager.cs @@ -165,9 +165,10 @@ namespace TEngine /// /// 资源的定位地址。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - T LoadAsset(string location, bool needInstance) where T : Object; + T LoadAsset(string location, bool needInstance, bool needCache = false) where T : Object; /// /// 同步加载资源。 @@ -175,18 +176,20 @@ namespace TEngine /// 资源的定位地址。 /// 父节点位置。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - T LoadAsset(string location, Transform parent, bool needInstance) where T : Object; + T LoadAsset(string location, Transform parent, bool needInstance, bool needCache = false) where T : Object; /// /// 同步加载资源。 /// /// 资源操作句柄。 /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - T LoadAsset(string location, out AssetOperationHandle handle) where T : Object; + T LoadAsset(string location, out AssetOperationHandle handle, bool needCache = false) where T : Object; /// /// 同步加载资源。 @@ -194,25 +197,28 @@ namespace TEngine /// 资源的定位地址。 /// 资源操作句柄。 /// 父节点位置。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - T LoadAsset(string location, Transform parent, out AssetOperationHandle handle) where T : Object; + T LoadAsset(string location, Transform parent, out AssetOperationHandle handle, bool needCache = false) where T : Object; /// /// 同步加载资源并获取句柄。 /// /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 同步加载资源句柄。 - AssetOperationHandle LoadAssetGetOperation(string location) where T : Object; + AssetOperationHandle LoadAssetGetOperation(string location, bool needCache = false) where T : Object; /// /// 异步加载资源并获取句柄。 /// /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 异步加载资源句柄。 - AssetOperationHandle LoadAssetAsyncHandle(string location) where T : Object; + AssetOperationHandle LoadAssetAsyncHandle(string location, bool needCache = false) where T : Object; /// /// 同步加载子资源对象。 @@ -269,17 +275,19 @@ namespace TEngine /// 资源定位地址。 /// 取消操作Token。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 异步资源实例。 - UniTask LoadAssetAsync(string location, CancellationToken cancellationToken = default, bool needInstance = true) where T : Object; + UniTask LoadAssetAsync(string location, CancellationToken cancellationToken = default, bool needInstance = true, bool needCache = false) where T : Object; /// /// 异步加载游戏物体。 /// /// 资源定位地址。 /// 取消操作Token。 + /// 是否需要缓存。 /// 异步游戏物体实例。 - UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default); + UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, bool needCache = false); /// /// 异步加载游戏物体。 @@ -287,8 +295,9 @@ namespace TEngine /// 资源定位地址。 /// 父节点位置。 /// 取消操作Token。 + /// 是否需要缓存。 /// 异步游戏物体实例。 - UniTask LoadGameObjectAsync(string location, Transform parent, CancellationToken cancellationToken = default); + UniTask LoadGameObjectAsync(string location, Transform parent, CancellationToken cancellationToken = default, bool needCache = false); /// /// 异步加载原生文件。 diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs index 12543095..0ebdc1f1 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceManager.cs @@ -204,10 +204,15 @@ namespace TEngine /// 从缓存中获取同步资源句柄。 /// /// 资源定位地址。 + /// 是否需要缓存。 /// 资源类型。 /// 资源句柄。 - private AssetOperationHandle GetHandleSync(string location) where T : Object + private AssetOperationHandle GetHandleSync(string location, bool needCache = false) where T : Object { + if (!needCache) + { + return YooAssets.LoadAssetSync(location); + } AssetOperationHandle handle = null; // 尝试从从ARC缓存表取出对象。 handle = _arcCacheTable.GetCache(location); @@ -226,10 +231,15 @@ namespace TEngine /// 从缓存中获取异步资源句柄。 /// /// 资源定位地址。 + /// 是否需要缓存。 /// 资源类型。 /// 资源句柄。 - private AssetOperationHandle GetHandleAsync(string location) where T : Object + private AssetOperationHandle GetHandleAsync(string location, bool needCache = false) where T : Object { + if (!needCache) + { + return YooAssets.LoadAssetAsync(location); + } AssetOperationHandle handle = null; // 尝试从从ARC缓存表取出对象。 handle = _arcCacheTable.GetCache(location); @@ -499,9 +509,10 @@ namespace TEngine /// /// 资源的定位地址。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, bool needInstance = true) where T : Object + public T LoadAsset(string location, bool needInstance = true, bool needCache = false) where T : Object { if (string.IsNullOrEmpty(location)) { @@ -509,23 +520,27 @@ namespace TEngine return default; } - AssetOperationHandle handle = GetHandleSync(location); + AssetOperationHandle handle = GetHandleSync(location, needCache); if (typeof(T) == typeof(GameObject)) { if (needInstance) { - GameObject ret = handle.InstantiateSync(); - return ret as T; + GameObject gameObject = handle.InstantiateSync(); + if (!needCache) + { + AssetReference.BindAssetReference(gameObject, handle, location); + } + return gameObject as T; } - - return handle.AssetObject as T; } - else + + T ret = handle.AssetObject as T; + if (!needCache) { - T ret = handle.AssetObject as T; - return ret; + handle.Dispose(); } + return ret; } /// @@ -534,9 +549,10 @@ namespace TEngine /// 资源的定位地址。 /// 父节点位置。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, Transform parent, bool needInstance = true) where T : Object + public T LoadAsset(string location, Transform parent, bool needInstance = true, bool needCache = false) where T : Object { if (string.IsNullOrEmpty(location)) { @@ -544,23 +560,27 @@ namespace TEngine return default; } - AssetOperationHandle handle = GetHandleSync(location); + AssetOperationHandle handle = GetHandleSync(location, needCache); if (typeof(T) == typeof(GameObject)) { if (needInstance) { - GameObject ret = handle.InstantiateSync(parent); - return ret as T; + GameObject gameObject = handle.InstantiateSync(parent); + if (!needCache) + { + AssetReference.BindAssetReference(gameObject, handle, location); + } + return gameObject as T; } - - return handle.AssetObject as T; } - else + + T ret = handle.AssetObject as T; + if (!needCache) { - T ret = handle.AssetObject as T; - return ret; + handle.Dispose(); } + return ret; } /// @@ -568,11 +588,12 @@ namespace TEngine /// /// 资源操作句柄。 /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, out AssetOperationHandle handle) where T : Object + public T LoadAsset(string location, out AssetOperationHandle handle, bool needCache = false) where T : Object { - handle = GetHandleSync(location); + handle = GetHandleSync(location, needCache); if (string.IsNullOrEmpty(location)) { Log.Error("Asset name is invalid."); @@ -581,13 +602,20 @@ namespace TEngine if (typeof(T) == typeof(GameObject)) { - GameObject ret = handle.InstantiateSync(); - return ret as T; + GameObject gameObject = handle.InstantiateSync(); + if (!needCache) + { + AssetReference.BindAssetReference(gameObject, handle, location); + } + return gameObject as T; } - else + + T ret = handle.AssetObject as T; + if (!needCache) { - return handle.AssetObject as T; + handle.Dispose(); } + return ret; } /// @@ -595,12 +623,13 @@ namespace TEngine /// /// 资源的定位地址。 /// 资源操作句柄。 + /// 是否需要缓存。 /// 父节点位置。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, Transform parent, out AssetOperationHandle handle) where T : Object + public T LoadAsset(string location, Transform parent, out AssetOperationHandle handle, bool needCache = false) where T : Object { - handle = GetHandleSync(location); + handle = GetHandleSync(location, needCache); if (string.IsNullOrEmpty(location)) { @@ -610,35 +639,44 @@ namespace TEngine if (typeof(T) == typeof(GameObject)) { - GameObject ret = handle.InstantiateSync(parent); - return ret as T; + GameObject gameObject = handle.InstantiateSync(parent); + if (!needCache) + { + AssetReference.BindAssetReference(gameObject, handle, location); + } + return gameObject as T; } - else + + T ret = handle.AssetObject as T; + if (!needCache) { - return handle.AssetObject as T; + handle.Dispose(); } + return ret; } /// /// 同步加载资源并获取句柄。 /// /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 同步加载资源句柄。 - public AssetOperationHandle LoadAssetGetOperation(string location) where T : Object + public AssetOperationHandle LoadAssetGetOperation(string location, bool needCache = false) where T : Object { - return GetHandleSync(location); + return GetHandleSync(location, needCache); } /// /// 异步加载资源并获取句柄。 /// /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 异步加载资源句柄。 - public AssetOperationHandle LoadAssetAsyncHandle(string location) where T : Object + public AssetOperationHandle LoadAssetAsyncHandle(string location, bool needCache = false) where T : Object { - return GetHandleAsync(location); + return GetHandleAsync(location, needCache); } /// @@ -721,10 +759,11 @@ namespace TEngine /// 要加载的实例名称。 /// 取消操作Token。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 资源实实例。 - public async UniTask LoadAssetAsync(string location, CancellationToken cancellationToken = default, bool needInstance = true) where T : Object + public async UniTask LoadAssetAsync(string location, CancellationToken cancellationToken = default, bool needInstance = true, bool needCache = false) where T : Object { - AssetOperationHandle handle = LoadAssetAsyncHandle(location); + AssetOperationHandle handle = LoadAssetAsyncHandle(location, needCache); bool cancelOrFailed = await handle.ToUniTask().AttachExternalCancellation(cancellationToken).SuppressCancellationThrow(); @@ -737,16 +776,21 @@ namespace TEngine { if (needInstance) { - GameObject ret = handle.InstantiateSync(); - return ret as T; + GameObject gameObject = handle.InstantiateSync(); + if (!needCache) + { + AssetReference.BindAssetReference(gameObject, handle, location); + } + return gameObject as T; } - - return handle.AssetObject as T; } - else + + T ret = handle.AssetObject as T; + if (!needCache) { - return handle.AssetObject as T; + handle.Dispose(); } + return ret; } /// @@ -754,10 +798,11 @@ namespace TEngine /// /// 要加载的游戏物体名称。 /// 取消操作Token。 + /// 是否需要缓存。 /// 异步游戏物体实例。 - public async UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default) + public async UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, bool needCache = false) { - AssetOperationHandle handle = LoadAssetAsyncHandle(location); + AssetOperationHandle handle = LoadAssetAsyncHandle(location, needCache); bool cancelOrFailed = await handle.ToUniTask().AttachExternalCancellation(cancellationToken).SuppressCancellationThrow(); @@ -766,9 +811,12 @@ namespace TEngine return null; } - GameObject ret = handle.InstantiateSync(); - - return ret; + GameObject gameObject = handle.InstantiateSync(); + if (!needCache) + { + AssetReference.BindAssetReference(gameObject, handle, location); + } + return gameObject; } /// @@ -777,10 +825,11 @@ namespace TEngine /// 资源定位地址。 /// 父节点位置。 /// 取消操作Token。 + /// 是否需要缓存。 /// 异步游戏物体实例。 - public async UniTask LoadGameObjectAsync(string location, Transform parent, CancellationToken cancellationToken = default) + public async UniTask LoadGameObjectAsync(string location, Transform parent, CancellationToken cancellationToken = default, bool needCache = false) { - GameObject gameObject = await LoadGameObjectAsync(location, cancellationToken); + GameObject gameObject = await LoadGameObjectAsync(location, cancellationToken, needCache); if (parent != null) { gameObject.transform.SetParent(parent); diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs index 8a83b9a3..1e729861 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/ResourceModule/ResourceModule.cs @@ -449,11 +449,12 @@ namespace TEngine /// /// 资源的定位地址。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, bool needInstance = true) where T : UnityEngine.Object + public T LoadAsset(string location, bool needInstance = true, bool needCache = false) where T : UnityEngine.Object { - return m_ResourceManager.LoadAsset(location, needInstance); + return m_ResourceManager.LoadAsset(location, needInstance, needCache); } /// @@ -462,11 +463,12 @@ namespace TEngine /// 资源的定位地址。 /// 父节点位置。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, Transform parent, bool needInstance = true) where T : UnityEngine.Object + public T LoadAsset(string location, Transform parent, bool needInstance = true, bool needCache = false) where T : UnityEngine.Object { - return m_ResourceManager.LoadAsset(location, parent, needInstance); + return m_ResourceManager.LoadAsset(location, parent, needInstance, needCache); } /// @@ -474,11 +476,12 @@ namespace TEngine /// /// 资源操作句柄。 /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, out AssetOperationHandle handle) where T : UnityEngine.Object + public T LoadAsset(string location, out AssetOperationHandle handle, bool needCache = false) where T : UnityEngine.Object { - return m_ResourceManager.LoadAsset(location, out handle); + return m_ResourceManager.LoadAsset(location, out handle, needCache); } /// @@ -487,11 +490,12 @@ namespace TEngine /// 资源的定位地址。 /// 资源操作句柄。 /// 父节点位置。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 资源实例。 - public T LoadAsset(string location, Transform parent, out AssetOperationHandle handle) where T : UnityEngine.Object + public T LoadAsset(string location, Transform parent, out AssetOperationHandle handle, bool needCache = false) where T : UnityEngine.Object { - return m_ResourceManager.LoadAsset(location, parent, out handle); + return m_ResourceManager.LoadAsset(location, parent, out handle, needCache); } /// @@ -499,10 +503,11 @@ namespace TEngine /// /// 资源的定位地址。 /// 回调函数。 + /// 是否需要缓存。 /// 要加载资源的类型。 - public void LoadAssetAsync(string location, Action callback = null) where T : UnityEngine.Object + public void LoadAssetAsync(string location, Action callback = null, bool needCache = false) where T : UnityEngine.Object { - AssetOperationHandle handle = m_ResourceManager.LoadAssetAsyncHandle(location); + AssetOperationHandle handle = m_ResourceManager.LoadAssetAsyncHandle(location, needCache); handle.Completed += callback; } @@ -511,22 +516,24 @@ namespace TEngine /// 同步加载资源并获取句柄。 /// /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 同步加载资源句柄。 - public AssetOperationHandle LoadAssetGetOperation(string location) where T : UnityEngine.Object + public AssetOperationHandle LoadAssetGetOperation(string location, bool needCache = false) where T : UnityEngine.Object { - return m_ResourceManager.LoadAssetGetOperation(location); + return m_ResourceManager.LoadAssetGetOperation(location, needCache); } /// /// 异步加载资源并获取句柄。 /// /// 资源的定位地址。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 异步加载资源句柄。 - public AssetOperationHandle LoadAssetAsyncHandle(string location) where T : UnityEngine.Object + public AssetOperationHandle LoadAssetAsyncHandle(string location, bool needCache = false) where T : UnityEngine.Object { - return m_ResourceManager.LoadAssetAsyncHandle(location); + return m_ResourceManager.LoadAssetAsyncHandle(location, needCache); } @@ -611,11 +618,12 @@ namespace TEngine /// 资源的定位地址。 /// 取消操作Token。 /// 是否需要实例化。 + /// 是否需要缓存。 /// 要加载资源的类型。 /// 异步资源实例。 - public async UniTask LoadAssetAsync(string location, CancellationToken cancellationToken = default, bool needInstance = true) where T : UnityEngine.Object + public async UniTask LoadAssetAsync(string location, CancellationToken cancellationToken = default, bool needInstance = true, bool needCache = false) where T : UnityEngine.Object { - return await m_ResourceManager.LoadAssetAsync(location, cancellationToken, needInstance); + return await m_ResourceManager.LoadAssetAsync(location, cancellationToken, needInstance, needCache); } /// @@ -623,10 +631,11 @@ namespace TEngine /// /// 要加载的游戏物体名称。 /// 取消操作Token。 + /// 是否需要缓存。 /// 异步游戏物体实例。 - public async UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default) + public async UniTask LoadGameObjectAsync(string location, CancellationToken cancellationToken = default, bool needCache = false) { - return await m_ResourceManager.LoadGameObjectAsync(location, cancellationToken); + return await m_ResourceManager.LoadGameObjectAsync(location, cancellationToken, needCache); } /// @@ -635,10 +644,11 @@ namespace TEngine /// 资源定位地址。 /// 父节点位置。 /// 取消操作Token。 + /// 是否需要缓存。 /// 异步游戏物体实例。 - public async UniTask LoadGameObjectAsync(string location, Transform parent, CancellationToken cancellationToken = default) + public async UniTask LoadGameObjectAsync(string location, Transform parent, CancellationToken cancellationToken = default, bool needCache = false) { - return await m_ResourceManager.LoadGameObjectAsync(location, parent, cancellationToken); + return await m_ResourceManager.LoadGameObjectAsync(location, parent, cancellationToken, needCache); } /// diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs index ce312a11..ca04cfaf 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs @@ -400,7 +400,7 @@ namespace TEngine throw new Exception($"Window {type.FullName} not found {nameof(WindowAttribute)} attribute."); string assetName = string.IsNullOrEmpty(attribute.Location) ? type.Name : attribute.Location; - window.Init(type.FullName, attribute.WindowLayer, attribute.FullScreen, assetName, attribute.FromResources); + window.Init(type.FullName, attribute.WindowLayer, attribute.FullScreen, assetName, attribute.FromResources, attribute.NeedCache); return window; } diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIWindow.cs b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIWindow.cs index fdf0e8a0..5e632ec9 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIWindow.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIWindow.cs @@ -66,6 +66,11 @@ namespace TEngine /// 是内部资源无需AB加载。 /// public bool FromResources { private set; get; } + + /// + /// 是否需要缓存。 + /// + public bool NeedCache { private set; get; } /// /// 自定义数据。 @@ -221,13 +226,14 @@ namespace TEngine #endregion - public void Init(string name, int layer, bool fullScreen, string assetName, bool fromResources) + public void Init(string name, int layer, bool fullScreen, string assetName, bool fromResources, bool needCache = false) { WindowName = name; WindowLayer = layer; FullScreen = fullScreen; AssetName = assetName; FromResources = fromResources; + NeedCache = needCache; } internal void TryInvoke(System.Action prepareCallback, System.Object[] userDatas) @@ -249,7 +255,7 @@ namespace TEngine this.userDatas = userDatas; if (!FromResources) { - GameModule.Resource.LoadAssetAsync(location, Handle_Completed); + GameModule.Resource.LoadAssetAsync(location, Handle_Completed, needCache: NeedCache); } else { @@ -392,6 +398,10 @@ namespace TEngine // 实例化对象 var panel = handle.InstantiateSync(UIModule.UIRootStatic); + if (!NeedCache) + { + AssetReference.BindAssetReference(panel, handle, AssetName); + } Handle_Completed(panel); } diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/WindowAttribute.cs b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/WindowAttribute.cs index e3a40a3b..da039f21 100644 --- a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/WindowAttribute.cs +++ b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/WindowAttribute.cs @@ -37,31 +37,41 @@ namespace TEngine /// public readonly bool FromResources; - public WindowAttribute(int windowLayer, string location = "", bool fullScreen = false) + /// + /// 需要缓存。 + /// 关闭界面不立刻释放资源。 + /// + public readonly bool NeedCache; + + public WindowAttribute(int windowLayer, string location = "", bool fullScreen = false,bool needCache = false) { WindowLayer = windowLayer; Location = location; FullScreen = fullScreen; + NeedCache = needCache; } - public WindowAttribute(UILayer windowLayer, string location = "", bool fullScreen = false) + public WindowAttribute(UILayer windowLayer, string location = "", bool fullScreen = false,bool needCache = false) { WindowLayer = (int)windowLayer; Location = location; FullScreen = fullScreen; + NeedCache = needCache; } - public WindowAttribute(UILayer windowLayer, bool fromResources, bool fullScreen = false) + public WindowAttribute(UILayer windowLayer, bool fromResources, bool fullScreen = false,bool needCache = false) { WindowLayer = (int)windowLayer; FromResources = fromResources; + NeedCache = needCache; } - public WindowAttribute(UILayer windowLayer, bool fromResources, string location, bool fullScreen = false) + public WindowAttribute(UILayer windowLayer, bool fromResources, string location, bool fullScreen = false,bool needCache = false) { WindowLayer = (int)windowLayer; FromResources = fromResources; Location = location; + NeedCache = needCache; } } } \ No newline at end of file