@@ -42,18 +42,14 @@ public static class VibrationAndroid
4242 // Available only from Api >= 26
4343 private static bool _isSupportVibrationEffect { get { return _apiLevel >= 26 ; } }
4444
45- // Available only from Api >= 29
46- private static bool _isSupportPredefinedEffect { get { return _apiLevel >= 29 ; } }
47-
4845 #region Initialization
4946
50- [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
51- private static void Initialize ( )
47+ public static void Init ( )
5248 {
5349 if ( _isInitialized )
5450 return ;
5551
56- if ( Application . isEditor )
52+ if ( Application . platform != RuntimePlatform . Android )
5753 return ;
5854
5955 // Get Api Level
@@ -76,22 +72,13 @@ private static void Initialize()
7672 _vibrationEffectClass = new AndroidJavaClass ( "android.os.VibrationEffect" ) ;
7773 _defaultAmplitude = Mathf . Clamp ( _vibrationEffectClass . GetStatic < int > ( "DEFAULT_AMPLITUDE" ) , 1 , 255 ) ;
7874 }
79-
80- // If device supports predefined effects, get their IDs
81- if ( _isSupportPredefinedEffect )
82- {
83- PredefinedEffect . CLICK = _vibrationEffectClass . GetStatic < int > ( "EFFECT_CLICK" ) ;
84- PredefinedEffect . DOUBLE_CLICK = _vibrationEffectClass . GetStatic < int > ( "EFFECT_DOUBLE_CLICK" ) ;
85- PredefinedEffect . HEAVY_CLICK = _vibrationEffectClass . GetStatic < int > ( "EFFECT_HEAVY_CLICK" ) ;
86- PredefinedEffect . TICK = _vibrationEffectClass . GetStatic < int > ( "EFFECT_TICK" ) ;
87- }
8875 }
8976 }
9077
9178 LDebug . Log ( typeof ( VibrationAndroid ) , $ "Initialized" +
9279 $ "\n Device has Vibrator = { HasVibrator ( ) } " +
9380 $ "\n Device support Amplitude Control = { HasAmplitudeControl ( ) } " +
94- $ "\n Device support Predefined Effects = { _isSupportPredefinedEffect } ") ;
81+ $ "\n Default amplitude = { _defaultAmplitude } ") ;
9582
9683 _isInitialized = true ;
9784 }
@@ -112,7 +99,6 @@ public static void Vibrate(VibrationType type)
11299 Vibrate ( _lightDuration , _lightAmplitude ) ;
113100 break ;
114101
115- case VibrationType . Rigid :
116102 case VibrationType . ImpactMedium :
117103 Vibrate ( _mediumDuration , _mediumAmplitude ) ;
118104 break ;
@@ -133,24 +119,8 @@ public static void Vibrate(VibrationType type)
133119 Vibrate ( _warningPattern , _warningPatternAmplitude ) ;
134120 break ;
135121
136- case VibrationType . Tick :
137- Vibrate ( PredefinedEffect . TICK ) ;
138- break ;
139-
140- case VibrationType . ClickSingle :
141- Vibrate ( PredefinedEffect . CLICK ) ;
142- break ;
143-
144- case VibrationType . ClickDouble :
145- Vibrate ( PredefinedEffect . DOUBLE_CLICK ) ;
146- break ;
147-
148- case VibrationType . ClickHeavy :
149- Vibrate ( PredefinedEffect . HEAVY_CLICK ) ;
150- break ;
151-
152122 default :
153- LDebug . Log ( typeof ( VibrationAndroid ) , $ "Undefined vibrate type { type } ") ;
123+ LDebug . Log ( typeof ( VibrationAndroid ) , $ "Undefined vibration type { type } ") ;
154124 break ;
155125 }
156126 }
@@ -160,10 +130,10 @@ public static void Vibrate(VibrationType type)
160130 /// If amplitude is -1, amplitude is Disabled. If -1, device DefaultAmplitude is used. Otherwise, values between 1-255 are allowed.
161131 /// If 'cancel' is true, Cancel() will be called automatically.
162132 /// </summary>
163- public static void Vibrate ( long milliseconds , int amplitude = - 1 , bool cancel = false )
133+ public static void Vibrate ( long milliseconds , int amplitude = 0 , bool cancel = false )
164134 {
165135 // Lazy initialize
166- Initialize ( ) ;
136+ Init ( ) ;
167137
168138 if ( ! HasVibrator ( ) )
169139 return ;
@@ -176,17 +146,14 @@ public static void Vibrate(long milliseconds, int amplitude = -1, bool cancel =
176146 // Validate amplitude
177147 amplitude = Mathf . Clamp ( amplitude , - 1 , 255 ) ;
178148
179- // If -1 , disable amplitude (use maximum amplitude)
180- if ( amplitude == - 1 )
149+ // If less -1 or don't have amplitude control , disable amplitude (use maximum amplitude)
150+ if ( amplitude <= - 1 || ! HasAmplitudeControl ( ) )
181151 amplitude = 255 ;
182152
183153 // If 0, use device DefaultAmplitude
184154 if ( amplitude == 0 )
185155 amplitude = _defaultAmplitude ;
186156
187- // If amplitude is not supported, use 255; if amplitude is -1, use systems DefaultAmplitude. Otherwise use user-defined value.
188- amplitude = ! HasAmplitudeControl ( ) ? 255 : amplitude ;
189-
190157 VibrateEffect ( milliseconds , amplitude ) ;
191158 }
192159 else
@@ -204,7 +171,7 @@ public static void Vibrate(long milliseconds, int amplitude = -1, bool cancel =
204171 public static void Vibrate ( long [ ] pattern , int [ ] amplitudes = null , int repeat = - 1 , bool cancel = false )
205172 {
206173 // Lazy initialize
207- Initialize ( ) ;
174+ Init ( ) ;
208175
209176 if ( ! HasVibrator ( ) )
210177 return ;
@@ -240,27 +207,6 @@ public static void Vibrate(long[] pattern, int[] amplitudes = null, int repeat =
240207 }
241208 }
242209
243- /// <summary>
244- /// Vibrate predefined effect (described in Vibration.PredefinedEffect). Available from Api Level >= 29.
245- /// If 'cancel' is true, Cancel() will be called automatically.
246- /// </summary>
247- public static void VibratePredefined ( int effectId , bool cancel = false )
248- {
249- // Lazy initialize
250- Initialize ( ) ;
251-
252- if ( ! HasVibrator ( ) )
253- return ;
254-
255- if ( ! _isSupportPredefinedEffect )
256- return ;
257-
258- if ( cancel )
259- Cancel ( ) ;
260-
261- VibrateEffectPredefined ( effectId ) ;
262- }
263-
264210 /// <summary>
265211 /// Returns true if device has vibrator
266212 /// </summary>
@@ -376,14 +322,6 @@ private static void ClampAmplitudesArray(int[] amplitudes)
376322 }
377323
378324 #endregion
379-
380- public static class PredefinedEffect
381- {
382- public static int CLICK ;
383- public static int DOUBLE_CLICK ;
384- public static int HEAVY_CLICK ;
385- public static int TICK ;
386- }
387325 }
388326}
389327
0 commit comments