- Home /
Did Unity 2017.1. deprecate Vibration on Android? My vibration methods no longer work on newer builds.
So, recently I've upgraded from 5.4.3f to 2017.1. I've had a Vibration section full of methods which had worked on builds prior to 2017.1, but now no longer work for some reason. The code is here:
static bool isAndroid()
{
#if UNITY_ANDROID && !UNITY_EDITOR
return true;
#else
return false;
#endif
}
#region 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");
#endif
public static void Vibrate(long ms)
{
#if UNITY_ANDROID && !UNITY_EDITOR
vibrator.Call("vibrate", ms);
#else
Handheld.Vibrate();
#endif
}
public static void Vibrate (long ms, float intensity)
{
#if UNITY_ANDROID && !UNITY_EDITOR
vibrator.Call("vibrate", CreateIntensity(ms, intensity), -1);
#else
Handheld.Vibrate();
#endif
}
public static void Vibrate(long[] WaitThenVibrate, int restartIndex)
{
#if UNITY_ANDROID && !UNITY_EDITOR
vibrator.Call("vibrate", WaitThenVibrate, restartIndex);
#else
Handheld.Vibrate();
#endif
}
public static bool HasVibrator()
{
return isAndroid();
}
public static void Cancel()
{
#if UNITY_ANDROID && !UNITY_EDITOR
vibrator.Call("cancel");
#endif
}
static float GetVibrate(float intensity, int NumberOfQuantums)
{
return math.round(intensity * NumberOfQuantums);
}
static long[] CreateIntensity (long ms, float intensity)
{
if (intensity < 0.025f)
return new long[] { ms, 0 };
else if (intensity >= 0.975)
return new long[] {0, ms};
else
{
int pairs = (int)(ms / Constants.NumberOfVibrateParts) + ((int)ms % Constants.NumberOfVibrateParts);
if (ms < Constants.NumberOfVibrateParts)
{
long[] toReturn = new long[4 * pairs];
long VibrateTime = (long)Mathf.Clamp(GetVibrate(intensity, Constants.NumberOfVibrateParts / 2) + (intensity > 0.45f ? 2 : 0), 0, Constants.NumberOfVibrateParts);
long DelayTime = (Constants.NumberOfVibrateParts / 2) - VibrateTime;
for (int i = 0; i < toReturn.Length;)
{
toReturn[i++] = DelayTime;
toReturn[i++] = VibrateTime;
}
return toReturn;
}
else
{
long[] toReturn = new long[2 * pairs];
long VibrateTime = (long)Mathf.Clamp(GetVibrate(intensity, Constants.NumberOfVibrateParts) + (intensity > 0.45f ? 3 : 0), 0, Constants.NumberOfVibrateParts);
long DelayTime = Constants.NumberOfVibrateParts - VibrateTime;
for (int i = 0; i < toReturn.Length;)
{
toReturn[i++] = DelayTime;
toReturn[i++] = VibrateTime;
}
return toReturn;
}
}
}
#endregion
After building in 2017.1 my vibration stopped working. I call it in methods I call when touching a button. An example of my use would be this:
public void OnButtonX ()
{
StartCoroutine(OpenMenu());
Statics.android.Vibrate(60, 0.35f);
}
However, they no longer work. I am unsure why. As far as I know, I should still have the permission to vibrate. The API level is 16. I'm testing this on a Resurrection Remix (Android 7.1) phone. The vibrate method runs surely because I can see the phoneutils.Vibrate in the editor every time I press a button.
Anyone had a similar issue or has an idea how to fix this?
Also native toast plugins not working (tested on 2017.4.1f1) this plugin
Answer by MaxPirat · Oct 09, 2017 at 05:06 PM
I tried almost the same code in my 2017.1 project and it didn't work on the device also.
I have the same problem in Unity 2017.3.0f3.
Handheld.Vibrate(); doesn't work.
even calling the native function vibrate() and adding the permissions on Android$$anonymous$$anifest.xml.
Your answer
Follow this Question
Related Questions
Regulate Vibration 2 Answers
How to control vibration duration of Handheld.Vibrate()? 2 Answers
Rigidbody, what the hell? *Vibrating* 0 Answers
How to change type of vibration on Android/IOS? 3 Answers
Haptic Feedback Android 0 Answers