/// <summary> /// Plays a haptic pattern, the most complex type of haptic, defined by a JSON string on iOS, and a pattern on Android /// </summary> /// <param name="iOSJSONString"></param> /// <param name="androidPattern"></param> /// <param name="androidAmplitudes"></param> /// <param name="androidRepeat"></param> /// <param name="fallbackOldiOS"></param> public static void AdvancedHapticPattern(string iOSJSONString, long[] androidPattern, int[] androidAmplitudes, int androidRepeat, HapticTypes fallbackOldiOS = HapticTypes.None) { if (!_vibrationsActive) { return; } DebugLog("[MMVibrationManager] Advanced Haptic Pattern"); if (Android()) { MMNVAndroid.AndroidVibrate(androidPattern, androidAmplitudes, androidRepeat); } else if (iOS()) { if (iOSVersion >= 13) { MMNViOSCoreHaptics.PlayCoreHapticsFromJSON(iOSJSONString); } else { MMNViOS.iOSTriggerHaptics(fallbackOldiOS); } } }
protected virtual void Awake() { _rigidBody = this.gameObject.GetComponent <Rigidbody2D>(); _ballAnimator = this.gameObject.GetComponent <Animator>(); _hitAnimationParameter = Animator.StringToHash("Hit"); MMNViOSCoreHaptics.CreateEngine(); }
/// <summary> /// Plays a continuous haptic of the specified intensity, sharpness and duration /// </summary> /// <param name="intensity"></param> /// <param name="sharpness"></param> /// <param name="duration"></param> public static void ContinuousHaptic(float intensity, float sharpness, float duration, HapticTypes fallbackOldiOS = HapticTypes.None, MonoBehaviour mono = null) { if (!_vibrationsActive) { return; } DebugLog("[MMVibrationManager] Continuous Haptic"); if (Android()) { intensity = NiceVibrationsDemoHelpers.Remap(intensity, 0f, 1f, 0, 255); MMNVAndroid.AndroidVibrate((long)(duration * 1000), (int)(intensity)); } else if (iOS()) { if (iOSVersion >= 13) { MMNViOSCoreHaptics.PlayContinuousHapticPattern(intensity, sharpness, duration, mono); } else { MMNViOS.iOSTriggerHaptics(fallbackOldiOS); } } }
protected virtual void UpdateContinuous() { if (_continuousActive) { MMNViOSCoreHaptics.UpdateContinuousHapticPatternRational(ContinuousIntensity, ContinuousSharpness); DebugAudioContinuous.volume = ContinuousIntensity; DebugAudioContinuous.pitch = 0.5f + ContinuousSharpness / 2f; } }
/// <summary> /// Stops all running pattern or continuous haptics /// </summary> public static void StopContinuousHaptic(bool alsoRumble = false) { DebugLog("[MMVibrationManager] Stop Continuous Haptic"); MMNViOSCoreHaptics.StopHapticPatterns(); MMNVAndroid.AndroidCancelVibrations(); #if MOREMOUNTAINS_NICEVIBRATIONS_RUMBLE if (alsoRumble) { MMNVRumble.StopRumble(); } #endif }
/// <summary> /// Plays a transient haptic, signature with more fine control /// </summary> /// <param name="vibrateiOS"></param> /// <param name="iOSIntensity"></param> /// <param name="iOSSharpness"></param> /// <param name="vibrateAndroid"></param> /// <param name="androidIntensity"></param> /// <param name="androidSharpness"></param> /// <param name="vibrateAndroidIfNoSupport"></param> /// <param name="rumble"></param> /// <param name="rumbleLowFrequency"></param> /// <param name="rumbleHighFrequency"></param> /// <param name="controllerID"></param> /// <param name="coroutineSupport"></param> public static void TransientHaptic(bool vibrateiOS, float iOSIntensity, float iOSSharpness, bool vibrateAndroid, float androidIntensity = 1f, float androidSharpness = 1f, bool vibrateAndroidIfNoSupport = false, bool rumble = true, float rumbleLowFrequency = 1f, float rumbleHighFrequency = 1f, int controllerID = -1, MonoBehaviour coroutineSupport = null, bool threaded = true) { if (!_vibrationsActive) { return; } DebugLog("[MMVibrationManager] Transient Haptic"); if (Android() && vibrateAndroid) { if (!MMNVAndroid.AndroidHasAmplitudeControl() && !vibrateAndroidIfNoSupport) { return; } androidIntensity = Remap(androidIntensity, 0f, 1f, 0, 255); MMNVAndroid.AndroidVibrate(100, (int)(androidIntensity)); } else if (iOS() && vibrateiOS) { if ((iOSVersion >= 13) && HapticsSupported()) { MMNViOSCoreHaptics.PlayTransientHapticPattern(iOSIntensity, iOSSharpness, threaded); _hapticsPlayedOnce = true; } else { if (iOSIntensity < 0.3f) { MMNViOS.iOSTriggerHaptics(HapticTypes.LightImpact); } else if ((iOSIntensity >= 0.3f) && (iOSIntensity < 0.6f)) { MMNViOS.iOSTriggerHaptics(HapticTypes.MediumImpact); } else { MMNViOS.iOSTriggerHaptics(HapticTypes.HeavyImpact); } } } if (rumble && (coroutineSupport != null)) { #if MOREMOUNTAINS_NICEVIBRATIONS_RUMBLE MMNVRumble.Rumble(rumbleLowFrequency, rumbleHighFrequency, 0.08f, coroutineSupport, controllerID); #endif } }
/// <summary> /// Stops all currently running haptics /// </summary> /// <param name="alsoRumble"></param> public static void StopAllHaptics(bool alsoRumble = false) { if (!_hapticsPlayedOnce) { return; } DebugLog("[MMVibrationManager] Stop all haptics"); MMNViOSCoreHaptics.StopEngine(); MMNVAndroid.AndroidCancelVibrations(); #if MOREMOUNTAINS_NICEVIBRATIONS_RUMBLE if (alsoRumble) { MMNVRumble.StopRumble(); } #endif }
/// <summary> /// Returns true if haptics are supported on this device /// </summary> /// <returns></returns> public static bool HapticsSupported() { if (iOS()) { if (iOSVersion >= 13) { return(MMNViOSCoreHaptics.CoreHapticsSupported()); } else { return(MMNViOS.iOSHapticsSupported()); } } if (Android()) { return(MMNVAndroid.AndroidHasVibrator()); } return(false); }
/// <summary> /// Updates a continuous haptic while it's playing. Not supported on Android for now, until Google adds API support for that /// More options signature /// </summary> /// <param name="ios"></param> /// <param name="iosIntensity"></param> /// <param name="iosSharpness"></param> /// <param name="android"></param> /// <param name="androidIntensity"></param> /// <param name="androidSharpness"></param> /// <param name="rumble"></param> /// <param name="rumbleIntensity"></param> /// <param name="rumbleSharpness"></param> /// <param name="controllerID"></param> public static void UpdateContinuousHaptic(bool ios, float iosIntensity, float iosSharpness, bool android, float androidIntensity, float androidSharpness, bool rumble, float rumbleIntensity, float rumbleSharpness, int controllerID = -1) { if (iOS() && ios) { if ((iOSVersion >= 13) && HapticsSupported()) { MMNViOSCoreHaptics.UpdateContinuousHapticPatternRational(iosIntensity, iosSharpness); _hapticsPlayedOnce = true; } } if (rumble) { #if MOREMOUNTAINS_NICEVIBRATIONS_RUMBLE MMNVRumble.RumbleContinuous(rumbleIntensity, rumbleSharpness, controllerID); #endif } }
/// <summary> /// Plays a advanced haptic pattern, /// </summary> /// <param name="ios"></param> /// <param name="iOSJSONString"></param> /// <param name="android"></param> /// <param name="androidPattern"></param> /// <param name="androidAmplitudes"></param> /// <param name="androidRepeat"></param> /// <param name="vibrateAndroidIfNoSupport"></param> /// <param name="rumble"></param> /// <param name="rumblePattern"></param> /// <param name="rumbleLowFreqAmplitudes"></param> /// <param name="rumbleHighFreqAmplitudes"></param> /// <param name="rumbleRepeat"></param> /// <param name="fallbackOldiOS"></param> /// <param name="coroutineSupport"></param> /// <param name="controllerID"></param> public static void AdvancedHapticPattern(bool ios, string iOSJSONString, bool android, long[] androidPattern, int[] androidAmplitudes, int androidRepeat, bool vibrateAndroidIfNoSupport, bool rumble, long[] rumblePattern, int[] rumbleLowFreqAmplitudes, int[] rumbleHighFreqAmplitudes, int rumbleRepeat, HapticTypes fallbackOldiOS = HapticTypes.None, MonoBehaviour coroutineSupport = null, int controllerID = -1, bool threaded = false) { if (!_vibrationsActive) { return; } DebugLog("[MMVibrationManager] Advanced Haptic Pattern"); if (Android()) { if (!MMNVAndroid.AndroidHasAmplitudeControl() && !vibrateAndroidIfNoSupport) { return; } MMNVAndroid.AndroidVibrate(androidPattern, androidAmplitudes, androidRepeat, threaded); } else if (iOS()) { if ((iOSVersion >= 13) && HapticsSupported()) { MMNViOSCoreHaptics.PlayCoreHapticsFromJSON(iOSJSONString, threaded); _hapticsPlayedOnce = true; } else { MMNViOS.iOSTriggerHaptics(fallbackOldiOS); } } #if MOREMOUNTAINS_NICEVIBRATIONS_RUMBLE if (coroutineSupport != null) { MMNVRumble.Rumble(rumblePattern, rumbleLowFreqAmplitudes, rumbleHighFreqAmplitudes, rumbleRepeat, coroutineSupport, controllerID); } #endif }
/// <summary> /// Plays a continuous haptic, full options signature /// </summary> /// <param name="vibrateiOS"></param> /// <param name="iOSIntensity"></param> /// <param name="iOSSharpness"></param> /// <param name="fallbackOldiOS"></param> /// <param name="vibrateAndroid"></param> /// <param name="androidIntensity"></param> /// <param name="androidSharpness"></param> /// <param name="vibrateAndroidIfNoSupport"></param> /// <param name="rumble"></param> /// <param name="rumbleLowFrequency"></param> /// <param name="rumbleHighFrequency"></param> /// <param name="controllerID"></param> /// <param name="duration">the duration in seconds</param> /// <param name="mono">a monobehaviour to use to sustain this haptic</param> /// <param name="threaded">whether to call this on the main thread (false) or a secondary one (true)</param> /// <param name="fullIntensity">whether to allow for full intensity control for subsequent updates</param> public static void ContinuousHaptic(bool vibrateiOS, float iOSIntensity, float iOSSharpness, HapticTypes fallbackOldiOS, bool vibrateAndroid, float androidIntensity, float androidSharpness, bool vibrateAndroidIfNoSupport, bool rumble, float rumbleLowFrequency, float rumbleHighFrequency, int controllerID, float duration, MonoBehaviour mono = null, bool threaded = false, bool fullIntensity = true) { if (!_vibrationsActive) { return; } DebugLog("[MMVibrationManager] Continuous Haptic"); if (Android() && vibrateAndroid) { if (!MMNVAndroid.AndroidHasAmplitudeControl() && !vibrateAndroidIfNoSupport) { return; } androidIntensity = Remap(androidIntensity, 0f, 1f, 0, 255); MMNVAndroid.AndroidVibrate((long)(duration * 1000), (int)(androidIntensity)); } else if (iOS() && vibrateiOS) { if ((iOSVersion >= 13) && HapticsSupported()) { MMNViOSCoreHaptics.PlayContinuousHapticPattern(iOSIntensity, iOSSharpness, duration, mono, threaded, fullIntensity); _hapticsPlayedOnce = true; } else { MMNViOS.iOSTriggerHaptics(fallbackOldiOS); } } if (rumble && (mono != null)) { #if MOREMOUNTAINS_NICEVIBRATIONS_RUMBLE MMNVRumble.RumbleContinuous(rumbleLowFrequency, rumbleHighFrequency, controllerID); #endif } }
/// <summary> /// Plays a transient haptic, a single, short haptic feedback, of the specified intensity and sharpness /// </summary> /// <param name="intensity"></param> /// <param name="sharpness"></param> public static void TransientHaptic(float intensity, float sharpness) { if (!_vibrationsActive) { return; } DebugLog("[MMVibrationManager] Transient Haptic"); if (Android()) { intensity = NiceVibrationsDemoHelpers.Remap(intensity, 0f, 1f, 0, 255); MMNVAndroid.AndroidVibrate(100, (int)(intensity)); } else if (iOS()) { if (iOSVersion >= 13) { MMNViOSCoreHaptics.PlayTransientHapticPattern(intensity, sharpness); } else { if (intensity < 0.3f) { MMNViOS.iOSTriggerHaptics(HapticTypes.LightImpact); } else if ((intensity >= 0.3f) && (intensity < 0.6f)) { MMNViOS.iOSTriggerHaptics(HapticTypes.MediumImpact); } else { MMNViOS.iOSTriggerHaptics(HapticTypes.HeavyImpact); } } } }
/// <summary> /// Triggers a simple vibration /// </summary> public static void Vibrate() { DebugLog("[MMVibrationManager] Vibrate"); if (!_vibrationsActive) { return; } if (Android()) { MMNVAndroid.AndroidVibrate(MediumDuration); } else if (iOS()) { if (iOSVersion >= 13) { MMNViOSCoreHaptics.PlayTransientHapticPattern(0.8f, 0.8f); } else { MMNViOS.iOSTriggerHaptics(HapticTypes.MediumImpact); } } }
/// <summary> /// Enables or disables console logs (off by default) /// </summary> /// <param name="log"></param> public static void SetDebugMode(bool log) { _debugLogActive = log; MMNViOSCoreHaptics.SetDebugMode(true); }
protected virtual void HandlePower() { _knobValue = Knob.Active ? Knob.Value : 0f; if (!_carStarted) { if ((_knobValue > MinimumKnobValue) && (Knob.Active)) { _carStarted = true; _carStartedAt = Time.time; //MMVibrationManager.Haptic(HapticTypes.Success); _lastStartClickAt = Time.time; MMVibrationManager.ContinuousHaptic(_knobValue, _knobValue, MaximumPowerDuration, HapticTypes.MediumImpact, this); CarEngineAudioSource.Play(); } else { Power += Time.deltaTime * ChargingSpeed; Power = Mathf.Clamp(Power, 0f, MaximumPowerDuration); if (Power == MaximumPowerDuration) { Knob.SetActive(true); Knob._rectTransform.localScale = Vector3.one; ReloadingPrompt.SetActive(false); } else { if (!Knob.Active) { Knob.SetValue(CarSpeed); } } } } else { if (Time.time - _carStartedAt > MaximumPowerDuration) { _carStarted = false; Knob.SetActive(false); Knob._rectTransform.localScale = Vector3.one * 0.9f; ReloadingPrompt.SetActive(true); } else { if (_knobValue > MinimumKnobValue) { Power -= Time.deltaTime; Power = Mathf.Clamp(Power, 0f, MaximumPowerDuration); MMNViOSCoreHaptics.UpdateContinuousHapticPatternRational(_knobValue, _knobValue); if (Power <= 0f) { _carStarted = false; Knob.SetActive(false); Knob._rectTransform.localScale = Vector3.one * 0.9f; ReloadingPrompt.SetActive(true); } } else { _carStarted = false; _lastStartClickAt = Time.time; MMVibrationManager.StopContinuousHaptic(); } } } }
/// <summary> /// Stops all running pattern or continuous haptics /// </summary> public static void StopContinuousHaptic() { DebugLog("[MMVibrationManager] Stop Continuous Haptic"); MMNViOSCoreHaptics.StopHapticPatterns(); MMNVAndroid.AndroidCancelVibrations(); }