feat: 核心功能实现

This commit is contained in:
2026-04-09 16:26:45 +08:00
parent 302e4e35a9
commit 156285a691
12 changed files with 988 additions and 4 deletions
@@ -11,8 +11,36 @@ namespace OCES.Haptic
public enum HapticType
{
Preset = 0, //播放预制
Transient = 1, //播放瞬时
Continuous = 2, //播放连续
Emphasis = 1, //播放瞬时
Constant = 2, //播放连续
Advance = 3, //播放文件
}
/// <summary>
/// 文件操作类
/// </summary>
public static class FileManager
{
/// <summary>
/// 从内存流中读取二进制
/// </summary>
/// <param name="bytes"></param>
/// <param name="data"></param>
/// <returns></returns>
public static bool ReadBinaryDataFromBytes(byte[] bytes, ref IBinarySerializable data)
{
if (bytes == null)
return false;
using (MemoryStream memoryStream = new(bytes))
{
using (var br = new BinaryReader(memoryStream))
{
data.DeSerialize(br);
br.Close();
}
memoryStream.Close();
}
return true;
}
}
}
@@ -1,12 +1,138 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Lofelt.NiceVibrations;
using UnityEngine;
namespace OCES.Haptic
{
public class HapticSystem : MonoBehaviour
{
public static HapticSystem Instance {get; private set;}
[NonSerialized]
public bool IsHapticSupported = DeviceCapabilities.isVersionSupported;
[NonSerialized]
public bool IsMeetsAdvanceRequirements = DeviceCapabilities.meetsAdvancedRequirements;
HapticObjectConfig m_hapticObjects;
const string k_hapticConfigPath = "HapticData/";
const string k_hapticResourcesPath = "Haptics/";
public void Play(uint hapticId, bool isDirectCall = true)
{
HapticObject hapticObject = this.m_hapticObjects.QueryById(hapticId);
if (hapticObject != null)
{
Play(hapticObject);
if (isDirectCall)
{
Debug.LogWarning($"[Haptic System] Playing haptic id {hapticId} without play audio." +
"This method should only be called during debugging.");
}
}
else
{
Debug.LogError("[Haptic System] Could not find Haptic Object with id: " + hapticId);
}
}
public void Play(HapticObject hapticObject)
{
switch (hapticObject.Type)
{
case HapticType.Preset:
if (Enum.TryParse(hapticObject.Payload, out HapticPatterns.PresetType hapticPattern))
{
HapticPatterns.PlayPreset(hapticPattern);
}
else
{
Debug.LogError($"[Haptic System] Could not parse haptic pattern: {hapticObject.Payload}]");
}
break;
case HapticType.Emphasis:
if (hapticObject.Amplitude < 0f || hapticObject.Frequency < 0f)
{
Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude or frequency." +
"Please check the datatable.");
break;
}
HapticPatterns.PlayEmphasis(hapticObject.Amplitude, hapticObject.Frequency);
break;
case HapticType.Constant:
if (hapticObject.Amplitude < 0f || hapticObject.Frequency < 0f || hapticObject.Duration < 0f)
{
Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude, frequency or duration." +
"Please check the datatable.");
break;
}
HapticController.Stop();
HapticPatterns.PlayConstant(hapticObject.Amplitude, hapticObject.Frequency, hapticObject.Duration);
break;
case HapticType.Advance:
if (Enum.TryParse(hapticObject.FallbackPreset, out HapticPatterns.PresetType fallbackPreset))
{
HapticController.fallbackPreset = fallbackPreset;
HapticClip hapticClip = GetHapticClip(hapticObject);
if (hapticClip)
{
HapticController.Stop();
HapticController.Play(hapticClip);
}
else
{
Debug.LogError($"[Haptic System] Could not parse haptic file: {hapticObject.Payload}");
}
}
else
{
Debug.LogError($"[Haptic System] Could not parse fallback preset: {hapticObject.FallbackPreset}]");
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public void Stop()
{
HapticController.Stop();
}
void Awake()
{
if (Instance && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
this.m_hapticObjects = HapticConfigLoader.Load<HapticObjectConfig>(k_hapticConfigPath + "HapticObject");
}
static HapticClip GetHapticClip(HapticObject hapticObject)
{
return Resources.Load<HapticClip>(k_hapticResourcesPath + hapticObject.Payload);
}
static class HapticConfigLoader
{
internal static T Load<T>(string tableName) where T : IBinarySerializable, new()
{
TextAsset bytes = Resources.Load<TextAsset>(tableName);
if (!bytes)
Debug.LogError($"未找到表 {tableName}");
IBinarySerializable data = new T();
bool readOk = FileManager.ReadBinaryDataFromBytes(bytes.bytes, ref data);
if (readOk)
return (T)data;
Debug.LogError($"{tableName} 解析出错,类型 {typeof(T)}");
return default;
}
}
}
}
@@ -0,0 +1,40 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace OCES.Haptic
{
public class HapticInvoker : MonoBehaviour
{
public InputField inputField;
public Button button;
uint m_hapticId;
void OnEnable()
{
this.inputField.onSubmit.AddListener(GetCurrentHapticId);
this.inputField.onEndEdit.AddListener(GetCurrentHapticId);
this.button.onClick.AddListener(InvokeHaptic);
}
void OnDisable()
{
this.inputField.onSubmit.RemoveListener(GetCurrentHapticId);
this.inputField.onEndEdit.RemoveListener(GetCurrentHapticId);
this.button.onClick.RemoveListener(InvokeHaptic);
}
void GetCurrentHapticId(string value)
{
if(string.IsNullOrEmpty(value)) return;
this.m_hapticId = uint.Parse(value);
}
void InvokeHaptic()
{
HapticSystem.Instance.Play(this.m_hapticId);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 40c403aed9b64df8a9cecc470d6788a4
timeCreated: 1775717470