migrate changes from audio system

This commit is contained in:
2026-05-14 20:50:43 +08:00
parent 5457cb31a1
commit 32be8f5887
15 changed files with 236 additions and 30 deletions
@@ -10,22 +10,19 @@ namespace OCES.Haptic
{
public static HapticSystem Instance {get; private set;}
[NonSerialized]
public bool IsHapticSupported = DeviceCapabilities.isVersionSupported;
public bool IsHapticSupported;
[NonSerialized]
public bool IsMeetsAdvanceRequirements = DeviceCapabilities.meetsAdvancedRequirements;
public bool IsMeetsAdvanceRequirements;
[SerializeField]
HapticSettings hapticSettings;
internal ResourceLoader ResourceLoader;
HapticObjectConfig m_hapticObjects;
const string k_hapticConfigPath = "HapticData/";
const string k_hapticResourcesPath = "Haptics/";
public void Play(uint hapticId, bool isDirectCall = true)
{
if (this.m_hapticObjects is null)
{
Debug.LogError($"[Haptic System] Config not loaded!");
return;
}
HapticObject hapticObject = this.m_hapticObjects.QueryById(hapticId);
if (hapticObject != null)
{
@@ -58,7 +55,7 @@ namespace OCES.Haptic
}
break;
case HapticType.Emphasis:
if (hapticObject.Amplitude <= 0f || hapticObject.Frequency <= 0f)
if (hapticObject.Amplitude < 0f || hapticObject.Frequency < 0f)
{
Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude or frequency." +
"Please check the datatable.");
@@ -67,7 +64,7 @@ namespace OCES.Haptic
HapticPatterns.PlayEmphasis(hapticObject.Amplitude, hapticObject.Frequency);
break;
case HapticType.Constant:
if (hapticObject.Amplitude <= 0f || hapticObject.Frequency <= 0f || hapticObject.Duration <= 0f)
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.");
@@ -79,10 +76,10 @@ namespace OCES.Haptic
case HapticType.Advance:
if (Enum.TryParse(hapticObject.FallbackPreset, out HapticPatterns.PresetType fallbackPreset))
{
HapticController.fallbackPreset = fallbackPreset;
HapticClip hapticClip = GetHapticClip(hapticObject);
if (hapticClip)
{
HapticController.fallbackPreset = fallbackPreset;
HapticController.Stop();
HapticController.Play(hapticClip);
}
@@ -109,6 +106,8 @@ namespace OCES.Haptic
void Awake()
{
HapticSettings.Instance = this.hapticSettings;
if (Instance && Instance != this)
{
Destroy(gameObject);
@@ -116,32 +115,29 @@ namespace OCES.Haptic
}
Instance = this;
DontDestroyOnLoad(gameObject);
this.ResourceLoader = gameObject.AddComponent<ResourceLoader>();
this.m_hapticObjects = HapticConfigLoader.Load<HapticObjectConfig>(k_hapticConfigPath + "HapticObject");
}
void OnDestroy()
{
if (Instance == this) Instance = null;
this.m_hapticObjects = HapticConfigLoader.Load<HapticObjectConfig>(HapticSettings.Instance.hapticConfigPath + "HapticObject");
this.IsHapticSupported = DeviceCapabilities.isVersionSupported;
this.IsMeetsAdvanceRequirements = DeviceCapabilities.meetsAdvancedRequirements;
}
static HapticClip GetHapticClip(HapticObject hapticObject)
{
return Resources.Load<HapticClip>(k_hapticResourcesPath + hapticObject.Payload);
//TODO: cache HapticClips into a Dictionary<string, HapticClip> or using AsyncLoad
return Instance.ResourceLoader.LoadSync<HapticClip>(HapticSettings.Instance.hapticResourcePath + hapticObject.Payload);
}
static class HapticConfigLoader
{
internal static T Load<T>(string tableName) where T : IBinarySerializable, new()
{
TextAsset bytes = Resources.Load<TextAsset>(tableName);
TextAsset bytes = Instance.ResourceLoader.LoadSync<TextAsset>(tableName);
if (!bytes)
{
Debug.LogError($"未找到表 {tableName}");
return default;
}
IBinarySerializable data = new T();
bool readOk = FileManager.ReadBinaryDataFromBytes(bytes.bytes, ref data);
if (readOk)