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

定义一个 LoaderManager class,用于管理各种资源加载器。它使用了对象池(Object Pool)来优化资源加载器的创建和销毁,从而提高性能

举例定义一个 PrefabLoader

Lua 复制代码
--[[
   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

设计思路:

  • 引入了各种资源加载器,如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来管理资源加载,确保资源加载的一致性和效率。
Lua 复制代码
---@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
相关推荐
58沈剑4 小时前
80后聊架构:架构设计中两个重要指标,延时与吞吐量(Latency vs Throughput) | 架构师之路...
架构
想进大厂的小王7 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
阿伟*rui8 小时前
认识微服务,微服务的拆分,服务治理(nacos注册中心,远程调用)
微服务·架构·firefox
ZHOU西口8 小时前
微服务实战系列之玩转Docker(十八)
分布式·docker·云原生·架构·数据安全·etcd·rbac
Footprint_Analytics10 小时前
Footprint Analytics 助力 Sei 游戏生态增长
游戏·web3·区块链
deephub10 小时前
Tokenformer:基于参数标记化的高效可扩展Transformer架构
人工智能·python·深度学习·架构·transformer
架构师那点事儿12 小时前
golang 用unsafe 无所畏惧,但使用不得到会panic
架构·go·掘金技术征文
W Y14 小时前
【架构-37】Spark和Flink
架构·flink·spark
半盏茶香14 小时前
【C语言】分支和循环详解(下)猜数字游戏
c语言·开发语言·c++·算法·游戏
Gemini199514 小时前
分布式和微服务的区别
分布式·微服务·架构