使用触觉振动
4 分
本指南将向您展示如何在 ZEPETOScrip 中基于 Handheld.Vibrate 函数实现触觉振动,该函数由 Unity 提供。
编写脚本
1) 在 [项目] 面板中,选择左上角的 [+] 菜单或选择资产 > 创建 > ZEPETO > TypeScript。

2) 将添加的脚本命名为 HapticVibration,并编写如下示例脚本。
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { Button } from 'UnityEngine.UI';
import { Handheld } from 'UnityEngine';
export default class HapticVibration extends ZepetoScriptBehaviour {
public vibrationButton: Button;
Start() {
this.vibrationButton.onClick.AddListener(() => {
Handheld.Vibrate();
});
}
}
3) 将一个 GameObject 添加到场景中并重命名为 HapticFeedback。添加您创建的 HapticVibration ZEPETOScript。 在层级窗口中,将按钮 UI 拖放到 ZEPETOScript 检查器窗口中显示的属性上。

在移动设备上测试
1) 使用 QR 移动测试运行示例世界。
2) 当您按下与脚本连接的按钮时,您的设备将会震动。
📘 请参考以下指南。 [QR 移动测试]
❗️ 注意 请注意,Handheld.Vibrate 仅在移动环境中有效。