定义了一个名为`LoaderWallet` class,用于管理资源加载器(Loader)。这个类封装了资源加载的功能,包括异步加载,以及资源的释放和状态查询。下面是对代码的详细解释:
### 类定义和初始化
这里定义了一个名为`LoaderWallet`的类,并使用`SimpleClassUtil:class()`方法进行初始化。
定义 一个对象池:TablePool 类 lua 游戏架构 之 TablePool`对象池-CSDN博客https://blog.csdn.net/heyuchang666/article/details/140530648
定义了一个`cachePool`对象,用于缓存`LoaderWallet`的内部对象,减少内存分配和回收的开销。
local cachePool =TablePool:new(16,nil,function(t)for i, _ in pairs(t) dot[i] = nilendend
)
owner
是一个成员变量,用于存储LoaderWallet
的拥有者。这个拥有者可以是任何对象,通常是一个游戏对象或场景对象,用于管理资源加载。
function LoaderWallet:initialize(owner)self.owner = owner
endfunction LoaderWallet:setOwner(owner)self.owner = ownerself.loaders = cachePool:getObj()self.callbacks = cachePool:getObj()
end
释放所有内部加载器,并释放loaders
和callbacks
对象。如果LoaderWallet
已经被释放过,则输出错误日志。
function LoaderWallet:release()if not self.owner thenLogger.error("Try to Release LoaderWallet Twice. You should check if nil firstly.")returnendfor _, loader in pairs(self.loaders) doloader:release()endcachePool:releaseObj(self.loaders)cachePool:releaseObj(self.callbacks)self.owner = nilself.loaders = nilself.callbacks = nilif self.rlsFunc thenself.rlsFunc(self)end
end
设计方法 于查询加载器的状态,包括是否完成、加载进度、是否正在加载以及句柄是否有效。
- isComplete
- getProgress
- isLoading
- isValidHandle
设计方法用于标记资源是否需要卸载、释放加载器、获取加载完成的资源以及获取子资源
- markUnloadParam
- releaseHandle
- getAsset
- getSubAsset
设计 异步加载不同类型的资源 方法 loadAssetAsync
- 创建资源加载器:使用
g.loaderManager:newAssetLoader(path)
创建一个新的资源加载器实例,path
是资源的路径。 - 判断是否使用队列:如果提供了
queue
参数,则调用loader:loadQueued(queue, self.onLoaderDone, self)
将资源加载操作加入队列中,并指定加载完成后的回调函数self.onLoaderDone
。否则,调用loader:loadAsync(self.onLoaderDone, self)
直接异步加载资源。 - 保存加载器实例和回调函数:将加载器实例和对应的回调函数保存到
self.loaders
和self.callbacks
表中,使用加载器的句柄(handleID
)作为键。 - 返回句柄:返回加载器的句柄,用于标识这个加载操作。
---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.Topjoy.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadAssetAsync(path, callback, queue)---@type AssetLoaderlocal loader = g.loaderManager:newAssetLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end
异步加载资源,并允许用户指定加载完成后的回调函数。这对于需要异步加载资源并处理加载结果的应用场景非常有用,比如游戏中的资源预加载、UI资源的动态加载等。
### 注意事项
1. **资源管理**:确保在不再需要资源时及时释放,避免内存泄漏。
2. **错误处理**:在资源加载失败时,应记录错误信息,便于调试。
3. **线程安全**:如果`LoaderWallet`在多线程环境下使用,需要确保线程安全。
这段代码主要用于游戏开发中的资源管理,通过封装资源加载器,简化了资源加载和管理的流程。
--[[Desc: Loader持有者,封装Release
--]]---@class LoaderWallet
local LoaderWallet = SimpleClassUtil:class()
local cachePool =TablePool:new(16,nil,function(t)for i, _ in pairs(t) dot[i] = nilendend
)
function LoaderWallet:initialize(owner)self.owner = owner
endfunction LoaderWallet:setOwner(owner)self.owner = ownerself.loaders = cachePool:getObj()self.callbacks = cachePool:getObj()
end--- 释放所有的内部loader
function LoaderWallet:release()if not self.owner thenLogger.error("Try to Release LoaderWallet Twice. You should check if nil firstly.")returnendfor _, loader in pairs(self.loaders) doloader:release()endcachePool:releaseObj(self.loaders)cachePool:releaseObj(self.callbacks)self.owner = nilself.loaders = nilself.callbacks = nilif self.rlsFunc thenself.rlsFunc(self)end
end---@private
---@param loader BaseLoader
function LoaderWallet:onLoaderDone(loader)if self.owner==nil thenloader:release()returnendlocal handle = loader.handleIDlocal cb = self.callbacks[handle]if cb thencb(self.owner, handle)end
end---@param handle number
---@return boolean
function LoaderWallet:isComplete(handle)local loader = self.loaders[handle]if loader thenreturn loader.isCompleteendreturn false
end---@param handle number
---@return number @[0, 1]
function LoaderWallet:getProgress(handle)local loader = self.loaders[handle]if loader thenreturn loader:getProgress()endreturn 0
end---@param handle number
---@return boolean
function LoaderWallet:isLoading(handle)local loader = self.loaders[handle]if loader thenreturn loader:isLoading()endreturn false
end---@param handle number
---@return boolean
function LoaderWallet:isValidHandle(handle)return self.loaders[handle]~=nil
end---@param handle number
---@param unload boolean
function LoaderWallet:markUnloadParam(handle, unload)local loader = self.loaders[handle]if loader thenloader:markUnloadParam(unload)elseLogger.error("Miss Loader for handle:", handle)end
end---@param handle number
function LoaderWallet:releaseHandle(handle)local loader = self.loaders[handle]if loader thenloader:release()self.loaders[handle] = nilself.callbacks[handle] = nilelseLogger.error("Miss Loader for handle:", handle)end
end---@param path string
---@return boolean
function LoaderWallet:hasAnyWithPath(path)for _, loader in pairs(self.loaders) doif loader.path == path thenreturn trueendendreturn false
end--- 获取加载完成后的资源。如,prefab。
---@param handle number
---@return CS.UnityEngine.Object
function LoaderWallet:getAsset(handle)local loader = self.loaders[handle]if loader thenreturn loader.resultelseLogger.error("Miss Loader for handle:", handle)end
end--- 获取子资源。如,sprite。
---@param handle number
---@param name string
---@return CS.UnityEngine.Object
function LoaderWallet:getSubAsset(handle, name)local loader = self.loaders[handle]if loader thenreturn loader:getSubAsset(name)elseLogger.error("Miss Loader for handle:", handle)end
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.GameYL.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadAssetAsync(path, callback, queue)---@type AssetLoaderlocal loader = g.loaderManager:newAssetLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.GameYL.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadListSpriteAsync(path, callback, queue)---@type ListSpriteLoaderlocal loader = g.loaderManager:newListSpriteLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.GameYL.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadMaterialAsync(path, callback, queue)---@type MaterialLoaderlocal loader = g.loaderManager:newMaterialLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.GameYL.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadPrefabAsync(path, callback, queue)---@type PrefabLoaderlocal loader = g.loaderManager:newPrefabLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@return number @handle
function LoaderWallet:loadSceneAsync(path, callback)---@type SceneLoaderlocal loader = Global.loaderManager:loadSceneAsync(path, self.onLoaderDone, self)local handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.GameYL.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadTextAssetAsync(path, callback, queue)---@type TextAssetLoaderlocal loader = g.loaderManager:newTextAssetLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@param queue CS.GameYL.Base.ResourceManagement.OperationHandles.OperationQueue
---@return number @handle
function LoaderWallet:loadTextureAsync(path, callback, queue)---@type TextureLoaderlocal loader = g.loaderManager:newTextureLoader(path)if queue thenloader:loadQueued(queue, self.onLoaderDone, self)elseloader:loadAsync(self.onLoaderDone, self)endlocal handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@param path string
---@param callback fun(self:table, handle:number) @self是owner
---@return number @handle
function LoaderWallet:loadWwiseBankAsync(path, callback)---@type WwiseBankLoaderlocal loader = g.loaderManager:newWwiseBankLoader(path)loader:loadAsync(self.onLoaderDone, self)local handle = loader.handleIDself.loaders[handle] = loaderself.callbacks[handle] = callbackreturn handle
end---@overload fun(path:string)
---@param path string
---@param decodeBank boolean
---@param saveDecodedBank boolean
---@return number @handle
function LoaderWallet:loadWwiseBankSync(path, decodeBank, saveDecodedBank)---@type WwiseBankLoaderlocal loader = g.loaderManager:newWwiseBankLoader(path)loader.decodeBank = decodeBankloader.saveDecodedBank = saveDecodedBankloader:loadSync()local handle = loader.handleIDself.loaders[handle] = loaderreturn handle
endreturn LoaderWallet