Skip to content

插件菜单与 HUD

设置菜单

声明 ui.menu 后,可以注册声明式设置页面。控件包括 textbuttonswitchsliderchoicetext_inputsection

lua
local state = {
    enabled = samp.storage.get("enabled", true),
    size = samp.storage.get("size", 16)
}

samp.ui.menu.register({
    id = "settings",
    title = "Example settings",
    controls = {
        {
            type = "switch",
            id = "enabled",
            title = "Enabled",
            value = state.enabled
        },
        {
            type = "slider",
            id = "size",
            title = "Text size",
            value = state.size,
            min = 10,
            max = 32,
            step = 1
        }
    }
})

samp.events.on("ui.control_changed", function(event)
    if event.pageId == "settings" then
        local id = event.controlId
        state[id] = event.value
        samp.storage.set(id, event.value)
    end
end)

控件变化通过 ui.control_changed 派发。使用值前必须检查 pageIdcontrolId

HUD 绘制

声明 ui.draw 后,只能在 draw.hud 中提交绘制:

lua
samp.events.on("draw.hud", function()
    samp.draw.text({
        text = "Plugin active",
        x = 24,
        y = 80,
        size = 16,
        color = 0xFFFF8C42
    })

    samp.draw.progress_bar({
        x1 = 24,
        y1 = 108,
        x2 = 204,
        y2 = 116,
        value = 0.75,
        color = 0xFFFF6B35,
        background_color = 0x66000000
    })
end)

支持文字、线段、矩形、填充矩形、圆、进度条、包内图片和世界坐标投影。颜色使用 0xAARRGGBB

每帧回调中应复用文本和图片,使用 samp.format.number() 格式化数字,不要访问存储或执行大循环。