ArkanoidTG/Assets/Scripts/HapticManager.cs

67 lines
1.6 KiB
C#
Raw Permalink Normal View History

2024-11-25 23:14:57 +03:00
using UnityEngine;
public class HapticManager : MonoBehaviour
{
private static HapticManager _instance;
public static HapticManager Instance => _instance;
#if !UNITY_EDITOR && UNITY_WEBGL
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern bool HapticFeedback(int duration);
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern int IsHapticSupported();
#endif
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
#if !UNITY_EDITOR && UNITY_WEBGL
Debug.Log($"Haptic feedback supported: {IsHapticSupported() == 1}");
#endif
}
public void LightTap()
{
TriggerHapticFeedback(20);
}
public void MediumTap()
{
TriggerHapticFeedback(40);
}
public void HeavyTap()
{
TriggerHapticFeedback(80);
}
private void TriggerHapticFeedback(int duration)
{
#if !UNITY_EDITOR && UNITY_WEBGL
try
{
if (IsHapticSupported() == 1)
{
bool success = HapticFeedback(duration);
if (success)
{
Debug.Log($"Haptic feedback triggered: {duration}ms");
}
}
}
catch (System.Exception e)
{
Debug.LogWarning($"Failed to trigger haptic feedback: {e.Message}");
}
#endif
}
}