Skip to content

Instantly share code, notes, and snippets.

@playfulbacon
Last active May 20, 2020 17:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save playfulbacon/4ff08fcdf7ab0c023118874f5339bf7a to your computer and use it in GitHub Desktop.
Save playfulbacon/4ff08fcdf7ab0c023118874f5339bf7a to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public static class Vibration
{
#if UNITY_ANDROID && !UNITY_EDITOR
public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
public static AndroidJavaClass vibrationEffectClass = new AndroidJavaClass("android.os.VibrationEffect");
public static int defaultAmplitude = vibrationEffectClass.GetStatic<int>("DEFAULT_AMPLITUDE");
public static AndroidJavaClass androidVersion = new AndroidJavaClass("android.os.Build$VERSION");
public static int apiLevel = androidVersion.GetStatic<int>("SDK_INT");
#else
public static AndroidJavaClass unityPlayer;
public static AndroidJavaObject vibrator;
public static AndroidJavaObject currentActivity;
public static AndroidJavaClass vibrationEffectClass;
public static int defaultAmplitude;
#endif
public static void Vibrate(long milliseconds)
{
CreateOneShot(milliseconds, defaultAmplitude);
}
public static void CreateOneShot(long milliseconds, int amplitude)
{
CreateVibrationEffect("createOneShot", new object[] { milliseconds, amplitude });
}
public static void CreateWaveform(long[] timings, int repeat)
{
CreateVibrationEffect("createWaveform", new object[] { timings, repeat });
}
public static void CreateWaveform(long[] timings, int[] amplitudes, int repeat)
{
CreateVibrationEffect("createWaveform", new object[] { timings, amplitudes, repeat });
}
public static void CreateVibrationEffect(string function, params object[] args)
{
if (isAndroid() && HasAmplituideControl())
{
AndroidJavaObject vibrationEffect = vibrationEffectClass.CallStatic<AndroidJavaObject>(function, args);
vibrator.Call("vibrate", vibrationEffect);
}
else
Handheld.Vibrate();
}
public static bool HasVibrator()
{
return vibrator.Call<bool>("hasVibrator");
}
public static bool HasAmplituideControl()
{
if (apiLevel >= 26)
return vibrator.Call<bool>("hasAmplitudeControl"); // API 26+ specific
else
return false; // no amplitude control below API level 26
}
public static void Cancel()
{
if (isAndroid())
vibrator.Call("cancel");
}
private static bool isAndroid()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return true;
#else
return false;
#endif
}
}
@playfulbacon
Copy link
Author

Modified this Gist by @aVolpe to support VibrationEffect introduced in Android API level 26.

@benbyford
Copy link

what is an example of the data passed in as timings?

@freseco
Copy link

freseco commented Dec 23, 2017

I've tried this code but it doesn't work, even adding the permission on AndroidManifiest.xml.
This is the error: An exception was thrown by the type initializer for Vibration.

Vibration.Vibrate(1000);

I am using Unity2017.3.0f3.

@kyl3r92
Copy link

kyl3r92 commented Dec 28, 2017

Does it work with API levels < 26, too?

edit: Nope, crashes. Should I add try-catch (+ remember to fallback to "old" vibration, when catch was called?)

@munkbusiness
Copy link

HasVibrator and HasAmplitude are both calls and not fields, so you have to access them with call instead of get, it took me a bit to find out that my if condition was the one crashing the app.

I adapted this solution together with the original @aVolpe version to make something that works with new AND old androids: https://gist.github.com/munkbusiness/9e0a7d41bb9c0eb229fd8f2313941564

@ruzrobert
Copy link

Hi all! I've made a drastically improved version.

Script automatically detects Api Level. Checks if Amplitude control (API >= 26) is available. Also supports Predefined Effects (API >= 29).
If API is < 26, legacy functions are used (old api).

Code is available here: github gist link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment