89 lines
1.9 KiB
C#
89 lines
1.9 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace OHM.UnityToolkit.Vibration
|
|
{
|
|
public class VibrationManager : MonoBehaviour
|
|
{
|
|
public static bool Enabled = true;
|
|
|
|
public static float MinDelay = 0.1f;
|
|
|
|
public static long AndroidSmallLen = 25L;
|
|
|
|
public static int AndroidSmallAmplitude = 64;
|
|
|
|
public static long AndroidMediumLen = 100L;
|
|
|
|
public static int AndroidMediumAmplitude = 128;
|
|
|
|
public static long AndroidBigLen = 300L;
|
|
|
|
public static int AndroidBigAmplitude = -1;
|
|
|
|
public static bool LogEnabled = true;
|
|
|
|
private static float _lastVibrationFrameTime = -1f;
|
|
|
|
private static VibrationType _lastVibrationType;
|
|
|
|
public static void Vibrate(VibrationType type)
|
|
{
|
|
if (!Enabled)
|
|
{
|
|
if (LogEnabled)
|
|
{
|
|
Debug.Log("Vibration disabled");
|
|
}
|
|
return;
|
|
}
|
|
float sinceLast = Time.time - _lastVibrationFrameTime;
|
|
if (sinceLast < MinDelay && _lastVibrationType >= type)
|
|
{
|
|
if (LogEnabled)
|
|
{
|
|
Debug.LogFormat("Vibration ignored (lower or equal type {0} / {1} and delay not done {2}) ",
|
|
type.ToString(), _lastVibrationType.ToString(), sinceLast);
|
|
}
|
|
return;
|
|
}
|
|
_lastVibrationFrameTime = Time.time;
|
|
_lastVibrationType = type;
|
|
long milliseconds;
|
|
int amplitude;
|
|
switch (type)
|
|
{
|
|
case VibrationType.BIG:
|
|
milliseconds = AndroidBigLen;
|
|
amplitude = AndroidBigAmplitude;
|
|
break;
|
|
case VibrationType.MEDIUM:
|
|
milliseconds = AndroidMediumLen;
|
|
amplitude = AndroidMediumAmplitude;
|
|
break;
|
|
case VibrationType.SMALL:
|
|
milliseconds = AndroidSmallLen;
|
|
amplitude = AndroidSmallAmplitude;
|
|
break;
|
|
default:
|
|
milliseconds = 0L;
|
|
amplitude = -1;
|
|
break;
|
|
}
|
|
AndroidVibrationManager.CreateOneShot(milliseconds, amplitude);
|
|
if (LogEnabled)
|
|
{
|
|
Debug.Log("Vibrate " + type.ToString());
|
|
}
|
|
}
|
|
|
|
public enum VibrationType
|
|
{
|
|
SMALL = 0,
|
|
MEDIUM = 1,
|
|
BIG = 2,
|
|
}
|
|
|
|
}
|
|
}
|