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 26, 2023
1 parent 074f81f commit 144ae4a
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 21 deletions.
9 changes: 7 additions & 2 deletions .github/scripts/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ packageVersion() {
echo $VERSION
}

# @description Add github actions state and output variables to be handled on .yml workflow files
#
# @see [shdoc](https://github.com/reconquest/shdoc)
# @see [Deprecating save-state and set-output commands](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands)
githubActionsOutputs() {
CURRENT_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo ::set-output name=tag::$CURRENT_TAG
echo ::set-output name=commit_message::$COMMIT_MESSAGE
# Use the format {name}={value} instead of ::set-output
echo "{tag}={$CURRENT_TAG}" >> $GITHUB_OUTPUT
echo "{commit_message}={$COMMIT_MESSAGE}" >> $GITHUB_OUTPUT
}

copyPackagesContent() {
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ on:
repository_dispatch:
types: [on-demand-test]
env:
# PS: You can define github action variables in a repository, without modify this file
# from: https://github.com/[profile]/[repository]/settings/variables/actions
PKG_BRANCH: upm
PKG_NAME: com.benoitfreslon.vibration
PKG_ROOT: ./
EXCLUDE_REMOVE_FILES: '!(Packages|.github|.git|README.md|README.md.meta|LICENSE|LICENSE.meta|Images|Images.meta|.|..)'
jobs:
split-upm:
name: Create upm package branch (force)
Expand All @@ -31,7 +34,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 $EXCLUDE_REMOVE_FILES
ls -la ./
- name: Move ./Packages content to root
run: ./.github/scripts/functions.sh movePackagesFolder
Expand Down
7 changes: 7 additions & 0 deletions Images.meta

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

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.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.

2 changes: 1 addition & 1 deletion Packages/com.benoitfreslon.vibration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.benoitfreslon.vibration",
"author": "Benoit Freslon, AminSojoudi, SH42913",
"displayName": "Vibration",
"description": "Native free plugin for Unity for iOS and Android. Use custom vibrations on mobile.",
"description": "Native free plugin for Unity for Android, iOS and WebGL. Use custom vibrations on mobile.",
"unity": "2019.3",
"version": "0.1.0",
"keywords": [],
Expand Down
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 144ae4a

Please sign in to comment.