Advertisement
BlackWindX

Unity Vibrator

Jan 14th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using Zenject;
  4.  
  5. namespace _Project.CodeBase.Services.Vibration
  6. {
  7.     public class Vibrator : IInitializable, IDisposable
  8.     {
  9.         public bool AndroidSupported { get; private set; }
  10.         public bool Enabled
  11.         {
  12.             get => _enabled;
  13.             set
  14.             {
  15.                 if (!AndroidSupported)
  16.                     return;
  17.  
  18.                 _enabled = value;
  19.             }
  20.         }
  21.  
  22.         private const string VibrationSignal = "vibrate";
  23.         private const string CancelSignal = "cancel";
  24.  
  25.         private AndroidJavaClass _unityPlayer;
  26.         private AndroidJavaObject _currentActivity;
  27.         private AndroidJavaObject _systemVibrator;
  28.         private bool _enabled;
  29.  
  30.         public void Initialize()
  31.         {
  32.         #if UNITY_ANDROID && !UNITY_EDITOR
  33.             _unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  34.             _currentActivity = _unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  35.             _systemVibrator = _currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
  36.             AndroidSupported = _systemVibrator.Call<bool>("hasVibrator");
  37.         #endif
  38.         }
  39.  
  40.         public void Dispose() =>
  41.             Cancel();
  42.  
  43.         public void Vibrate()
  44.         {
  45.             Debug.Log($"Enabled: {Enabled} | Supported: {AndroidSupported}");
  46.            
  47.             if (!Enabled)
  48.                 return;
  49.            
  50.             if (AndroidSupported)
  51.             {
  52.                 _systemVibrator.Call(VibrationSignal);
  53.                 Debug.Log("Vibrate!");
  54.                 return;
  55.             }
  56.            
  57.             Handheld.Vibrate();
  58.         }
  59.  
  60.         public void Vibrate(long milliseconds)
  61.         {
  62.             if (!Enabled)
  63.                 return;
  64.            
  65.             if (AndroidSupported)
  66.                 _systemVibrator.Call(VibrationSignal, milliseconds);
  67.             else
  68.                 Handheld.Vibrate();
  69.         }
  70.  
  71.         public void Vibrate(long[] pattern, int repeat)
  72.         {
  73.             if (!Enabled)
  74.                 return;
  75.  
  76.             if (AndroidSupported)
  77.                 _systemVibrator.Call(VibrationSignal, pattern, repeat);
  78.             else
  79.                 Handheld.Vibrate();
  80.         }
  81.  
  82.         public void Cancel()
  83.         {
  84.             if (AndroidSupported)
  85.                 _systemVibrator.Call(CancelSignal);
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement