Lua加载使用async-await方式

This commit is contained in:
fatiao 2025-02-26 13:59:25 +08:00
parent 6817f58af6
commit 69b1350da5
15 changed files with 593 additions and 520 deletions

View File

@ -84,6 +84,7 @@ local LuaMain = class("LuaMain")
-- 这里是异步初始化, 初始化的耗时操作可放这里面
function LuaMain:Init()
AW = InternalRequire("async-await")
Constant = InternalRequire("Constant")
PlatformPack = InternalRequire("PlatformPack")
Inspect = InternalRequire("inspect")

View File

@ -0,0 +1,45 @@
local co = coroutine
local unp = table.unpack ~= nil and table.unpack or unpack
local M = {}
local function next_step (thread, success, ...)
local res = {co.resume(thread, ...)}
assert(res[1], unp(res, 2))
if co.status(thread) ~= 'dead' then
res[2](function (...)
next_step(thread, success, ...)
end)
else
success(unp(res, 2))
end
end
M.async = function (func)
assert(type(func) == 'function', 'async params must be function')
local res = {
is_done = false,
data = nil,
cb = nil
}
next_step(co.create(func), function (...)
res.is_done = true
res.data = {...}
if res.cb ~= nil then
res.cb(unp(res.data))
end
end)
return function (cb)
if cb ~= nil and res.is_done then
cb(unp(res.data))
else
res.cb = cb
end
end
end
M.await = function (async_cb)
assert(type(async_cb) == 'function', 'await params must be function')
return co.yield(async_cb)
end
return M

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 355d2e9fe69c94e48a86eadc3a0433eb
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -19,14 +19,16 @@ function MinePlayerEntityView:ExitWorld()
end
function MinePlayerEntityView:InitGenerateView()
AW.async(function()
MinePlayerEntityView.super.InitGenerateView(self)
-- 添加主角光环
local guanghuanGo = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.EffectPath, 'Hero/Common/FX_Hero_guanghuan')
local guanghuanGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.EffectPath, 'Hero/Common/FX_Hero_guanghuan'))
local guanghuanTrans = guanghuanGo.transform
guanghuanTrans:SetParent(self.transform)
guanghuanTrans.localPosition = Vector3.zero
guanghuanTrans.localRotation = Quaternion.identity
self.guanghuanGo = guanghuanGo
end)
end
function MinePlayerEntityView:RefreshGenerateView()

View File

@ -46,6 +46,7 @@ function PlayerEntityView:CollectPreloadAssets()
end
function PlayerEntityView:InitGenerateView()
AW.async(function()
-- 创建Model
local modelGo = self.roleViewSystem:Create()
local modelTrans = modelGo.transform
@ -58,24 +59,12 @@ function PlayerEntityView:InitGenerateView()
UnityEngine.Component.Destroy(colliders[i])
end
-- 添加阴影
local shadowGo = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.EffectPath, 'Common/FX_Yinying')
local shadowGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.EffectPath, 'Common/FX_Yinying'))
local shadowTrans = shadowGo.transform
shadowTrans:SetParent(self.transform)
shadowTrans.localPosition = Vector3.zero
shadowTrans.localRotation = Quaternion.identity
-- local uipoint = modelTrans:Find("ui_point")
-- local uiPosition = uipoint.position
-- 添加UI
-- local uiAssetName = Constants.UI3DPath .. "/MainCity/UIPlayerView"
-- local uiGo = ManagerContainer.ResMgr:LuaGetGoFromPool(nil, uiAssetName)
-- local uiTrans = uiGo.transform
-- uiTrans:SetParent(self.transform)
-- uiTrans.position = uiPosition
-- uiTrans.rotation = Quaternion.identity
-- local uiPlayerView = UIPlayerView:new(uiTrans, ManagerContainer.LuaMainCityMgr:GetMainCamera(), self.uid)
self.animator = modelGo:GetComponent(Enum.TypeInfo.Animator)
self.shadowGo = shadowGo
-- self.uiGo = uiGo
@ -84,6 +73,7 @@ function PlayerEntityView:InitGenerateView()
if self.isUpdate then
self:RefreshGenerateView()
end
end)
end
function PlayerEntityView:RefreshGenerateView()

View File

@ -45,14 +45,16 @@ function GoPoolMgr:SpawnGo(prefabName, cb)
if gridItemView.GridItemName ~= "" then
local itemlua = require("GridViewItem/"..gridItemView.GridItemName.."_Generate"):new()
--itemlua.gameObject = UnityEngine.GameObject.Instantiate(go)
AW.async(function()
local goName = gridItemView.OriName ~= "" and gridItemView.OriName or gridItemView.GridItemName
itemlua.gameObject = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UIItemDir, goName)
itemlua.gameObject = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UIItemDir, goName))
itemlua.prefabName = gridItemView.GridItemName
itemlua.oriName = goName
itemlua:InitGenerate(itemlua.gameObject.transform)
itemlua.gameObject:SetActive(true)
v(itemlua)
--v = nil
end)
end
end
loadingPrefabList[prefabName] = nil

View File

@ -193,7 +193,8 @@ function LuaGuildLobbyMgr:_LoadProgressChanged(progress)
end
function LuaGuildLobbyMgr:_PreloadAssetsComplete()
local modelGo = ManagerContainer.ResMgr:LuaGetGoFromPool("Assets/Content/Prefabs/Camera", "DefaultCVC")
AW.async(function()
local modelGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync("Assets/Content/Prefabs/Camera", "DefaultCVC"))
local defaultCVC = modelGo:GetComponent(typeof(Cinemachine.CinemachineVirtualCamera))
self.mainCamera = LuaBattleBridge.InitMainCamera()
self.cullingMask = self.mainCamera.cullingMask
@ -207,6 +208,7 @@ function LuaGuildLobbyMgr:_PreloadAssetsComplete()
defaultCVC.Follow = self.entityMgr:GetTransform(entityType, uid)
-- 打开UI界面
ManagerContainer.LuaUIMgr:Open(Enum.UIPageName.UIGuildLobby)
end)
end
function LuaGuildLobbyMgr:_OnLoadProgress(progress)

View File

@ -245,6 +245,7 @@ function RedPointSimpleMgr:RefreshRedPointDisplayPriority(treeNode, needCheckLoc
end
function RedPointSimpleMgr:FillContentCompeleted(wnd)
AW.async(function()
local list = ManagerContainer.CfgMgr:GetUIRedPointCfgDataByUIId(wnd.uiData.id)
for k,v in pairs(list) do
if Constant.OpenPay or not v.NoPay then
@ -255,7 +256,7 @@ function RedPointSimpleMgr:FillContentCompeleted(wnd)
if target then
local redPointGo = target.transform:Find("redPoint"..data.Id)
if not redPointGo then
redPointGo = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UICommonPath, Enum.RPPrefab[data.RPType])
redPointGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UICommonPath, Enum.RPPrefab[data.RPType]))
else
redPointGo = redPointGo.gameObject
end
@ -278,6 +279,7 @@ function RedPointSimpleMgr:FillContentCompeleted(wnd)
end
end
end
end)
end
function RedPointSimpleMgr:UICloseCompeleted(wnd)

View File

@ -115,17 +115,26 @@ end
___G_Coroutine_Id_Dict = {}
function ResMgr:LuaGetGoFromPool(path, asset)
local k = path.."/"..asset
local retgo = nil
___G_Coroutine_Id_Dict[k] = coroutine.create(function()
self:GetGoFromPoolAsync(path, asset, function(go)
retgo = go
coroutine.resume(___G_Coroutine_Id_Dict[k])
end)
coroutine.yield()
end)
coroutine.resume(___G_Coroutine_Id_Dict[k])
return retgo
-- local k = path.."/"..asset
-- local retgo = nil
-- ___G_Coroutine_Id_Dict[k] = coroutine.create(function()
-- self:GetGoFromPoolAsync(path, asset, function(go)
-- retgo = go
-- coroutine.resume(___G_Coroutine_Id_Dict[k])
-- end)
-- coroutine.yield()
-- end)
-- coroutine.resume(___G_Coroutine_Id_Dict[k])
-- return retgo
return self:GetGoFromPool(path, asset)
end
function ResMgr:LuaGetGoFromPoolAsync(path, asset)
local func = function(cb)
self:GetGoFromPoolAsync(path, asset, function(go) cb(go) end)
end
return func
end
return ResMgr

View File

@ -359,7 +359,7 @@ end
--只用于主城的建筑
function UIFuncUnlockMgr:SpecialLock(wnd, data, target, result, key, guideState, conds)
AW.async(function()
local function InitTitle(lock, targetButtonField, targetTitle, textContent)
local title = lock.transform:Find("Title")
title.transform.position = targetTitle.transform.position
@ -393,7 +393,7 @@ function UIFuncUnlockMgr:SpecialLock(wnd, data, target, result, key, guideState,
local lockBtn = target.transform:Find("LockbtnItem")
if not result and data.NeedMask then
if lockBtn == nil then
lockBtn = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UICommonPath, "LockbtnItem")
lockBtn = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UICommonPath, "LockbtnItem"))
lockBtn.name = "LockbtnItem"
lockBtn.transform:SetParent(target.transform)
lockBtn.transform.localPosition = Vector3.zero
@ -433,7 +433,7 @@ function UIFuncUnlockMgr:SpecialLock(wnd, data, target, result, key, guideState,
local lockBubble = target.transform:Find("LockBubbleItem")
if result and funcBtns[wnd.uiData.id][key]["needGuide"] then
if lockBubble == nil then
lockBubble = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UICommonPath, "LockBubbleItem")
lockBubble = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UICommonPath, "LockBubbleItem"))
lockBubble.name = "LockBubbleItem"
lockBubble.transform:SetParent(target.transform)
lockBubble.transform.localPosition = Vector3.zero
@ -511,6 +511,7 @@ function UIFuncUnlockMgr:SpecialLock(wnd, data, target, result, key, guideState,
end
funcBtns[wnd.uiData.id][key]["lockBubble"] = lockBubble
end)
end
function UIFuncUnlockMgr:UICloseCompeleted(wnd)

View File

@ -130,9 +130,10 @@ function ModelViewSystem:CancelCreate()
end
function ModelViewSystem:Create()
AW.async(function()
local newGo = nil
if self.loadModelPath and self.loadModelName then
newGo = ManagerContainer.ResMgr:LuaGetGoFromPool(self.loadModelPath, self.loadModelName)
newGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(self.loadModelPath, self.loadModelName))
self.modelPath = self.loadModelPath
self.modelName = self.loadModelName
end
@ -167,13 +168,15 @@ function ModelViewSystem:Create()
self.ownercb = nil
self.cb = nil
end)
end
--冒泡对话
function ModelViewSystem:SetDialogue()
AW.async(function()
if self._dialogueData then
if not self._dialogueGo then
local _newGo = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.ModelPath, _dialogueName);
local _newGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.ModelPath, _dialogueName))
if not _newGo then
LogError("冒泡预设的路径或者名字不对,请检查");
else
@ -201,6 +204,7 @@ function ModelViewSystem:SetDialogue()
end
self._dialogueEndTimer = ManagerContainer.LuaTimerMgr:AddTimer(self._dialogueData._showEndTime, 1, self, _endDialogueText, nil);
end
end)
end
function ModelViewSystem:GetGameObject()

View File

@ -422,7 +422,7 @@ end
---@generic 预加载的资源已加载完成
function RoleViewSystem:PreloadedAssets()
self:Create()
AW.async(function() self:Create() end)
end
function RoleViewSystem:BeginCreate()
@ -506,7 +506,7 @@ function RoleViewSystem:Create()
local avatarData = ManagerContainer.CfgMgr:GetAvatarCfgById(roleCfg.AvatarId)
assetName = avatarData.AvatarPrefab
end
newGO = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.ModelPath, assetName)
newGO = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.ModelPath, assetName))
if ManagerContainer.LuaModelMgr and ManagerContainer.LuaModelMgr.ModelRoot then
newGO.transform:SetParent(ManagerContainer.LuaModelMgr.ModelRoot.transform)
else

View File

@ -141,7 +141,8 @@ function IconItemCtr:RefreshIcon(wnd, itemlua, logicData, enterType, onClickOwne
itemlua.qualityFX:SetActive(false)
else
if itemlua.uiParticle == nil then
local fxGo = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.EffectPath,qualityFxName)
AW.async(function()
local fxGo = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.EffectPath,qualityFxName))
if fxGo~= nil then
fxGo.transform:SetParent(itemlua.qualityFX.transform)
fxGo.transform.localPosition = Vector3.zero
@ -181,6 +182,7 @@ function IconItemCtr:RefreshIcon(wnd, itemlua, logicData, enterType, onClickOwne
wnd:AddNewEffect(itemlua,qualityFxName,fxGo)
itemlua.qualityFX:SetActive(true)
end
end)
end
end
end

View File

@ -454,6 +454,7 @@ function BigMapView:_RefreshPoints()
end
function BigMapView:_PreloadedAssets()
AW.async(function()
local uiPath = Constants.UIPath
local enterType = self.enterType
local cfgMgr = ManagerContainer.CfgMgr
@ -472,7 +473,7 @@ function BigMapView:_PreloadedAssets()
local startPos = self.levelStartPos
if self:_IsSelfAtCurMap() then
if not self.selfPlayerLuaCtr then
local selfGoItem = resMgr:LuaGetGoFromPool(uiPath, PointPlayerItemPath)
local selfGoItem = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(uiPath, PointPlayerItemPath))
local iconImage, myRankGo, dscLeftGo, dscLeftTxt, dscRightGO, dscRightTxt, selfGoAnimator
if selfGoItem then
local selfGoItemTrans = selfGoItem.transform
@ -630,6 +631,7 @@ function BigMapView:_PreloadedAssets()
elseif enterType == Enum.BigMapEnterType.NextLevel then
self:_PlayNextLevelEffect()
end
end)
end
function BigMapView:_OnClickStoryItem(btn, params)
@ -854,8 +856,9 @@ function BigMapView:_DisposeSmallPoints()
end
function BigMapView:_CreateStartPoint(pointPos)
AW.async(function()
if not self.startPoint then
local startItem = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UIPath, PointStartItemPath)
local startItem = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UIPath, PointStartItemPath))
self.startPoint = startItem
end
if self.startPoint then
@ -866,6 +869,7 @@ function BigMapView:_CreateStartPoint(pointPos)
startItemTrans.localScale = Vector3.one
self.startPoint:SetActive(true)
end
end)
end
@ -877,7 +881,7 @@ function BigMapView:_DisposeStartPoint()
end
function BigMapView:_CreateStoryIconLuaCtr(levelId, artRes, pointPos)
local iconItem = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UIPath, PointIconItemPath)
local iconItem = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UIPath, PointIconItemPath))
local iconAnimator, iconImage, button
if iconItem then
local iconItemTrans = iconItem.transform
@ -924,7 +928,7 @@ function BigMapView:_DisposeStoryIconLuaCtr(levelId)
end
function BigMapView:_CreateStoryGoLuaCtr(levelId, artRes, pointPos)
local goItem = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UIPath, artRes)
local goItem = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UIPath, artRes))
local goAnimator, button
if goItem then
local goItemTrans = goItem.transform

View File

@ -536,7 +536,8 @@ function UIRoleMain1View:RefreshRedPoint(rpType)
end
function UIRoleMain1View:CreateNormalRedPoint(parent)
local redPoint = ManagerContainer.ResMgr:LuaGetGoFromPool(Constants.UICommonPath, UIRedPointRP)
AW.async(function()
local redPoint = AW.await(ManagerContainer.ResMgr:LuaGetGoFromPoolAsync(Constants.UICommonPath, UIRedPointRP))
if redPoint then
redPoint.transform:SetParent(parent)
redPoint.transform.localPosition = Vector3.zero
@ -544,6 +545,7 @@ function UIRoleMain1View:CreateNormalRedPoint(parent)
redPoint.transform.localScale = Vector3.one
redPoint:SetActive(true)
end
end)
end
function UIRoleMain1View:RefreshOtherRedPoint()