diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dbaa53a..f1a251a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,7 +31,7 @@ jobs: run: | git checkout --orphan "$PKG_BRANCH" shopt -s extglob dotglob - rm -rf !(Packages|.github|.git|README.md|README.md.meta|LICENSE|LICENSE.meta|.|..) + rm -rf !(Packages|.github|.git|README.md|README.md.meta|LICENSE|LICENSE.meta|Images|.|..) ls -la ./ - name: Move ./Packages content to root run: ./.github/scripts/functions.sh movePackagesFolder diff --git a/Images/Icons.meta b/Images/Icons.meta new file mode 100644 index 0000000..5411c47 --- /dev/null +++ b/Images/Icons.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e7aa47501c634e5ba31545747f0ae7fb +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Images/Icons/android-icon.png b/Images/Icons/android-icon.png new file mode 100644 index 0000000..148da64 Binary files /dev/null and b/Images/Icons/android-icon.png differ diff --git a/Images/Icons/android-icon.png.meta b/Images/Icons/android-icon.png.meta new file mode 100644 index 0000000..fcee902 --- /dev/null +++ b/Images/Icons/android-icon.png.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: f933b0819250496e804e0bf9453c5cbc +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Images/Icons/ios-icon.png b/Images/Icons/ios-icon.png new file mode 100644 index 0000000..672e3db Binary files /dev/null and b/Images/Icons/ios-icon.png differ diff --git a/Images/Icons/ios-icon.png copy.meta b/Images/Icons/ios-icon.png copy.meta new file mode 100644 index 0000000..096a1e7 --- /dev/null +++ b/Images/Icons/ios-icon.png copy.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ec25a935b6df43ac805fcf5d194528eb +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Images/Icons/webgl-icon.png b/Images/Icons/webgl-icon.png new file mode 100644 index 0000000..786552b Binary files /dev/null and b/Images/Icons/webgl-icon.png differ diff --git a/Images/Icons/webgl-icon.png.meta b/Images/Icons/webgl-icon.png.meta new file mode 100644 index 0000000..78ea70e --- /dev/null +++ b/Images/Icons/webgl-icon.png.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 739deb37b1c340428412e0b793fd3da1 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Images/haptic-logo-color.png b/Images/haptic-logo-color.png new file mode 100644 index 0000000..47438bc Binary files /dev/null and b/Images/haptic-logo-color.png differ diff --git a/Images/haptic-logo-color.png.meta b/Images/haptic-logo-color.png.meta new file mode 100644 index 0000000..a672212 --- /dev/null +++ b/Images/haptic-logo-color.png.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c633211abc1145a5a486bfa5878cc0dd +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Images/vibration-component-inspector.png b/Images/vibration-component-inspector.png new file mode 100644 index 0000000..6f1dcc8 Binary files /dev/null and b/Images/vibration-component-inspector.png differ diff --git a/Images/vibration-component-inspector.png.meta b/Images/vibration-component-inspector.png.meta new file mode 100644 index 0000000..ffb2f99 --- /dev/null +++ b/Images/vibration-component-inspector.png.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 918dc7918d2243148352b5ef41ef4c9e +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md b/README.md index 3ee97e7..e41b92e 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,127 @@ -# Vibration +# Vibration/Haptics: Unity Plugin -Native **free** plugin for Unity for iOS and Android. +

+ +

+ +Native **free** plugin for Unity for Android, iOS and [WebGL](https://caniuse.com/webgl2) (with some limitations). Use custom vibrations on mobile. If you like this free plugin, that's be cool if you can buy me a coffee 😀☕️ Send tips to https://paypal.me/UnityVibrationPlugin -# Installation +## Supported Platforms + +- Aimeos logo Android +- Aimeos logo iOS +- Aimeos logo WebGL (some limitations apply on Mobile) -The minimal checked Unity Version is 2019.3.* LTS +## Installation + +The minimal checked Unity Version is **`2019.3.*`** LTS Open Package Manager and "Add package from git url..." using next string: -* `https://github.com/BenoitFreslon/Vibration.git` + +* `https://github.com/BenoitFreslon/Vibration.git#upm` + +Or use the latest git release/tag: + +* `https://github.com/BenoitFreslon/Vibration.git#0.1.0` You also can edit `Packages/manifest.json` manually, just add: -* `"com.benoitfreslon.vibration": "https://github.com/BenoitFreslon/Vibration.git",` -Or you can simply copy and paste the entire `Vibration` folder to your Unity3D `Assets` folder. +* `"com.benoitfreslon.vibration": "https://github.com/BenoitFreslon/Vibration.git#0.1.0",` + +Or you can simply copy and paste the entire `[upm]` branch content from this repo, to your Unity3D `Packages/com.benoitfreslon.vibration` folder. -# Use +## Getting Started -## Initialization +There are 2 ways of usage this plugin: -Initialize the plugin with this line before using vibrations: +1. Use the `Runtime/VibrationComponent.cs` script attached to a _gameObject_ **(Recommended)** -`Vibration.Init();` + ![Vibration Component](./Images/vibration-component-inspector.png) + + On that script, you can: + + - Enable/Disable vibration from inspector or programatically + > **TIP:** Useful for enable/disable from a menu settings in your game! + - Add a `ScriptableObject` asset with vibration settings (only enable/disable for now) + - Configure the duration and/or select a pre-defined vibration effect type + + This `MonoBehaviour` component use `Runtime/Vibration.cs` static class as a "_fallback_" for some implemented native integrations (**IOS** and **WebGL**) + +2. Use the `Runtime/Vibration.cs` static class + + See the scene and a sample `MonoBehaviour` script under folder: `Samples/VibrationExample` ## Vibrations +Using `Runtime/VibrationComponent` + +```csharp +// That's the main method to pass a duration (milliseconds) +// and/or a pre-defined `VibrationType` effect +Vibrate( + milliseconds: 20, + vibrationType: VibrationType.Click +); + +``` + +Optionally, you can pass an array of values of **_pattern_** as well: + +```csharp +// That's the main method to pass a duration (milliseconds) +// and/or a pre-defined `VibrationType` effect +Vibrate( + pattern: new[] { 200, 10, 50 }, + repeat: VibrationRepeat.Once +); + +``` + +Also, it's possible define the **timeunit** of the duration value: + +```csharp +// That's the main method to pass a duration (milliseconds) +// and/or a pre-defined `VibrationType` effect +Vibrate( + duration: 10, + timeUnit: MobileTimeUnit.Seconds +); + +``` + +Check if the mobile device has **vibration support**: + +```csharp +// Fallback to "Vibration.HasVibrator()" on iOS and WebGL +HasVibrator(); +``` + +### Android (only) + +Check if an Android [VibrationEffect](https://developer.android.com/reference/android/os/VibrationEffect) is supported: + +```csharp +// Where: "0" is `VibrationEffect.EFFECT_CLICK` value +// from native Android Kotlin/Java +IsEffectSupported(0); +``` + +Check if an Android [VibrationEffect.Composition](https://developer.android.com/reference/android/os/VibrationEffect.Composition) is supported: + +```csharp +// Where: "1" is `VibrationEffect.Composition.PRIMITIVE_CLICK` value +// from native Android Kotlin/Java +IsPrimitiveSupported(1); +``` + ### iOS and Android +Using `Runtime/Vibration.cs` static class + #### Default vibration Use `Vibration.Vibrate();` for a classic default ~400ms vibration @@ -53,25 +145,40 @@ Nope vibration: series of three weak booms (For iOS: only available with the hap `Vibration.VibrateNope();` --- + ## Android Only #### Custom duration in milliseconds -`Vibration.Vibrate(500);` +`Vibration.Vibrate(500);` #### Pattern -``` -long [] pattern = { 0, 1000, 1000, 1000, 1000 }; -Vibration.Vibrate ( pattern, -1 ); +```csharp +long[] pattern = { 0, 1000, 1000, 1000, 1000 }; +Vibration.Vibrate (pattern, -1); ``` #### Cancel -`Vibration.Cancel();` +Using `Runtime/VibrationComponent.cs` + +```csharp + +// Cancel for Android and WebGL +VibrationComponent.Cancel(); +``` + +Using `Runtime/Vibration.cs` static class + +```csharp +Vibration.Cancel(); +``` --- + ## IOS only + vibration using haptic engine `Vibration.VibrateIOS(ImpactFeedbackStyle.Light);` @@ -90,4 +197,17 @@ vibration using haptic engine `Vibration.VibrateIOS(NotificationFeedbackStyle.Warning);` -`Vibration.VibrateIOS_SelectionChanged();` \ No newline at end of file +`Vibration.VibrateIOS_SelectionChanged();` + +### References + +#### ANDROID + +- [Using Vibrate In Android](https://proandroiddev.com/using-vibrate-in-android-b0e3ef5d5e07) +- [Android 12: VibratorManager & New Vibration Primitives](https://yggr.medium.com/exploring-android-12-vibratormanager-new-vibration-primitives-e862c95fe938) +- [Developers Android: VibrationEffect](https://developer.android.com/reference/android/os/VibrationEffect) +- [Developers Android: VibrationEffect.Composition](https://developer.android.com/reference/android/os/VibrationEffect.Composition) + +#### ICONS (Copyright) + +Haptic icons created by Uniconlabs - Flaticon \ No newline at end of file