定义一个 LoaderManager class,用于管理各种资源加载器。它使用了对象池(Object Pool)来优化资源加载器的创建和销毁,从而提高性能
举例定义一个 PrefabLoader
--[[
Desc: 封装AAS的接口,加载Prefab
--]]
---@alias PrefabLoader.CompleteDelegate fun(obj:table, l:PrefabLoader) | fun(l:PrefabLoader)
---@class PrefabLoader : BaseLoader
---@field result CS.UnityEngine.GameObject
---@field callback PrefabLoader.CompleteDelegate
local PrefabLoader = SimpleClassUtil:class(require 'Topjoy.Engine.Resources.BaseLoader')
PrefabLoader.intType = CS.Topjoy.Base.ResourceManagement.ResourcesUtil.TypeInts.GameObject
---@overload fun(parent:CS.UnityEngine.Transform): CS.UnityEngine.GameObject | CS.UnityEngine.Object
---@overload fun(): CS.UnityEngine.GameObject | CS.UnityEngine.Object
---@param parent CS.UnityEngine.Transform
---@param worldStay boolean @默认false
---@return CS.UnityEngine.GameObject | CS.UnityEngine.Object
function PrefabLoader:instantiate(parent, worldStay)
if self.result then
if parent then
return CS.UnityEngine.GameObject.Instantiate(self.result, parent, worldStay==true)
else
return CS.UnityEngine.GameObject.Instantiate(self.result)
end
end
end
return PrefabLoader
设计思路:
- 引入了各种资源加载器,如
AssetLoader
、PrefabLoader
等,这些加载器负责加载不同类型的资源。 -
initialize
方法调用onInit
方法进行初始化。onInit
方法中,首先获取资源管理器实例,然后初始化各种资源加载器池,每个池子使用TablePool
来管理对象池。 -
PrefabLoader
为例,创建了一个对象池,池子大小为16。当需要新的PrefabLoader
实例时,会调用传入的函数创建一个新的PrefabLoader
对象,并设置其释放函数为self._assetPool:releaseObj(1) -
loadAssetAsync
方法用于异步加载资源。它从对象池中获取一个PrefabLoader
实例,初始化它,并调用其loadAsync
方法进行异步加载。 -
release
方法用于释放所有资源加载器池,并清理资源管理器实例。
注意事项
- 对象池的使用:通过对象池管理资源加载器,可以减少对象的频繁创建和销毁,提高性能。
- 异步加载:资源加载方法(如
loadAssetAsync
)都是异步的,可以避免阻塞主线程。 - 资源管理器:使用
CS.Topjoy.Base.ResourceManagement.ResourceManager
来管理资源加载,确保资源加载的一致性和效率。
---@class LoaderManager
local LoaderManager = SimpleClassUtil:class()
local AssetLoader = require 'Topjoy.Engine.Resources.AssetLoader'
local PrefabLoader = require 'Topjoy.Engine.Resources.PrefabLoader'
local MaterialLoader = require 'Topjoy.Engine.Resources.MaterialLoader'
local TextureLoader = require 'Topjoy.Engine.Resources.TextureLoader'
local TextAssetLoader = require 'Topjoy.Engine.Resources.TextAssetLoader'
local ListSpriteLoader = require 'Topjoy.Engine.Resources.ListSpriteLoader'
local MultiAssetLoaders = require 'Topjoy.Engine.Resources.MultiAssetLoaders'
local SceneLoader = require 'Topjoy.Engine.Resources.SceneLoader'
local WwiseBankLoader = require 'Topjoy.Engine.Resources.WwiseBankLoader'
local WalletLoader = require 'Topjoy.Engine.Resources.Wallet.LoaderWallet'
function LoaderManager:initialize()
self:onInit()
end
function LoaderManager:onInit()
self._resourceManager = CS.Topjoy.Base.ResourceManagement.ResourceManager.Instance
self._assetPool = TablePool:new(16, function()
local loader = AssetLoader:new()
loader.rlsFunc = function(l)
self._assetPool:releaseObj(l)
end
return loader
end)
self._prefabPool = TablePool:new(16, function()
local loader = PrefabLoader:new()
loader.rlsFunc = function(l)
self._prefabPool:releaseObj(l)
end
return loader
end)
self._materialPool = TablePool:new(16, function()
local loader = MaterialLoader:new()
loader.rlsFunc = function(l)
self._materialPool:releaseObj(l)
end
return loader
end)
self._texturePool = TablePool:new(16, function()
local loader = TextureLoader:new()
loader.rlsFunc = function(l)
self._texturePool:releaseObj(l)
end
return loader
end)
self._textAssetPool = TablePool:new(16, function()
local loader = TextAssetLoader:new()
loader.rlsFunc = function(l)
self._textAssetPool:releaseObj(l)
end
return loader
end)
self._listSpritePool = TablePool:new(16, function()
local loader = ListSpriteLoader:new()
loader.rlsFunc = function(l)
self._listSpritePool:releaseObj(l)
end
return loader
end)
self._multiAssetsPool = TablePool:new(16, function()
local loader = MultiAssetLoaders:new()
loader.rlsFunc = function(l)
self._multiAssetsPool:releaseObj(l)
end
return loader
end)
self._scenePool = TablePool:new(16, function()
local loader = SceneLoader:new()
loader.rlsFunc = function(l)
self._scenePool:releaseObj(l)
end
return loader
end)
self._wwiseBankPool = TablePool:new(4, function()
local loader = WwiseBankLoader:new()
loader.rlsFunc = function(l)
self._wwiseBankPool:releaseObj(l)
end
return loader
end)
self._walletPool = TablePool:new(16,function ()
local loader = WalletLoader:new()
loader.rlsFunc = function(l)
self._walletPool:releaseObj(l)
end
return loader
end)
end
function LoaderManager:release()
self._resourceManager = nil
Logger.print("LoaderManager:release")
self._assetPool:release()
self._prefabPool:release()
self._materialPool:release()
self._texturePool:release()
self._textAssetPool:release()
self._listSpritePool:release()
self._multiAssetsPool:release()
self._scenePool:release()
self._wwiseBankPool:release()
self._walletPool:release()
end
---@param path string
---@return AssetLoader
function LoaderManager:newAssetLoader(path)
local loader = self._assetPool:getObj()
loader:init(path)
return loader
end
---@param path string
---@param obj table @回调的self参数,可为nil
---@param data any @loader的附加参数,可通过loader.data获取
---@param callback AssetLoader.CompleteDelegate
---@return AssetLoader
function LoaderManager:loadAssetAsync(path, callback, obj, data)
---@type AssetLoader
local loader = self._assetPool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param path string
---@return PrefabLoader
function LoaderManager:newPrefabLoader(path)
local loader = self._prefabPool:getObj()
loader:init(path)
return loader
end
---@param path string
---@param obj table @回调的self参数,可为nil
---@param data any @loader的附加参数,可通过loader.data获取
---@param callback PrefabLoader.CompleteDelegate
---@return PrefabLoader
function LoaderManager:loadPrefabAsync(path, callback, obj, data)
---@type PrefabLoader
local loader = self._prefabPool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param path string
---@return MaterialLoader
function LoaderManager:newMaterialLoader(path)
local loader = self._materialPool:getObj()
loader:init(path)
return loader
end
---@param path string
---@param obj table @回调的self参数,可为nil
---@param data any @loader的附加参数,可通过loader.data获取
---@param callback MaterialLoader.CompleteDelegate
---@return MaterialLoader
function LoaderManager:loadMaterialAsync(path, callback, obj, data)
---@type MaterialLoader
local loader = self._materialPool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param path string
---@return TextureLoader
function LoaderManager:newTextureLoader(path)
local loader = self._texturePool:getObj()
loader:init(path)
return loader
end
---@param path string
---@param obj table @回调的self参数,可为nil
---@param data any @loader的附加参数,可通过loader.data获取
---@param callback TextureLoader.CompleteDelegate
---@return TextureLoader
function LoaderManager:loadTextureAsync(path, callback, obj, data)
---@type TextureLoader
local loader = self._texturePool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param path string
---@return TextAssetLoader
function LoaderManager:newTextAssetLoader(path)
local loader = self._textAssetPool:getObj()
loader:init(path)
return loader
end
---@param path string
---@param obj table @回调的self参数,可为nil
---@param data any @loader的附加参数,可通过loader.data获取
---@param callback TextAssetLoader.CompleteDelegate
---@return TextAssetLoader
function LoaderManager:loadTextAssetAsync(path, callback, obj, data)
---@type TextAssetLoader
local loader = self._textAssetPool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param path string
---@return ListSpriteLoader
function LoaderManager:newListSpriteLoader(path)
local loader = self._listSpritePool:getObj()
loader:init(path)
return loader
end
---@param path string
---@param obj table @回调的self参数,可为nil
---@param data any @loader的附加参数,可通过loader.data获取
---@param callback ListSpriteLoader.CompleteDelegate
---@return ListSpriteLoader
function LoaderManager:loadListSpriteAsync(path, callback, obj, data)
---@type ListSpriteLoader
local loader = self._listSpritePool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param callback MultiAssetLoaders.CompleteDelegate
---@return MultiAssetLoaders
function LoaderManager:loadMultiAssetAsync(callback, obj, data, ...)
---@type MultiAssetLoaders
local loader = self._multiAssetsPool:getObj()
loader:init(...)
loader:loadAsync(callback, obj, data)
return loader
end
---@param callback SceneLoader.CompleteDelegate
---@return SceneLoader
function LoaderManager:loadSceneAsync(path, callback, obj, data)
---@type SceneLoader
local loader = self._scenePool:getObj()
loader:init(path)
loader:loadAsync(callback, obj, data)
return loader
end
---@param callback WwiseBankLoader.CompleteDelegate
---@return WwiseBankLoader
function LoaderManager:newWwiseBankLoader(path)
---@type SceneLoader
local loader = self._wwiseBankPool:getObj()
loader:init(path)
return loader
end
---@param owner @传self
---@return LoaderWallet
function LoaderManager:newLoaderWallet(owner)
local loader = self._walletPool:getObj()
loader:setOwner(owner)
return loader
end
---@param maxExecute number @最大同时异步加载数量
---@param maxComplete number @最大同帧回调数量
---@return CS.Topjoy.Base.ResourceManagement.OperationHandles.OperationQueue
function LoaderManager:newLoaderQueue(maxExecute, maxComplete)
local queue = self._resourceManager:NewOperationQueue(maxExecute, maxComplete)
return queue
end
---@param queue CS.Topjoy.Base.ResourceManagement.ResourceManager
function LoaderManager:destroyLoaderQueue(queue)
self._resourceManager:DestroyOperationQueue(queue)
end
return LoaderManager