Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Feedback Haptic feedback class

Discussion in 'Android' started by stav_pm, Nov 9, 2019 at 8:06 PM.

  1. stav_pm

    stav_pm

    Joined:
    Saturday
    Posts:
    1
    Hello everyone,
    So, I made this class in unity for haptic feedback but unfortunately as I figured out my device does not support neither the Vibrator class nor the HapticFeedback (both Vibrator.hasVibrator and View.isHapticFeedbackEnabled return false). Can someone tell me if the code below works with a device that has either vibration or haptic?

    Here is the code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AndroidHapticFeedback
    4. {
    5.     //VibrationEffect Constants
    6.     public const int EFFECT_CLICK           =  0;   //api 29
    7.     public const int EFFECT_DOUBLE_CLICK    =  1;   //api 29
    8.     public const int EFFECT_TICK            =  2;   //api 29
    9.     public const int EFFECT_HEAVY_CLICK     =  5;   //api 29
    10.  
    11.     //HapticFeedback Constants
    12.     public const int LONG_PRESS             = 0;   //api 3
    13.     public const int VIRTUAL_KEY            = 1;   //api 5
    14.     public const int KEYBOARD_TAP           = 3;   //api 8
    15.     public const int KEYBOARD_PRESS         = 3;   //api 27
    16.     public const int CLOCK_TICK             = 4;   //api 21
    17.     public const int CONTEXT_CLICK          = 6;   //api 23
    18.     public const int KEYBOARD_RELEASE       = 7;   //api 27
    19.     public const int VIRTUAL_KEY_RELEASE    = 8;   //api 27
    20.  
    21. #if UNITY_ANDROID
    22.     private int apiLevel;
    23.     private bool hasVibrator;
    24.     private bool hasHapticFeedback;
    25.     private AndroidJavaClass vibratorClass;
    26.     private AndroidJavaClass vibrationEffectClass;
    27.     private AndroidJavaClass viewClass;
    28.     //private AndroidJavaClass unityActivity;
    29. #endif
    30.     public AndroidHapticFeedback()
    31.     {
    32. #if UNITY_ANDROID
    33.         apiLevel = GetSDKInt();
    34.  
    35.         vibratorClass = new AndroidJavaClass("android.os.Vibrator");
    36.         hasVibrator = vibratorClass.Call<bool>("hasVibrator");
    37.         if (hasVibrator)
    38.         {
    39.             vibrationEffectClass = new AndroidJavaClass("android.os.VibrationEffect");
    40.         }
    41.         else
    42.         {
    43.             viewClass = new AndroidJavaClass("android.view.View");
    44.             hasHapticFeedback = viewClass.Call<bool>("isHapticFeedbackEnabled");
    45.             Toast.ShowToast("Has haptic: " + hasHapticFeedback.ToString());
    46.         }
    47.  
    48.         //unityActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    49. #endif
    50.     }
    51.  
    52. #if UNITY_ANDROID
    53.     private int GetSDKInt()
    54.     {
    55.         var version = new AndroidJavaClass("android.os.Build$VERSION");
    56.         return version.GetStatic<int>("SDK_INT");
    57.     }
    58. #endif
    59.  
    60.     /// <summary>
    61.     /// Gives the desired haptic feedback / vibration pattern if the device supports it
    62.     /// TODO: need to find better method to determine what effect to play when an unavailable one is provided
    63.     /// </summary>
    64.     /// <param name="vibrationEffect">One of the constants of the AndroidHapticFeedback class</param>
    65.     public void Execute(int vibrationEffect)
    66.     {
    67. #if UNITY_ANDROID
    68.         if (hasVibrator && apiLevel >= 29) // vibration effects are only supported with api level 29 and above (android 10)
    69.         {
    70.             vibratorClass.Call("vibrate", vibrationEffectClass.CallStatic<AndroidJavaClass>("createPredefined", vibrationEffect));
    71.         }
    72.         else if(hasHapticFeedback && apiLevel >= 3)
    73.         {
    74.             viewClass.Call<bool>("performHapticFeedback", vibrationEffect);
    75.         }
    76. #endif
    77.     }
    78. }