在项目中使用 FairyGUI 包
将 FairyGUI 包导入 Studio 后,需通过脚本结合 FairyGUI API 和 资源管理器 API 在场景中使用它们,操作方式与 Studio 内置UI编辑器创建的包一致。
基本步骤
要使用导入的 FairyGUI 包:
脚本化交互式UI元素
为帮助创作者快速上手 FairyGUI 包的交互式UI元素脚本,我们提供了一个FairyGUI 示例包,可直接使用。导入前请先将包压缩为 ZIP 文件。
转场动画
获取“StartGame”面板及其内部的“start”动画。播放动画,动画结束后隐藏该面板。
-- 转场动画:播放开场动画
def ShowStartGame()
-- 使用 GetChild 方法获取 "StartComp" 组件
local startComp = mainPanel:GetChild("StartGame")
-- 从 "StartComp" 组件获取 "start" 动画
local startTransition = startComp:GetTransition("start")
-- 定义隐藏 StartComp 的函数
local function HideStartComp()
startComp.visible = false -- 设置 StartComp 不可见
end
-- 播放 startTransition,动画结束后执行 HideStartComp
startTransition:Play(HideStartComp)
end
文本与进度条
在 Update 方法中更新时间信息,实现倒计时和进度条显示。
-- 文本与进度条功能
local time=0
local totalTime=120--设置倒计时总时长
function CountDownText()
if not mainPanel then
return
end
time=time+0.02--每帧增加 0.02 秒
local countDownText=mainPanel:GetChildByPath("ProgressComp.Countdown")--通过路径获取倒计时文本框
countDownText.text="剩余时间 : "..math.ceil(totalTime-time).." S"--更新倒计时显示
local progress=mainPanel:GetChildByPath("ProgressComp.CountdownProgress")--通过路径获取进度条
progress.value=((totalTime-time)/totalTime)*100--根据剩余时间计算进度条百分比
end
-- 系统 Update 方法
script.OnUpdate(function ()
-- 调用倒计时更新函数
CountDownText()
end)