如何在WebGL中制作振动

问题描述 投票:0回答:1

我需要在 webgl 构建中振动手机。如果你可以调整持续时间和强度,那就太酷了。然而,我什至不知道如何发出正常的振动。

我尝试使用这个:https://gamedev.stackexchange.com/questions/177384/vibration-duration-for-mobile-devices-in-unity 但它不起作用,在互联网上我只找到了 30 美元的资产,所以我会很高兴得到任何帮助

unity-webgl
1个回答
0
投票

振动API可以使用,但在回答时,有些设备(主要是Safari)不支持。

统一脚本:

using System.Runtime.InteropServices;
using UnityEngine;

public class Vibration : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void Vibrate(int ms);
    
    private void Update()
    {
        if (Input.GetMouseButtonDown(0)) 
        {
#if !UNITY_EDITOR && UNITY_WEBGL
            Vibrate(1000);
#else
            Debug.Log("Simulate vibration");
#endif
        }
    }
}

JS代码(在Assets/Plugin/WebGL/Vibration.jslib中):

mergeInto(LibraryManager.library,
{
    Vibrate: function(duration)
    {
        if (typeof navigator.vibrate === "function") {
            navigator.vibrate(duration);
        }
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.