xlua 实现协程替换Unity中的协程

时间:2022-01-22 20:31:49

C#中的协程:

      IEnumerator ShowSpiritInfo()
{
UIMessageMgr.ShowMsgWait(true); DestroyUIModelInfo(); bool isLoad = false;
AppInterface.GUIModule.CreateUISpirit(currSpiritId, delegate(UISpirit sp)
{
infoTextureCtrl.TargetSpirit = sp;
infoTextureCtrl.ui_Tx.width = ;
infoTextureCtrl.ui_Tx.height = ;
isLoad = true;
});
while (!isLoad)
yield return null;
modelTex.enabled = true;
UIMessageMgr.ShowMsgWait(false);
}

xlua中实现以上协程:

 local ShowSpiritInfo = function(previewWnd)
return util.cs_generator(function()
CS.UIMessageMgr.ShowMsgWait(true); previewWnd:DestroyUIModelInfo(); -- 加载精灵
local PlayerManager = CS.Common.AppInterface.GUIModule.PlayerManager;
local root = PlayerManager.m_ModelRoot;
local spirit = CS.UISpirit(PlayerManager.m_ModelRoot, previewWnd.currSpiritId);
PlayerManager.listSpirit:Add(spirit);
PlayerManager.dicSpirit:Add(spirit.number, spirit);
local isLoad = false;
spirit:BeginLoad(function()
isLoad = true;
end);
while false == isLoad do
coroutine.yield(CS.UnityEngine.WaitForSeconds(0.01));
end previewWnd.infoTextureCtrl.TargetSpirit = spirit;
previewWnd.infoTextureCtrl.ui_Tx.width = ;
previewWnd.infoTextureCtrl.ui_Tx.height = ;
previewWnd.modelTex.enabled = true; CS.UIMessageMgr.ShowMsgWait(false);
end)
end;

xlua中主要是 1、2、17、18、19行的写法。

调用(第10行实现):

 xlua.hotfix(CS.Modules.UI.UI_SpiritPreviewWnd, "OnChooseHandle", function(self, index)
if index >= and index <= self.currSpiritConfigList.Count then
self.currSpiritConfig = self.currSpiritConfigList[index];
self.currSpiritId = self.currSpiritConfig.f_SpiritID;
self.currSpiritData = CS.ClientData.ClientSpiritData.CreateSpiritData(self.currSpiritId);
self:RefreshSpiritInfo();
if == self.currSpiritConfigList[index].f_NextStarID then
CS.GameDefine.Mono:StartCoroutine(ShowCharacterInfo(self));
else
CS.GameDefine.Mono:StartCoroutine(ShowSpiritInfo(self));
end
end
end)