您的位置:首页 > 娱乐 > 明星 > lua 游戏架构 之 资源加载 LoaderManager (一)

lua 游戏架构 之 资源加载 LoaderManager (一)

2024/12/23 15:28:36 来源:https://blog.csdn.net/heyuchang666/article/details/140505395  浏览:    关键词:lua 游戏架构 之 资源加载 LoaderManager (一)

定义一个 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 thenif parent thenreturn CS.UnityEngine.GameObject.Instantiate(self.result, parent, worldStay==true)elsereturn CS.UnityEngine.GameObject.Instantiate(self.result)endend
endreturn PrefabLoader

设计思路:

  • 引入了各种资源加载器,如AssetLoaderPrefabLoader等,这些加载器负责加载不同类型的资源。
  • initialize方法调用onInit方法进行初始化。onInit方法中,首先获取资源管理器实例,然后初始化各种资源加载器池,每个池子使用TablePool来管理对象池。

  • PrefabLoader为例,创建了一个对象池,池子大小为16。当需要新的PrefabLoader实例时,会调用传入的函数创建一个新的PrefabLoader对象,并设置其释放函数为self._assetPool:releaseObj(1)

  • loadAssetAsync方法用于异步加载资源。它从对象池中获取一个PrefabLoader实例,初始化它,并调用其loadAsync方法进行异步加载。

  • release方法用于释放所有资源加载器池,并清理资源管理器实例。

注意事项

  1. 对象池的使用:通过对象池管理资源加载器,可以减少对象的频繁创建和销毁,提高性能。
  2. 异步加载:资源加载方法(如loadAssetAsync)都是异步的,可以避免阻塞主线程。
  3. 资源管理器:使用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()
endfunction LoaderManager:onInit()self._resourceManager = CS.Topjoy.Base.ResourceManagement.ResourceManager.Instanceself._assetPool = TablePool:new(16, function()local loader = AssetLoader:new()loader.rlsFunc = function(l)self._assetPool:releaseObj(l)endreturn loaderend)self._prefabPool = TablePool:new(16, function()local loader = PrefabLoader:new()loader.rlsFunc = function(l)self._prefabPool:releaseObj(l)endreturn loaderend)self._materialPool = TablePool:new(16, function()local loader = MaterialLoader:new()loader.rlsFunc = function(l)self._materialPool:releaseObj(l)endreturn loaderend)self._texturePool = TablePool:new(16, function()local loader = TextureLoader:new()loader.rlsFunc = function(l)self._texturePool:releaseObj(l)endreturn loaderend)self._textAssetPool = TablePool:new(16, function()local loader = TextAssetLoader:new()loader.rlsFunc = function(l)self._textAssetPool:releaseObj(l)endreturn loaderend)self._listSpritePool = TablePool:new(16, function()local loader = ListSpriteLoader:new()loader.rlsFunc = function(l)self._listSpritePool:releaseObj(l)endreturn loaderend)self._multiAssetsPool = TablePool:new(16, function()local loader = MultiAssetLoaders:new()loader.rlsFunc = function(l)self._multiAssetsPool:releaseObj(l)endreturn loaderend)self._scenePool = TablePool:new(16, function()local loader = SceneLoader:new()loader.rlsFunc = function(l)self._scenePool:releaseObj(l)endreturn loaderend)self._wwiseBankPool = TablePool:new(4, function()local loader = WwiseBankLoader:new()loader.rlsFunc = function(l)self._wwiseBankPool:releaseObj(l)endreturn loaderend)self._walletPool = TablePool:new(16,function ()local loader = WalletLoader:new()loader.rlsFunc = function(l)self._walletPool:releaseObj(l)endreturn loaderend)
endfunction LoaderManager:release()self._resourceManager = nilLogger.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 AssetLoaderlocal 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 PrefabLoaderlocal 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 MaterialLoaderlocal 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 TextureLoaderlocal 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 TextAssetLoaderlocal 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 ListSpriteLoaderlocal 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 MultiAssetLoaderslocal 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 SceneLoaderlocal 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 SceneLoaderlocal 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)
endreturn LoaderManager

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com