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
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e681c729026b4fdf83b38f1824f4ab02
timeCreated: 1778761511
@@ -0,0 +1,62 @@
using UnityEditor;
using UnityEngine;
namespace OCES.Haptic.Editor
{
static class HapticSettingsProvider
{
const string k_settingPath = "Assets/Settings/HapticSettings.asset";
[SettingsProvider]
static SettingsProvider Creat()
{
return new SettingsProvider("Project/Haptic Settings", SettingsScope.Project)
{
label = "Haptic Settings",
guiHandler = _ =>
{
HapticSettings settings = AssetDatabase.LoadAssetAtPath<HapticSettings>(k_settingPath);
if (!settings)
{
EditorGUILayout.HelpBox(
$"未找到 AudioExtendSettings.asset\n期望路径: {k_settingPath}",
MessageType.Warning);
if (GUILayout.Button("创建默认配置"))
CreateDefaultAsset();
return;
}
SerializedObject serializedObject = new(settings);
SerializedProperty serializedProperty = serializedObject.GetIterator();
serializedProperty.NextVisible(true);
EditorGUI.BeginChangeCheck();
while (serializedProperty.NextVisible(false))
{
EditorGUILayout.PropertyField(serializedProperty, true);
}
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(settings);
AssetDatabase.SaveAssets();
}
},
keywords = new[] { "Haptic", "Audio", "Vibration", "Vibrator", "Path" },
};
}
[MenuItem("Tools/Haptic/Create Haptic Settings Asset")]
static void CreateDefaultAsset()
{
if (!AssetDatabase.IsValidFolder("Assets/Settings"))
AssetDatabase.CreateFolder("Assets", "Settings");
HapticSettings asset = ScriptableObject.CreateInstance<HapticSettings>();
AssetDatabase.CreateAsset(asset, k_settingPath);
AssetDatabase.SaveAssets();
Debug.Log($"[HapticSettings] 已创建默认配置:{k_settingPath}");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fd277ab8102549be90d99031ceb8193a
timeCreated: 1778761525
@@ -31,12 +31,15 @@ namespace OCES.Haptic
{
if (bytes == null)
return false;
using MemoryStream memoryStream = new(bytes);
using (BinaryReader br = new(memoryStream))
using (MemoryStream memoryStream = new(bytes))
{
data.DeSerialize(br);
using (var br = new BinaryReader(memoryStream))
{
data.DeSerialize(br);
br.Close();
}
memoryStream.Close();
}
memoryStream.Close();
return true;
}
}
@@ -0,0 +1,20 @@
using UnityEngine;
namespace OCES.Haptic
{
/// <summary>
/// 触感反馈系统扩展配置,集中管理所有路径和可调参数。
/// 资产位置:Assets/Resources/AudioExtendSettings.asset
/// 编辑入口:Project Settings > Audio Extend
/// </summary>
public class HapticSettings : ScriptableObject
{
[Tooltip("Resources 子目录:触感配置文件")]
public string hapticConfigPath = "HapticData/";
[Tooltip("Resources 子目录:触感资源文件(.haptic")]
public string hapticResourcePath = "Haptics/";
public static HapticSettings Instance { get; internal set; }
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cf447f41468045bf9789408bd135ebeb
timeCreated: 1778750228
@@ -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)