"ui";
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 设置悬浮窗位置
floatyWin.setPosition(device.width - 420, 100);
// 开始运行脚本 单独购买
if (!config.isRunning&&config.select_runtype!="单商品购买"){
run_step()
}
// 开始运行脚本 循环操作
floatyWin.startBtn.click(function() {
if (config.isRunning) {
log("脚本正在运行中,请勿重复启动");
return;
}
run_step()
}); 这么写脚本运行后会直接卡死,保持功能不变的情况下,帮我改成"ui"模式
回答
文档地址 https://docs.hamibot.com/
将你的代码从 ui
模式改为兼容 Rhino 的写法,并确保所有 UI 操作在主线程执行,以下是修改后的版本:
"ui";
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 设置悬浮窗位置
floatyWin.setPosition(device.width - 420, 100);
// 开始运行脚本 单独购买
if (!config.isRunning && config.select_runtype != "单商品购买") {
run_step();
}
// 开始运行脚本 循环操作
floatyWin.startBtn.click(function() {
if (config.isRunning) {
log("脚本正在运行中,请勿重复启动");
return;
}
run_step();
});
注意事项:
- 确保
run_step()
函数已定义且无阻塞逻辑。 - 如果
config
是全局变量,请确认它已在外部正确初始化。 - 所有对控件的操作自动发生在 UI 线程,不需要额外用
ui.run()
包裹。
如果你仍然遇到卡死问题,请检查 run_step()
函数是否执行了长时间的同步任务。如果是,请使用 threads.start()
将其放到子线程中运行。例如:
threads.start(function() {
run_step();
});
ℹ️ 本回答由 AI 生成,内容仅供参考