-
Star
(132)
You must be signed in to star a gist -
Fork
(29)
You must be signed in to fork a gist
-
-
Save aVolpe/707c8cf46b1bb8dfb363 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"); | |
#else | |
public static AndroidJavaClass unityPlayer; | |
public static AndroidJavaObject currentActivity; | |
public static AndroidJavaObject vibrator; | |
#endif | |
public static void Vibrate() | |
{ | |
if (isAndroid()) | |
vibrator.Call("vibrate"); | |
else | |
Handheld.Vibrate(); | |
} | |
public static void Vibrate(long milliseconds) | |
{ | |
if (isAndroid()) | |
vibrator.Call("vibrate", milliseconds); | |
else | |
Handheld.Vibrate(); | |
} | |
public static void Vibrate(long[] pattern, int repeat) | |
{ | |
if (isAndroid()) | |
vibrator.Call("vibrate", pattern, repeat); | |
else | |
Handheld.Vibrate(); | |
} | |
public static bool HasVibrator() | |
{ | |
return isAndroid(); | |
} | |
public static void Cancel() | |
{ | |
if (isAndroid()) | |
vibrator.Call("cancel"); | |
} | |
private static bool isAndroid() | |
{ | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
return true; | |
#else | |
return false; | |
#endif | |
} | |
} |
in unity2019.4.10f1,
get error c# exception:UnityEngine.AndroidJavaException: java.lang.NoSuchMethodError: no non-static method with name='vibrate' signature='()V' in class Landroid.os.SystemVibrator;
change vibrator.Call("vibrate");
to vibrator.Call("vibrate" ,100);
may fix it
didn't work with Vibration.vibrate(x) but it does with Vibration1.vibrate(x). didn't change the script Vibration.cs. The mystery of informatics ...
Other than that works perfect! thank you!!!
This doesn't seem to be working for me. I get the following error when running:
Vibration' is missing the class attribute 'ExtensionOfNativeClass'!
I changed the class name from
Vibration
-->Vibration1
and that got rid of the error. But when calling Vibration1.Vibrate() the console printsiPhoneUtils.Vibrate()
implying it's usingHandheld.Vibrate()
which isn't working for Android.(I'm using Unity 2018.3.0 and testing w/ UnityRemote 5)
Edit: found out that debugging in UnityRemote does not support vibrations... that's probably the crux of it. When building,
Handheld.Vibrate()
worked
Great code. Worrks well in Unity 2020.1.17f1. Thanks! Be blessed!
Thank you for this, everything works great!
Unity 2020.3.2f1 Personal
Vibration.Vibrate(100);
'Vibration' is missing the class attribute 'ExtensionOfNativeClass'!
how to Solve this error.......
Unity 2020.3.17f1
Thank you😊
Doesn't expect this still work in 2022.
You're a hero
@SamKyada , read the reply by @agustinaA-Lopez
meanwhile 2023, Android14, copy, paste, works like a charm.
Thanks @aVolpe , you're the man...
Thanks for good class.