Skip to content

Commit

Permalink
Android And iOS fixed.
Browse files Browse the repository at this point in the history
Example scene to test vibrations
  • Loading branch information
BenoitFreslon committed Apr 23, 2019
1 parent 36f38b6 commit e8a777e
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 17 deletions.
8 changes: 8 additions & 0 deletions Example.meta

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

71 changes: 71 additions & 0 deletions Example/VibrationExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
////////////////////////////////////////////////////////////////////////////////
//
// @author Benoît Freslon @benoitfreslon
// https://github.com/BenoitFreslon/Vibration
// https://benoitfreslon.com
//
////////////////////////////////////////////////////////////////////////////////

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class VibrationExample : MonoBehaviour
{
public Text inputTime;
public Text inputPattern;
public Text inputRepeat;

// Use this for initialization
void Start ()
{

}

// Update is called once per frame
void Update ()
{

}

public void TapVibrate ()
{
Vibration.Vibrate ();
}

public void TapVibrateCustom ()
{
Debug.Log ( inputTime.text );
Vibration.Vibrate ( int.Parse ( inputTime.text ) );
}

public void TapVibratePattern ()
{
long[] longs = inputPattern.text.Select ( item => ( long )item ).ToArray ();
Debug.Log ( longs + " " + int.Parse ( inputRepeat.text ) );
Vibration.Vibrate ( longs, int.Parse ( inputRepeat.text ) );
}

public void TapCancelVibrate ()
{

Vibration.Cancel ();
}

public void TapPopVibrate ()
{
Vibration.VibratePop ();
}

public void TapPeekVibrate ()
{
Vibration.VibratePeek ();
}

public void TapNopeVibrate ()
{
Vibration.VibrateNope ();
}
}
11 changes: 11 additions & 0 deletions Example/VibrationExample.cs.meta

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

Binary file added Example/VibrationExample.unity
Binary file not shown.
7 changes: 7 additions & 0 deletions Example/VibrationExample.unity.meta

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

7 changes: 7 additions & 0 deletions LICENSE.meta

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

8 changes: 8 additions & 0 deletions Plugins.meta

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

8 changes: 8 additions & 0 deletions Plugins/iOS.meta

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

8 changes: 8 additions & 0 deletions Plugins/iOS/Vibration.meta

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

24 changes: 24 additions & 0 deletions Plugins/iOS/Vibration/Vibration.h.meta

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

8 changes: 4 additions & 4 deletions Plugins/iOS/Vibration/Vibration.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ + (BOOL) hasVibrator {
return !(USING_IPAD);
}
+ (void) vibrate {
AudioServicesPlaySystemSoundWithCompletion(1352, NULL));
AudioServicesPlaySystemSoundWithCompletion(1352, NULL);
}
+ (void) vibratePeek {
AudioServicesPlaySystemSoundWithCompletion(1519, NULL)); // Actuate `Peek` feedback (weak boom)
AudioServicesPlaySystemSoundWithCompletion(1519, NULL); // Actuate `Peek` feedback (weak boom)
}
+ (void) vibratePop {
AudioServicesPlaySystemSoundWithCompletion(1520, NULL)); // Actuate `Pop` feedback (strong boom)
AudioServicesPlaySystemSoundWithCompletion(1520, NULL); // Actuate `Pop` feedback (strong boom)
}
+ (void) vibrateNope {
AudioServicesPlaySystemSoundWithCompletion(1521, NULL)); // Actuate `Nope` feedback (series of three weak booms)
AudioServicesPlaySystemSoundWithCompletion(1521, NULL); // Actuate `Nope` feedback (series of three weak booms)
}

@end
Expand Down
34 changes: 34 additions & 0 deletions Plugins/iOS/Vibration/Vibration.mm.meta

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

7 changes: 7 additions & 0 deletions README.md.meta

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

93 changes: 80 additions & 13 deletions Vibration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
using UnityEngine;
////////////////////////////////////////////////////////////////////////////////
//
// @author Benoît Freslon @benoitfreslon
// https://github.com/BenoitFreslon/Vibration
// https://benoitfreslon.com
//
////////////////////////////////////////////////////////////////////////////////

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Vibration
{
Expand All @@ -23,58 +32,116 @@ public class Vibration
#endif

#if UNITY_ANDROID
public static AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
public static AndroidJavaClass unityPlayer = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" );
public static AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject> ( "currentActivity" );
public static AndroidJavaObject vibrator = currentActivity.Call<AndroidJavaObject> ( "getSystemService", "vibrator" );
public static AndroidJavaObject context = currentActivity.Call<AndroidJavaObject> ( "getApplicationContext" );

#endif

public static void Vibrate ()
///<summary>
///Only on iOS
///</summary>
public static void VibratePop ()
{
#if UNITY_ANDROID
vibrator.Call ( "vibrate" );
#elif UNITY_IOS
_Vibrate();
if ( !IsOnMobile () ) return;

#if UNITY_IOS
_VibratePop ();
#endif
}

///<summary>
///Only on iOS
///</summary>
public static void VibratePeek ()
{
if ( !IsOnMobile () ) return;

#if UNITY_IOS
_VibratePeek ();
#endif
}

///<summary>
///Only on iOS
///</summary>
public static void VibrateNope ()
{
if ( !IsOnMobile () ) return;

#if UNITY_IOS
_VibrateNope ();
#endif
}

public static void Vibrate ()
{
Handheld.Vibrate ();
}

///<summary>
/// Only on Android
/// https://developer.android.com/reference/android/os/Vibrator.html#vibrate(long)
///</summary>
public static void Vibrate ( long milliseconds )
{
if ( !IsOnMobile () ) return;

#if UNITY_ANDROID
vibrator.Call ( "vibrate", milliseconds );
#elif UNITY_IOS
_Vibrate();
_Vibrate ();
#endif
}

///<summary>
/// Only on Android
/// https://proandroiddev.com/using-vibrate-in-android-b0e3ef5d5e07
///</summary>
public static void Vibrate ( long[] pattern, int repeat )
{
if ( !IsOnMobile () ) return;

#if UNITY_ANDROID
vibrator.Call ( "vibrate", pattern, repeat );
#elif UNITY_IOS
_Vibrate();
_Vibrate ();
#endif
}

public static bool HasVibrator ()
{
if ( !IsOnMobile () ) return false;

#if UNITY_ANDROID
AndroidJavaClass contextClass = new AndroidJavaClass ( "android.content.Context" );
string Context_VIBRATOR_SERVICE = contextClass.GetStatic<string> ( "VIBRATOR_SERVICE" );
AndroidJavaObject systemService = context.Call<AndroidJavaObject> ( "getSystemService", Context_VIBRATOR_SERVICE );
if ( systemService.Call<bool> ( "hasVibrator" ) ) {
return true;
} else
return false;
#elif UNITY_IOS
return _HasVibratior();
return _HasVibrator ();
#endif
}

///<summary>
///Only on Android
///</summary>
public static void Cancel ()
{
if ( !IsOnMobile () ) return;
#if UNITY_ANDROID
vibrator.Call ( "cancel" );
vibrator.Call ( "cancel" );
#endif
}
private static bool IsOnMobile ()
{
if ( Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer )
return true;

return false;
}
}

0 comments on commit e8a777e

Please sign in to comment.