Skip to content

Commit 681c9fa

Browse files
author
lamnd
committed
- Remove predefined patterns vibration on Android because it sucks
1 parent 13cd11c commit 681c9fa

6 files changed

Lines changed: 21 additions & 163 deletions

File tree

Modules/Vibration/README.md

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Native plugin for Unity for iOS and Android.
44
Use custom vibrations on mobile.
55

6-
Origin github repository: "https://github.com/BenoitFreslon/Vibration"
6+
Reference: "https://github.com/BenoitFreslon/Vibration", "https://gist.github.com/ruzrobert/d98220a3b7f71ccc90403e041967c46b"
77

88
# Use
99

@@ -15,70 +15,8 @@ Initialize the plugin with this line before using vibrations:
1515

1616
## Vibrations
1717

18-
### iOS and Android
19-
20-
#### Default vibration
21-
22-
Use `Vibration.Vibrate();` for a classic default ~400ms vibration
23-
24-
#### Pop vibration
25-
26-
Pop vibration: weak boom (For iOS: only available with the haptic engine. iPhone 6s minimum or Android)
27-
28-
`Vibration.VibratePop();`
29-
30-
#### Peek Vibration
31-
32-
Peek vibration: strong boom (For iOS: only available on iOS with the haptic engine. iPhone 6s minimum or Android)
33-
34-
`Vibration.VibratePeek();`
35-
36-
#### Nope Vibration
37-
38-
Nope vibration: series of three weak booms (For iOS: only available with the haptic engine. iPhone 6s minimum or Android)
39-
40-
`Vibration.VibrateNope();`
18+
Just call `Vibration.Vibrate(VibrationType type);`
4119

4220
## Enable/Disable
4321

44-
Set `Vibration.Enable` to true or false.
45-
46-
---
47-
## Android Only
48-
49-
#### Custom duration in milliseconds
50-
51-
`Vibration.Vibrate(500);`
52-
53-
#### Pattern
54-
55-
```
56-
long [] pattern = { 0, 1000, 1000, 1000, 1000 };
57-
Vibration.Vibrate ( pattern, -1 );
58-
```
59-
60-
#### Cancel
61-
62-
`Vibration.Cancel();`
63-
64-
---
65-
## IOS only
66-
vibration using haptic engine
67-
68-
`Vibration.VibrateIOS(ImpactFeedbackStyle.Light);`
69-
70-
`Vibration.VibrateIOS(ImpactFeedbackStyle.Medium);`
71-
72-
`Vibration.VibrateIOS(ImpactFeedbackStyle.Heavy);`
73-
74-
`Vibration.VibrateIOS(ImpactFeedbackStyle.Rigid);`
75-
76-
`Vibration.VibrateIOS(ImpactFeedbackStyle.Soft);`
77-
78-
`Vibration.VibrateIOS(NotificationFeedbackStyle.Error);`
79-
80-
`Vibration.VibrateIOS(NotificationFeedbackStyle.Success);`
81-
82-
`Vibration.VibrateIOS(NotificationFeedbackStyle.Warning);`
83-
84-
`Vibration.VibrateIOS_SelectionChanged();`
22+
Set `Vibration.Enable` to true or false.

Modules/Vibration/Vibration.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
using UnityEngine;
2-
3-
namespace LFramework.Vibration
1+
namespace LFramework.Vibration
42
{
53
public static class Vibration
64
{
75
public static bool Enabled = true;
86

7+
public static void Init()
8+
{
9+
#if UNITY_ANDROID
10+
VibrationAndroid.Init();
11+
#endif
12+
}
13+
914
public static void Vibrate(VibrationType type)
1015
{
1116
#if UNITY_IOS

Modules/Vibration/VibrationAndroid.cs

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -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
$"\nDevice has Vibrator = {HasVibrator()}" +
9380
$"\nDevice support Amplitude Control = {HasAmplitudeControl()}" +
94-
$"\nDevice support Predefined Effects = {_isSupportPredefinedEffect}");
81+
$"\nDefault 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

Modules/Vibration/VibrationIOS.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public static void Vibrate(VibrationType type)
9898
Handheld.Vibrate();
9999
break;
100100

101-
case VibrationType.Tick:
102101
case VibrationType.ImpactLight:
103102
Vibrate(ImpactFeedbackStyle.Light);
104103
break;
@@ -107,21 +106,10 @@ public static void Vibrate(VibrationType type)
107106
Vibrate(ImpactFeedbackStyle.Medium);
108107
break;
109108

110-
case VibrationType.ClickHeavy:
111109
case VibrationType.ImpactHeavy:
112110
Vibrate(ImpactFeedbackStyle.Heavy);
113111
break;
114112

115-
case VibrationType.ClickDouble:
116-
case VibrationType.Rigid:
117-
Vibrate(ImpactFeedbackStyle.Rigid);
118-
break;
119-
120-
case VibrationType.ClickSingle:
121-
case VibrationType.Soft:
122-
Vibrate(ImpactFeedbackStyle.Soft);
123-
break;
124-
125113
case VibrationType.Success:
126114
Vibrate(NotificationFeedbackStyle.Success);
127115
break;

Modules/Vibration/VibrationType.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,5 @@ public enum VibrationType
1212
Success,
1313
Failure,
1414
Warning,
15-
16-
// iOS
17-
Rigid,
18-
Soft,
19-
20-
// Android predefined patterns
21-
ClickSingle,
22-
ClickDouble,
23-
ClickHeavy,
24-
25-
Tick,
2615
}
2716
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "l.unity.frameworks",
33
"displayName": "LFramework",
4-
"version": "1.0.10",
4+
"version": "1.0.11",
55
"unity": "2022.3",
66
"description": "Collection of utility methods, design patterns, and extensions for Unity.",
77
"keywords": [

0 commit comments

Comments
 (0)