unity-新手引导
这个引导思路还可以,中间实现的流程及接口还比较粗糙,后面再做一版更完善的。
主要思路:使引导逻辑脱离于ui逻辑,便于扩展和维护。
引导主要接口类
GuideUtil = {}
local this = GuideUtil
local corList = {}
local corState = {
suspended = "suspended", --挂起
running = "running", --运行
dead = "dead", --死亡
normal = "normal", --正常
}
-------------------------------------------------------------------
--执行guide脚本
--@param step string 引导步骤
function this.OpenGuideByStep( step )
if not step then
helper.logError(step .. " == nil")
return
end
local guide = require( guideStep[step].path )
if not guide then
helper.logError("step =" .. step .. " not found")
return
end
-- EventApi.StartEvent( guide )
this.main = guide.main
this.clean = guide.clean
if (not this.main) then
helper.logError("step =" .. step .. " not found function of main")
return
else
this.main()
end
end
--启动协程
--@param uiName string 需要等待的ui名字
--@param callBack function 回调方法
--@return co coroutine 协程
function this.InitCor( uiName, callBack )
local FindUI = function()
if uiName then
while true do
local curUI = uiMgr.getCurrentFUI()
if curUI and curUI.fairyUIName == uiName then
break
else
coroutine.wait(0)
end
end
end
end
local co = coroutine.create(function()
FindUI()
if callBack then
callBack()
end
end)
table.insert( corList, co )
-- coroutine.resume( co )
end
function this.ClearCor()
if not corList then
corList = {}
end
for _, co in pairs( corList ) do
co = nil
end
corList = {}
end
function this.StartCor()
if next( corList ) then
local co = table.remove( corList, 1 )
coroutine.resume( co )
else
if this.clean then
this.clean()
end
this.ClearCor()
end
end
--获取ui坐标
--@param parent gObject 父节点
--@param child gObject 子节点
--@return localPos Vector2 子节点相对布局坐标
function this.GetUILocalPos( parent, child )
local screenPos = child:LocalToGlobal(Vector2.zero)
local localPos = parent.inst:GlobalToLocal(screenPos)
return localPos
end
--点击事件
--@param btnObj gObject btn节点
--@return callBack function 点击事件回调方法
function this.TouchEndEvent( btnObj, callBack )
btnObj.onTouchEnd:Set(function(context)
if callBack then
callBack()
end
end)
end
--获取ui上元件
function this.GetTargetElement( ui, ... )
local names = {...}
local targetElement
local elementName
-- names = unpack(names)
for i = 1, #names do
elementName = names[i]
if not targetElement then
targetElement = ui.inst:GetChild( elementName )
else
targetElement = targetElement:GetChild( elementName )
end
end
if targetElement then
return targetElement
else
helper.logError("ui = " .. ui.fairyUIName .. " 没有找到指定节点 names[#names] = " .. names[#names])
end
end
--获取ui上 指定list 指定元件
function this.GetListTargetElement( ui, index, listName, elementName )
local list = ui.inst:GetChild( listName )
local item = list:GetChildAt( index )
local targetElement = item:GetChild( elementName )
return targetElement
end
引导步骤配置
guideStep = {
homelandGuide = { step = 1, name = "homelandGuide", path = "guide/step/homelandGuide"},
battleGuide = { step = 2, name = "battleGuide", path = "guide/step/battleGuide"},
}
guideList = {
[1] = guideStep.homelandGuide,
[2] = guideStep.battleGuide,
}
引导实例
local homelandGuide = {}
local this = homelandGuide
local posCfg = {
registerPos = Vector3(19.95, 1.28, -18.43), --Plane2607 (x, y, z)
cellPos = Vector3(-85.12, 1.85, -101.16), --Plot90 (x - 10, y, z)
}
function this.main()
local HomeHudGuide1 = function()
local homeHud = uiMgr.FindUI('homeHud')
if not homeHud then helper.logError("homeHud is nil") return end
local jumpBtn = GuideUtil.GetTargetElement( homeHud, "taskTipBtn", "jumpBtn" )
local localPos = GuideUtil.GetUILocalPos( homeHud, jumpBtn )
guideCtrl.OpenGuide({
guideId = 1,
callBack = function()
Lua2CSharp.SetHomelandGodCamControl(false)
FunctionUtil.HomelandCamJump( BuildType1.plot, 81, true )
guideCtrl.OpenGuide({
guideId = 2,
maskName = "window1",
})
GuideUtil.StartCor()
end
})
end
local WardenOfficeGuide = function()
local wardenOffice = uiMgr.FindUI('wardenOffice')
if not wardenOffice then helper.logError("wardenOffice is nil") return end
local recruitBtn = GuideUtil.GetTargetElement( wardenOffice, "recruitBtn" )
local localPos = GuideUtil.GetUILocalPos( wardenOffice, recruitBtn )
guideCtrl.OpenGuide({
guideId = 3,
size = recruitBtn.size,
pos = localPos,
isShowHand = true,
})
GuideUtil.TouchEndEvent( recruitBtn, function()
guideCtrl.CloseGuide()
local wardenTopInfo = uiMgr.FindUI('wardenTopInfo')
local closeBtn = GuideUtil.GetTargetElement( wardenTopInfo, "closeBtn" )
local localPos = GuideUtil.GetUILocalPos( wardenTopInfo, closeBtn )
guideCtrl.OpenGuide({
guideId = 4,
size = closeBtn.size,
pos = localPos,
isShowHand = true,
})
GuideUtil.TouchEndEvent( closeBtn, function()
guideCtrl.CloseGuide()
GuideUtil.StartCor()
end)
end )
end
local HomeHudGuide2 = function()
local homeHud = uiMgr.FindUI('homeHud')
if not homeHud then helper.logError("homeHud is nil") return end
local pos = posCfg.registerPos
taskCtrl.MoveCamera(pos.x, pos.y, pos.z)
guideCtrl.OpenGuide({
guideId = 5,
callBack = function ()
pos = posCfg.cellPos
taskCtrl.MoveCamera(pos.x, pos.y, pos.z)
guideCtrl.OpenGuide({
guideId = 6,
callBack = function()
Lua2CSharp.SetHomelandGodCamControl(false)
FunctionUtil.HomelandCamJump( BuildType1.space, 1, true )
guideCtrl.OpenGuide(
{
guideId = 7,
maskName = "window2",
})
GuideUtil.StartCor()
end
})
end
})
end
local CleanBuildGuide = function()
local buildClean = uiMgr.FindUI('buildClean')
if not buildClean then helper.logError("buildClean is nil") return end
local cleanBtnGroup = GuideUtil.GetTargetElement( buildClean, "cleanBtnGroup" )
local localPos = GuideUtil.GetUILocalPos( buildClean, cleanBtnGroup )
guideCtrl.OpenGuide({
guideId = 8,
size = cleanBtnGroup.size,
pos = localPos,
isShowHand = true,
})
GuideUtil.TouchEndEvent( cleanBtnGroup, function ()
guideCtrl.CloseGuide()
Lua2CSharp.SetHomelandGodCamControl(false)
FunctionUtil.HomelandCamJump( BuildType1.plot, 1, true )
guideCtrl.OpenGuide({
guideId = 9,
maskName = "window2",
})
GuideUtil.StartCor()
end)
end
local BuildListGuide = function()
local buildlist = uiMgr.FindUI('buildlist')
if not buildlist then helper.logError("buildlist is nil") return end
local buildbtn = GuideUtil.GetListTargetElement( buildlist, 0, "buildShowList", "buildbtn" )
local localPos = GuideUtil.GetUILocalPos( buildlist, buildbtn )
guideCtrl.OpenGuide({
guideId = 10,
size = buildbtn.size,
pos = localPos,
isShowHand = true,
})
GuideUtil.TouchEndEvent( buildbtn, function ()
guideCtrl.CloseGuide()
Lua2CSharp.SetHomelandGodCamControl(false)
guideCtrl.OpenGuide({
guideId = 11,
maskName = "window2",
isShowArrow = true,
})
GuideUtil.StartCor()
end)
end
local ConstructiondetailsGuide = function()
local constructiondetails = uiMgr.FindUI('constructiondetails')
if not constructiondetails then helper.logError("constructiondetails is nil") return end
guideCtrl.OpenGuide({
guideId = 12,
callBack = function() end
})
this.clean()
end
GuideUtil.ClearCor()
GuideUtil.InitCor( 'homeHud', HomeHudGuide1 )
GuideUtil.InitCor( 'wardenTopInfo', WardenOfficeGuide )
GuideUtil.InitCor( 'homeHud', HomeHudGuide2 )
GuideUtil.InitCor( 'buildClean', CleanBuildGuide )
GuideUtil.InitCor( 'buildlist', BuildListGuide )
GuideUtil.InitCor( 'constructiondetails', ConstructiondetailsGuide )
GuideUtil.StartCor()
end
function this.clean()
Lua2CSharp.SetHomelandGodCamControl(true)
guideCtrl.FinishGuideStep( guideStep.homelandGuide )
end
-------------------------------------------------------------------
return this