Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by TheSOULDev · Sep 14, 2017 at 09:14 PM · android buildvibrationvibrate

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zakirshikhli · Apr 13, 2018 at 05:45 PM 0
Share

Also native toast plugins not working (tested on 2017.4.1f1) this plugin

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image freseco · Dec 23, 2017 at 10:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges