com.benoitfreslon.vibration 0.1.1
Install from the command line:
Learn more about npm packages
$ npm install @mfdeveloper/com.benoitfreslon.vibration@0.1.1
Install via package.json:
"@mfdeveloper/com.benoitfreslon.vibration": "0.1.1"
About this version
PS: This is a fork of original Vibration package BenoitFreslon/Vibration to be published in openupm registry, while the changes aren't merged into it!
Native free plugin for Unity for Android, iOS and WebGL (with some limitations). Use custom vibrations/haptics 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
-
Android
-
iOS
-
WebGL (some limitations apply on Mobile)
Install the OpenUPM CLI and add the com.benoitfreslon.vibration package
# Install openupm-cli
npm install -g openupm-cli
# Go to your Unity project directory
cd YOUR_UNITY_PROJECT_DIR
# Install package: com.benoitfreslon.vibration
openupm add com.benoitfreslon.vibration
Or alternatively, merge the snippet below to Packages/manifest.json manually
-
Add a
$HOME/.upmconfig.toml
file with the TOKEN to authenticate on registry[npmAuth."https://npm.pkg.github.com/@mfdeveloper"] # Generate the token from your github profile: # https://github.com/settings/tokens _authToken = "<TOKEN-VALUE>" email = "<YOUR_EMAIL>" alwaysAuth = true
WARNING: GitHub/Gitlab Packages registry always requires authentication. For private and public packages.
-
Configure the scope registry and dependency in your Unity project
Packages/manifest.json
{ "dependencies": { // Verifiy which is the latest tag in this repository "com.benoitfreslon.vibration": "<LATEST_VERSION>" }, "scopedRegistries": [ { "name": "Github Packages: mfdeveloper", "url": "https://npm.pkg.github.com/@mfdeveloper", "scopes": [ "com.benoitfreslon" ] } ] }
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#upm
Or use the latest git release/tag:
https://github.com/BenoitFreslon/Vibration.git#<LATEST_VERSION>
You also can edit Packages/manifest.json
manually, just add:
"com.benoitfreslon.vibration": "https://github.com/BenoitFreslon/Vibration.git#<LATEST_VERSION>",
Or you can simply copy and paste the entire [upm]
branch content from this repo, to your Unity3D Packages/com.benoitfreslon.vibration
folder.
There are 2 ways to use this plugin:
-
Use the
Runtime/VibrationComponent.cs
script attached to a gameObject (Recommended)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 useRuntime/Vibration.cs
static class as a "fallback" for some implemented native integrations (IOS and WebGL) - Enable/Disable vibration from inspector or programatically
-
Use the
Runtime/Vibration.cs
static classSee the scene and a sample
MonoBehaviour
script under folder:Samples/VibrationExample
Using Runtime/VibrationComponent
// 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:
// 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:
// 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:
// Fallback to "Vibration.HasVibrator()" on iOS and WebGL
HasVibrator();
Check if an Android VibrationEffect is supported:
// Where: "0" is `VibrationEffect.EFFECT_CLICK` value
// from native Android Kotlin/Java
IsEffectSupported(0);
Check if an Android VibrationEffect.Composition is supported:
// Where: "1" is `VibrationEffect.Composition.PRIMITIVE_CLICK` value
// from native Android Kotlin/Java
IsPrimitiveSupported(1);
Using Runtime/Vibration.cs
static class
Use Vibration.Vibrate();
for a classic default ~400ms vibration
Pop vibration: weak boom (For iOS: only available with the haptic engine. iPhone 6s minimum or Android)
Vibration.VibratePop();
Peek vibration: strong boom (For iOS: only available on iOS with the haptic engine. iPhone 6s minimum or Android)
Vibration.VibratePeek();
Nope vibration: series of three weak booms (For iOS: only available with the haptic engine. iPhone 6s minimum or Android)
Vibration.VibrateNope();
Vibration.Vibrate(500);
long[] pattern = { 0, 1000, 1000, 1000, 1000 };
Vibration.Vibrate (pattern, -1);
Using Runtime/VibrationComponent.cs
// Cancel for Android and WebGL
VibrationComponent.Cancel();
Using Runtime/Vibration.cs
static class
Vibration.Cancel();
vibration using haptic engine
Vibration.VibrateIOS(ImpactFeedbackStyle.Light);
Vibration.VibrateIOS(ImpactFeedbackStyle.Medium);
Vibration.VibrateIOS(ImpactFeedbackStyle.Heavy);
Vibration.VibrateIOS(ImpactFeedbackStyle.Rigid);
Vibration.VibrateIOS(ImpactFeedbackStyle.Soft);
Vibration.VibrateIOS(NotificationFeedbackStyle.Error);
Vibration.VibrateIOS(NotificationFeedbackStyle.Success);
Vibration.VibrateIOS(NotificationFeedbackStyle.Warning);
Vibration.VibrateIOS_SelectionChanged();
-
Generate a Github access token (classic)
-
Authenticate using the generated TOKEN:
Create a user
$HOME/.npmrc
file# Where: <GITHUB_TOKEN> is the access token (classic) generated in your profile @github:registry=https://npm.pkg.github.com //npm.pkg.github.com/:_authToken=<GITHUB_TOKEN>
Or, use the file
Packages/com.benoitfreslon.vibration/.npmrc
that already exists in this repository:# Export the environment variable "$GITHUB_TOKEN" # the file ".npmrc" inside of this repo will use this variable export GITHUB_TOKEN=<GITHUB_TOKEN>
-
Publish the package with npm publish
From
[upm-package-embedded]
git branch# Clone this repo and checkout to branch "[upm-package-embedded]" git clone https://github.com/mfdeveloper/Vibration.git git checkout upm-package-embedded # Run the npm scripts npm install npm run publish:package # Publish from repository ROOT path cd Packages/com.benoitfreslon.vibration npm publish # Publish from package path
-
Bump the version with a new Github release or Git tag
-
The changes should be reflected in: https://openupm.com/packages/com.benoitfreslon.vibration
- Using Vibrate In Android
- Android 12: VibratorManager & New Vibration Primitives
- Developers Android: VibrationEffect
- Developers Android: VibrationEffect.Composition