更新
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace SLSUtilities.Feedback
|
||||
{
|
||||
/// <summary>
|
||||
/// FeedbackData 的容器,供策划在武器 Prefab 上配置多个反馈序列。
|
||||
/// 支持按 feedbackName 索引查找。
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "FeedbackDataCollection", menuName = "SLS/Feedback/FeedbackDataCollection")]
|
||||
public class FeedbackDataCollection : SerializedScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// 反馈数据列表。
|
||||
/// </summary>
|
||||
[Searchable]
|
||||
[ListDrawerSettings(ShowFoldout = true, CustomRemoveIndexFunction = "OnRemoveItem")]
|
||||
[OnValueChanged("OnListChanged", true)]
|
||||
public List<FeedbackData> feedbackDataList = new List<FeedbackData>();
|
||||
|
||||
/// <summary>
|
||||
/// 按 feedbackName 索引查找 FeedbackData。
|
||||
/// </summary>
|
||||
public FeedbackData this[string name]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (feedbackDataList == null || string.IsNullOrEmpty(name)) return null;
|
||||
return feedbackDataList.FirstOrDefault(d => d != null && d.feedbackName == name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试按名称获取 FeedbackData。
|
||||
/// </summary>
|
||||
public bool TryGet(string name, out FeedbackData data)
|
||||
{
|
||||
data = this[name];
|
||||
return data != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当列表发生任何变化(添加、拖入、重新排序)时调用,维护父子引用。
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自定义删除逻辑:先解绑父级引用,再从列表中移除。
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 运行时按名称预览指定的 FeedbackData。
|
||||
/// </summary>
|
||||
[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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user