2025-07-15 04:01:35 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using UnityEngine;
|
2025-08-11 18:27:18 +00:00
|
|
|
using UnityEngine.Serialization;
|
2025-07-15 04:01:35 +00:00
|
|
|
|
|
|
|
namespace DDD
|
|
|
|
{
|
|
|
|
public class DataSo<T> : ScriptableObject where T : IId
|
|
|
|
{
|
2025-08-11 18:27:18 +00:00
|
|
|
[FormerlySerializedAs("Datas")] [SerializeField] protected List<T> _datas = new();
|
2025-07-15 04:01:35 +00:00
|
|
|
|
2025-08-11 18:27:18 +00:00
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnValidate()
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Initialize() { }
|
|
|
|
|
|
|
|
public T GetDataById(string id) => _datas.FirstOrDefault(x => x.Id == id);
|
2025-07-25 07:58:53 +00:00
|
|
|
|
2025-08-11 18:27:18 +00:00
|
|
|
public bool ContainsData(string id) => _datas.Any(x => x.Id == id);
|
2025-08-06 02:45:08 +00:00
|
|
|
|
2025-07-25 07:58:53 +00:00
|
|
|
public bool TryGetDataById(string id, out T data)
|
|
|
|
{
|
2025-08-11 18:27:18 +00:00
|
|
|
data = _datas.FirstOrDefault(x => x.Id == id);
|
2025-07-25 07:58:53 +00:00
|
|
|
return data != null;
|
|
|
|
}
|
2025-07-15 04:01:35 +00:00
|
|
|
|
|
|
|
public void SetDataList(List<T> newList)
|
|
|
|
{
|
2025-08-11 18:27:18 +00:00
|
|
|
_datas = newList;
|
2025-07-15 04:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<T> GetDataList()
|
|
|
|
{
|
2025-08-11 18:27:18 +00:00
|
|
|
return _datas;
|
2025-07-15 04:01:35 +00:00
|
|
|
}
|
2025-07-21 10:51:11 +00:00
|
|
|
|
2025-08-11 18:27:18 +00:00
|
|
|
public int GetDataCount() => _datas.Count;
|
2025-07-15 04:01:35 +00:00
|
|
|
}
|
|
|
|
}
|