文档参考

完整脚本示例

下载文件,在游戏内导入,再在工作区开启对应模块。

本页目录

Hello HUD#

这个脚本绘制带圆角底板的 FPS 卡片。导入后启用 Hello HUD,右键打开设置,可调整 X、Y 和 Show FPS。位置选项属于这个脚本自己的设置,不自动接入聊天栏拖动编辑。

hello.sc下载 .sc
//@author Polaris
//@version 1.0
//@description A configurable FPS HUD
var hud;

script.handle("onLoad", function () {
    hud = polaris.registerModule("Hello HUD", "A configurable FPS display");
    hud.registerSetting("number", "X", 18, 0, 1000, 1);
    hud.registerSetting("number", "Y", 18, 0, 1000, 1);
    hud.registerSetting("boolean", "Show FPS", true);

    hud.handle("onRender2D", function (event) {
        var x = hud.getSetting("X");
        var y = hud.getSetting("Y");
        var text = "Hello, Polaris";
        if (hud.getSetting("Show FPS")) text += " | " + polaris.getFPS() + " FPS";
        var font = render.getMainFontRenderer(18);
        render.roundedRectangle(x, y, font.width(text) + 24, 40, 10, [18, 20, 26, 190]);
        font.draw(text, x + 12, y + 12, [240, 244, 255, 255]);
    });
});

script.handle("onUnload", function () {
    if (hud) hud.unregister();
});

Hello Notice#

这个脚本在模块开启时显示一次信息通知,关闭时输出本地聊天提示。它没有把通知放进每帧调用的事件中。先开启 Dynamic Island,再启用 Hello Notice 查看灵动岛提示;关闭 Hello Notice 会显示本地聊天消息。

notify.sc下载 .sc
//@author Polaris
//@version 1.0
//@description Notify when a script module changes state
var notice;

script.handle("onLoad", function () {
    notice = polaris.registerModule("Hello Notice", "Shows a notification when enabled");
    notice.handle("onEnable", function () {
        polaris.displayInfoNotification("Hello Notice", "Ready to use");
    });
    notice.handle("onDisable", function () {
        polaris.displayChat("Hello Notice disabled");
    });
});

script.handle("onUnload", function () {
    if (notice) notice.unregister();
});

确认加载成功#

  • 输入 .script list,确认文件显示 loaded。
  • 在工作区中找到 Hello HUD 或 Hello Notice 脚本模块并开启。
  • 修改文件后重载;onUnload 会注销旧模块,再由 onLoad 创建新模块。
  • 遇到错误时保留带文件名的完整提示,并对照 API 参数表。