备份CatanBuilding瘦身独立工程
This commit is contained in:
251
Assets/Scripts/Core/VibrationController.cs
Normal file
251
Assets/Scripts/Core/VibrationController.cs
Normal file
@@ -0,0 +1,251 @@
|
||||
using UnityEngine;
|
||||
using asap.core;
|
||||
using game;
|
||||
using UniRx;
|
||||
using System;
|
||||
using MoreMountains.NiceVibrations;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class VibrationData
|
||||
{
|
||||
public VibrationData(HapticTypes hapticTypes)
|
||||
{
|
||||
this.hapticTypes = hapticTypes;
|
||||
}
|
||||
public HapticTypes hapticTypes { get; set; }
|
||||
public VibrationData(int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
public int id { get; set; }
|
||||
}
|
||||
|
||||
public class VibrationController
|
||||
{
|
||||
|
||||
IDisposable disposable;
|
||||
public void Init()
|
||||
{
|
||||
disposable = GContext.OnEvent<VibrationData>().Subscribe(OnVibrationData);
|
||||
|
||||
Debug.Log("[VibrationController] Init iOSInitializeHaptics");
|
||||
MMVibrationManager.iOSInitializeHaptics();
|
||||
}
|
||||
void OnVibrationData(VibrationData data)
|
||||
{
|
||||
bool isVibrate = GContext.container.Resolve<ISettingService>().Vibration;
|
||||
if (isVibrate)
|
||||
{
|
||||
if (data.id > 0)
|
||||
{
|
||||
PlayVibrationList(data.id);
|
||||
return;
|
||||
}
|
||||
switch (data.hapticTypes)
|
||||
{
|
||||
case HapticTypes.Selection:
|
||||
case HapticTypes.Success:
|
||||
case HapticTypes.Warning:
|
||||
case HapticTypes.Failure:
|
||||
case HapticTypes.LightImpact:
|
||||
case HapticTypes.MediumImpact:
|
||||
case HapticTypes.HeavyImpact:
|
||||
case HapticTypes.SoftImpact:
|
||||
case HapticTypes.RigidImpact:
|
||||
MMVibrationManager.Haptic(data.hapticTypes);
|
||||
break;
|
||||
case HapticTypes.Vibrate:
|
||||
MMVibrationManager.Vibrate();
|
||||
break;
|
||||
default:
|
||||
#if UNITY_IOS || UNITY_ANDROID
|
||||
Handheld.Vibrate();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~VibrationController()
|
||||
{
|
||||
disposable.Dispose();
|
||||
disposable = null;
|
||||
MMVibrationManager.iOSReleaseHaptics();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the default Unity vibration, without any control over duration, pattern or amplitude
|
||||
/// </summary>
|
||||
public void TriggerDefault()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
#if UNITY_IOS || UNITY_ANDROID
|
||||
Handheld.Vibrate();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the default Vibrate method, which will result in a medium vibration on Android and a medium impact on iOS
|
||||
/// </summary>
|
||||
public void TriggerVibrate()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Vibrate();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the selection haptic feedback, a light vibration on Android, and a light impact on iOS
|
||||
/// </summary>
|
||||
public void TriggerSelection()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.Selection);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the success haptic feedback, a light then heavy vibration on Android, and a success impact on iOS
|
||||
/// </summary>
|
||||
public void TriggerSuccess()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.Success);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the warning haptic feedback, a heavy then medium vibration on Android, and a warning impact on iOS
|
||||
/// </summary>
|
||||
public void TriggerWarning()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.Warning);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the failure haptic feedback, a medium / heavy / heavy / light vibration pattern on Android, and a failure impact on iOS
|
||||
/// </summary>
|
||||
public void TriggerFailure()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.Failure);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers a light impact on iOS and a short and light vibration on Android.
|
||||
/// </summary>
|
||||
public void TriggerLightImpact()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.LightImpact);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers a medium impact on iOS and a medium and regular vibration on Android.
|
||||
/// </summary>
|
||||
public void TriggerMediumImpact()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.MediumImpact);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers a heavy impact on iOS and a long and heavy vibration on Android.
|
||||
/// </summary>
|
||||
public void TriggerHeavyImpact()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.HeavyImpact);
|
||||
}
|
||||
}
|
||||
|
||||
public void TriggerSoftImpact()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.SoftImpact);
|
||||
}
|
||||
}
|
||||
|
||||
public void TriggerRigidImpact()
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
MMVibrationManager.Haptic(HapticTypes.RigidImpact);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsVibrate
|
||||
{
|
||||
get
|
||||
{
|
||||
ISettingService settingService = GContext.container.Resolve<ISettingService>();
|
||||
return settingService.Vibration;
|
||||
}
|
||||
}
|
||||
async void PlayVibrationList(int id)
|
||||
{
|
||||
if (IsVibrate)
|
||||
{
|
||||
cfg.Tables tables = GContext.container.Resolve<cfg.Tables>();
|
||||
if (tables != null)
|
||||
{
|
||||
cfg.VibrationDef vibrationDef = tables.TbVibrationDef.GetOrDefault(id);
|
||||
if (vibrationDef != null)
|
||||
{
|
||||
List<string> VibrationList = vibrationDef.VibrationList;
|
||||
List<float> DelayList = vibrationDef.DelayList;
|
||||
for (int i = 0; i < VibrationList.Count; i++)
|
||||
{
|
||||
await Awaiters.Seconds(DelayList[i]);
|
||||
OnVibrationData(VibrationList[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void OnVibrationData(string hapticTypes)
|
||||
{
|
||||
bool success = Enum.TryParse(hapticTypes, out HapticTypes value);
|
||||
if (success)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case HapticTypes.Selection:
|
||||
case HapticTypes.Success:
|
||||
case HapticTypes.Warning:
|
||||
case HapticTypes.Failure:
|
||||
case HapticTypes.LightImpact:
|
||||
case HapticTypes.MediumImpact:
|
||||
case HapticTypes.HeavyImpact:
|
||||
case HapticTypes.SoftImpact:
|
||||
case HapticTypes.RigidImpact:
|
||||
MMVibrationManager.Haptic(value);
|
||||
break;
|
||||
case HapticTypes.Vibrate:
|
||||
MMVibrationManager.Vibrate();
|
||||
break;
|
||||
default:
|
||||
#if UNITY_IOS || UNITY_ANDROID
|
||||
Handheld.Vibrate();
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user