Skip to content

Commit

Permalink
[Docs] Updated README with VibrationComponent usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper committed Sep 25, 2023
1 parent 1dc7ff3 commit afa347e
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Images/Icons.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Images/Icons/android-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Images/Icons/android-icon.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Images/Icons/ios-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Images/Icons/ios-icon.png copy.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Images/Icons/webgl-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Images/Icons/webgl-icon.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Images/haptic-logo-color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Images/haptic-logo-color.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Images/vibration-component-inspector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Images/vibration-component-inspector.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 137 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,127 @@
# Vibration
# Vibration/Haptics: Unity Plugin

Native **free** plugin for Unity for iOS and Android.
<h1 align="left">
<img src="./Images/haptic-logo-color.png">
</h1>

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

- <img src="./Images/Icons/android-icon.png" alt="Aimeos logo" title="Android" align="center" height="32" /> Android
- <img src="./Images/Icons/ios-icon.png" alt="Aimeos logo" title="iOS" align="center" height="32" /> iOS
- <img src="./Images/Icons/webgl-icon.png" alt="Aimeos logo" title="iOS" align="center" height="32" /> <a href="https://caniuse.com/webgl2"> WebGL </a> (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
Expand All @@ -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);`
Expand All @@ -90,4 +197,17 @@ vibration using haptic engine

`Vibration.VibrateIOS(NotificationFeedbackStyle.Warning);`

`Vibration.VibrateIOS_SelectionChanged();`
`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)

<a href="https://www.flaticon.com/free-icons/haptic" title="haptic icons">Haptic icons created by Uniconlabs - Flaticon</a>

0 comments on commit afa347e

Please sign in to comment.