Skip to content

04 进度条与场景 UI ​

给作物头顶挂一条进度条:讲清场景 UI 和屏幕 UI 的区别,以及它为什么只做给自己看。

你将做出什么 ​

开始成长时,作物上方出现0%到100%的进度条,约3秒结束后销毁;服务端只通知种下这株的玩家,所以只有本人看见。

作物头顶的进度条

1. 场景 UI 与屏幕 UI ​

类型位置和用途公开入口
场景 UI世界中的一株作物上方,摄像机移动时仍在作物的位置SceneGui创建场景容器
屏幕 UI玩家屏幕上的总分/飘分,第05课使用PlayerGui.EuiManager:GetRootNode()

界面编辑器与节点层级进度条层级参考

本课完整源码直接创建相同结构,无需私有界面预设,也不假定普通 WorldUnit 带有“Head”插槽。先在明确世界坐标创建场景容器,再每个进度节拍更新容器位置。

具体配方:

text
农场进度:EUISceneNode,220×64,最大可见距离30
├── progress_bar_bg:EUIImage,左下(0,0),220×64深色底板
│   └── 成长进度:EUILoadingBar,左下(10,10),200×20
└── 进度值:EUITextLabel,中心(110,48),200×32,白字、字体28

容器跟随作物上方4格,避开近处角色的头部与名字;完整深色底板让百分比不依赖天空或地面的底色。场景UI仍随距离缩小,应在目标摄像机距离检查可读性。

底板/进度使用当前公开基础图片 official://image/11017,颜色由 Color.New(...) 设定。子节点以父左下角为原点;底板与进度明确 Anchor = (0,0),文字使用默认中心锚点。场景容器的 Position 是 Vector3,普通 UI 子节点的 Position 是 Vector2。

安装与恢复检查点 ​

下载 第 04 课完整源码,先停止试玩,再把包里的 client/、server/、common/(如有)覆盖到这张农场地图自己的工程根目录。切换阶段时,删除课程包未列出的旧农场模块;保留编辑器为你当前地图生成的工程连接配置,不要复用别人的项目 ID。保存、同步,再重新试玩。包里的 SCENE.md 包含完整场景配方,README.md 列出本课文件和验收目标。

下面代码与下载包的指定文件/区域逐字一致,是解释用摘录;它依赖本课完整入口中的服务、配置和辅助函数,不能脱离工程单独运行。

2. 服务端通知,客户端创建并清理 ​

common/remote_event.lua 定义共享“Farm.Grow”“Farm.Sell”频道。服务端动画完成后发 grow:FireClient(player, { plant = seed, time = Config.GrowTime });客户端从收到的真实对象和时长建立表现。场景容器先用世界坐标创建,成功后再通过 Name、Size、MaxVisibleDistance 属性配置;普通子控件仍使用 World:CreateUnit 的配置表。

lua
-- @runtime client
Events.grow.OnClientEvent:Connect(function(data)
    if type(data) ~= "table" or type(data.time) ~= "number" or data.time <= 0 then return end
    local plant = data.plant
    if plant == nil or not plant:IsA("WorldUnit") or plant.Parent == nil then return end
    local scene = SceneGui:CreateSceneNodeAtPosition(plant.Position + Vector3.New(0, 4, 0))
    if scene == nil then print("[Farm] SceneGui 未就绪,跳过本次进度UI"); return end
    scene.Name = "农场进度"
    scene.Size = Vector2.New(220, 64)
    scene.MaxVisibleDistance = 30
    local bg = World:CreateUnit("EUIImage", {
        Parent = scene, Name = "progress_bar_bg", Anchor = Vector2.New(0, 0),
        Position = Vector2.New(0, 0), Size = Vector2.New(220, 64),
        Image = "official://image/11017", Color = Color.New(35, 45, 35, 255),
    })
    if bg == nil or not bg:IsA("EUIImage") then scene:Destroy(); return end
    local bar = World:CreateUnit("EUILoadingBar", {
        Parent = bg, Name = "成长进度", Anchor = Vector2.New(0, 0),
        Position = Vector2.New(10, 10), Size = Vector2.New(200, 20),
        Image = "official://image/11017", Color = Color.New(100, 210, 90, 255), Percent = 0,
    })
    local text = World:CreateUnit("EUITextLabel", {
        Parent = scene, Name = "进度值", Position = Vector2.New(110, 48),
        Size = Vector2.New(200, 32), Font = "hkxzyt_w9_gb.ttf", FontSize = 28, Text = "0%",
        TextColor = Color.New(255, 255, 255, 255),
    })
    if bg == nil or not bg:IsA("EUIImage") or bar == nil or not bar:IsA("EUILoadingBar")
        or text == nil or not text:IsA("EUITextLabel") then
        scene:Destroy()
        print("[Farm] 进度节点创建失败")
        return
    end
    local alive = true
    local timer
    local connection
    local function Close()
        if not alive then return end
        alive = false
        if timer ~= nil then timer:Cancel() end
        if connection ~= nil then connection:Disconnect() end
        scene:Destroy()
    end
    scene.Destroying:Once(function()
        alive = false
        if timer ~= nil then timer:Cancel() end
        if connection ~= nil then connection:Disconnect() end
    end)
    connection = plant.Destroying:Once(Close)
    local n = 0
    local count = math.max(1, math.ceil(data.time / 0.06))
    timer = TimerService:CreateTimer(count, 0.06, false, function()
        if not alive then return end
        n = n + 1
        local percent = math.min(100, n / count * 100)
        scene.Position = plant.Position + Vector3.New(0, 4, 0)
        bar.Percent = percent
        text.Text = tostring(math.ceil(percent)) .. "%"
        if n == count then Close() end
    end)
end)

返回 nil、类型错误时终止本次 UI 创建并给出提醒;作物/界面被销毁时取消计时器和事件连接;结束时销毁整个场景容器。SceneGui 只使用当前公开创建入口,不调用隐藏的预设构造接口。

运行并核对 ​

  1. 种下后本人看见0%→100%;结束后场景容器消失。
  2. 走远回看,进度条留在作物上方;返回30格范围内应正常显示。
  3. 两名测试玩家时,仅种植者收到这次进度UI;这只验证通知范围,本课程尚未实现多人占地。
  4. 生长中删除种子,界面和计时器应清理,日志无后续失效访问。

没有进度UI时查两端共享频道名、FireClient目标与 SceneGui 提醒;位置偏了查 Vector3 世界位置/Vector2局部位置;文字被裁切查字体和64高容器。

保存进度 ​

下一步:05 放置区与计分。完整恢复点为第04课源码包。

API:SceneGui、EUISceneNode、EUILoadingBar、RemoteEvent。