Search Unity

[Guide] Haptic Feedback on Android with no Plugin

Discussion in 'Android' started by Zocker1996, Jan 28, 2016.

  1. Zocker1996

    Zocker1996

    Joined:
    Jan 12, 2015
    Posts:
    20
    Hi!

    So it took me 4 hours to figure out Haptic Feedback on Android without a plugin (which would be easly made, but I just didn't want to).
    Keep in mind that this is just Haptic Feedback and not a fully functional vibrator!
    I would be happy if someone could post other, non-plugin Haptic Feedback solutions.
    I post this here since I think this is not Asset worthy and to see other, maybe better solutions.

    The advantages of my method are:
    -No need for vibration permission and thiswhile no custom AndroidManifest needed
    -If the user disables Haptic Feedback on his device in general, there is no Haptic Feedback here, too

    You would just call AndroidManager.HapticFeedback();


    Code (CSharp):
    1. using UnityEngine;
    2. public class AndroidManager : MonoBehaviour
    3. {
    4. private class HapticFeedbackManager
    5.     {
    6.     #if UNITY_ANDROID && !UNITY_EDITOR
    7.         private int HapticFeedbackConstantsKey;
    8.         private AndroidJavaObject UnityPlayer;
    9.     #endif
    10.  
    11.         public HapticFeedbackManager(){
    12.             #if UNITY_ANDROID && !UNITY_EDITOR
    13.             HapticFeedbackConstantsKey=new AndroidJavaClass("android.view.HapticFeedbackConstants").GetStatic<int>("VIRTUAL_KEY");
    14.             UnityPlayer=new AndroidJavaClass ("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer");
    15.             //Alternative way to get the UnityPlayer:
    16.             //int content=new AndroidJavaClass("android.R$id").GetStatic<int>("content");
    17.             //new AndroidJavaClass ("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity").Call<AndroidJavaObject>("findViewById",content).Call<AndroidJavaObject>("getChildAt",0);
    18.             #endif
    19.         }
    20.  
    21.         public bool Execute(){
    22.             #if UNITY_ANDROID && !UNITY_EDITOR
    23.             return UnityPlayer.Call<bool> ("performHapticFeedback",HapticFeedbackConstantsKey);
    24.             #endif
    25.             return false;
    26.         }
    27.     }
    28.  
    29.     //Cache the Manager for performance
    30.     private static HapticFeedbackManager mHapticFeedbackManager;
    31.  
    32.     public static bool HapticFeedback(){
    33.         if (mHapticFeedbackManager == null) {
    34.             mHapticFeedbackManager = new HapticFeedbackManager ();
    35.         }
    36.         return mHapticFeedbackManager.Execute ();
    37.     }
    38. }
     
    Last edited: Jan 28, 2016
  2. liptree

    liptree

    Joined:
    Apr 5, 2015
    Posts:
    1
    Just found this and want to say thank you.
     
  3. DuneZerna

    DuneZerna

    Joined:
    Nov 28, 2017
    Posts:
    5
    Thanks, helped a bunch!
     
  4. Glowball123

    Glowball123

    Joined:
    Mar 10, 2016
    Posts:
    78
    how do i control the intensity of the haptic feedback
     
    SolidAlloy and roy432002 like this.
  5. yablood

    yablood

    Joined:
    Jul 17, 2020
    Posts:
    1
    This is great and saved me a lot of time - thanks!!
     
  6. Lello1992X

    Lello1992X

    Joined:
    Dec 9, 2020
    Posts:
    1
    Wow!! Thanks very much you are fantastic!!
     
  7. ShirazAkber

    ShirazAkber

    Joined:
    Mar 20, 2018
    Posts:
    7
    Not working for me, Used your code and called AndroidManager.HapticFeedback(); whenever colliding with something but nothing seems to work any idea what i am doin wrong?
     
  8. Carocrazy132

    Carocrazy132

    Joined:
    Aug 16, 2015
    Posts:
    15
    Came back here to say Handheld.Vibrate() now exists so no one goes looking for plugins and support for different device types like we did.
     
    Nodata likes this.
  9. Arshil111

    Arshil111

    Joined:
    Aug 19, 2017
    Posts:
    8
    Thank You so much man