Gamepad类提供了一个方法 Gamepad.current.SetMotorSpeeds , 这次的核心就是使用这一个方法, 话不多说直接放代码
public void GamepadVibrate(float low, float high, float time) => StartCoroutine(IEGamepadVibrate(low, high, time));
public IEnumerator IEGamepadVibrate(float low, float high, float time)
{
//防止因未连接手柄造成的 DebugError
if (Gamepad.current == null)
yield break;
//设置手柄的 震动速度 以及 恢复震动 , 计时到达之后暂停震动
Gamepad.current.SetMotorSpeeds(low, high);
Gamepad.current.ResumeHaptics();
var endTime = Time.time + time;
while (Time.time < endTime)
{
Gamepad.current.ResumeHaptics();
yield return null;
}
if (Gamepad.current == null)
yield break;
Gamepad.current.PauseHaptics();
}