using System.Collections.Generic; using System.Linq; using Sirenix.OdinInspector; using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif namespace SLSUtilities.Feedback { /// /// FeedbackData 的容器,供策划在武器 Prefab 上配置多个反馈序列。 /// 支持按 feedbackName 索引查找。 /// [CreateAssetMenu(fileName = "FeedbackDataCollection", menuName = "SLS/Feedback/FeedbackDataCollection")] public class FeedbackDataCollection : SerializedScriptableObject { /// /// 反馈数据列表。 /// [Searchable] [ListDrawerSettings(ShowFoldout = true, CustomRemoveIndexFunction = "OnRemoveItem")] [OnValueChanged("OnListChanged", true)] public List feedbackDataList = new List(); /// /// 按 feedbackName 索引查找 FeedbackData。 /// public FeedbackData this[string name] { get { if (feedbackDataList == null || string.IsNullOrEmpty(name)) return null; return feedbackDataList.FirstOrDefault(d => d != null && d.feedbackName == name); } } /// /// 尝试按名称获取 FeedbackData。 /// public bool TryGet(string name, out FeedbackData data) { data = this[name]; return data != null; } /// /// 当列表发生任何变化(添加、拖入、重新排序)时调用,维护父子引用。 /// private void OnListChanged() { if (feedbackDataList == null) return; foreach (var data in feedbackDataList) { if (data != null && data.parentCollection != this) { data.parentCollection = this; #if UNITY_EDITOR EditorUtility.SetDirty(data); #endif } } } /// /// 自定义删除逻辑:先解绑父级引用,再从列表中移除。 /// private void OnRemoveItem(int index) { if (index < 0 || index >= feedbackDataList.Count) return; FeedbackData dataToRemove = feedbackDataList[index]; if (dataToRemove != null) { if (dataToRemove.parentCollection == this) { dataToRemove.parentCollection = null; #if UNITY_EDITOR EditorUtility.SetDirty(dataToRemove); #endif } } feedbackDataList.RemoveAt(index); #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif } /// /// 运行时按名称预览指定的 FeedbackData。 /// [Button("Preview by Name")] [EnableIf("@UnityEngine.Application.isPlaying")] public void PreviewByName(string name) { if (!Application.isPlaying) { Debug.LogWarning("[FeedbackDataCollection] Preview is only available in Play mode."); return; } if (TryGet(name, out FeedbackData data)) { data.Preview(); } else { Debug.LogWarning($"[FeedbackDataCollection] FeedbackData with name '{name}' not found."); } } } }