improve: 整体健壮性

This commit is contained in:
2026-04-09 17:37:03 +08:00
parent 156285a691
commit 66c63896b0
4 changed files with 23 additions and 17 deletions
Binary file not shown.
@@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 21ad6923737cb4c9bbb7577a5f361772
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -31,15 +31,12 @@ namespace OCES.Haptic
{ {
if (bytes == null) if (bytes == null)
return false; return false;
using (MemoryStream memoryStream = new(bytes)) using MemoryStream memoryStream = new(bytes);
using (BinaryReader br = new(memoryStream))
{ {
using (var br = new BinaryReader(memoryStream)) data.DeSerialize(br);
{
data.DeSerialize(br);
br.Close();
}
memoryStream.Close();
} }
memoryStream.Close();
return true; return true;
} }
} }
@@ -20,6 +20,12 @@ namespace OCES.Haptic
public void Play(uint hapticId, bool isDirectCall = true) 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); HapticObject hapticObject = this.m_hapticObjects.QueryById(hapticId);
if (hapticObject != null) if (hapticObject != null)
{ {
@@ -52,7 +58,7 @@ namespace OCES.Haptic
} }
break; break;
case HapticType.Emphasis: 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." + Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude or frequency." +
"Please check the datatable."); "Please check the datatable.");
@@ -61,7 +67,7 @@ namespace OCES.Haptic
HapticPatterns.PlayEmphasis(hapticObject.Amplitude, hapticObject.Frequency); HapticPatterns.PlayEmphasis(hapticObject.Amplitude, hapticObject.Frequency);
break; break;
case HapticType.Constant: 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." + Debug.LogWarning($"[Haptic System] Haptic {hapticObject.Id} have no amplitude, frequency or duration." +
"Please check the datatable."); "Please check the datatable.");
@@ -73,10 +79,10 @@ namespace OCES.Haptic
case HapticType.Advance: case HapticType.Advance:
if (Enum.TryParse(hapticObject.FallbackPreset, out HapticPatterns.PresetType fallbackPreset)) if (Enum.TryParse(hapticObject.FallbackPreset, out HapticPatterns.PresetType fallbackPreset))
{ {
HapticController.fallbackPreset = fallbackPreset;
HapticClip hapticClip = GetHapticClip(hapticObject); HapticClip hapticClip = GetHapticClip(hapticObject);
if (hapticClip) if (hapticClip)
{ {
HapticController.fallbackPreset = fallbackPreset;
HapticController.Stop(); HapticController.Stop();
HapticController.Play(hapticClip); HapticController.Play(hapticClip);
} }
@@ -114,9 +120,15 @@ namespace OCES.Haptic
this.m_hapticObjects = HapticConfigLoader.Load<HapticObjectConfig>(k_hapticConfigPath + "HapticObject"); this.m_hapticObjects = HapticConfigLoader.Load<HapticObjectConfig>(k_hapticConfigPath + "HapticObject");
} }
void OnDestroy()
{
if (Instance == this) Instance = null;
}
static HapticClip GetHapticClip(HapticObject hapticObject) static HapticClip GetHapticClip(HapticObject hapticObject)
{ {
return Resources.Load<HapticClip>(k_hapticResourcesPath + hapticObject.Payload); return Resources.Load<HapticClip>(k_hapticResourcesPath + hapticObject.Payload);
//TODO: cache HapticClips into a Dictionary<string, HapticClip> or using AsyncLoad
} }
static class HapticConfigLoader static class HapticConfigLoader
@@ -125,7 +137,11 @@ namespace OCES.Haptic
{ {
TextAsset bytes = Resources.Load<TextAsset>(tableName); TextAsset bytes = Resources.Load<TextAsset>(tableName);
if (!bytes) if (!bytes)
{
Debug.LogError($"未找到表 {tableName}"); Debug.LogError($"未找到表 {tableName}");
return default;
}
IBinarySerializable data = new T(); IBinarySerializable data = new T();
bool readOk = FileManager.ReadBinaryDataFromBytes(bytes.bytes, ref data); bool readOk = FileManager.ReadBinaryDataFromBytes(bytes.bytes, ref data);
if (readOk) if (readOk)