46 lines
1.1 KiB
C#
46 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace DDD
|
|
{
|
|
public class DataSo<T> : ScriptableObject where T : IId
|
|
{
|
|
[FormerlySerializedAs("Datas")] [SerializeField] protected List<T> _datas = new();
|
|
|
|
private void OnEnable()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
private void OnValidate()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
protected virtual void Initialize() { }
|
|
|
|
public T GetDataById(string id) => _datas.FirstOrDefault(x => x.Id == id);
|
|
|
|
public bool ContainsData(string id) => _datas.Any(x => x.Id == id);
|
|
|
|
public bool TryGetDataById(string id, out T data)
|
|
{
|
|
data = _datas.FirstOrDefault(x => x.Id == id);
|
|
return data != null;
|
|
}
|
|
|
|
public void SetDataList(List<T> newList)
|
|
{
|
|
_datas = newList;
|
|
}
|
|
|
|
public List<T> GetDataList()
|
|
{
|
|
return _datas;
|
|
}
|
|
|
|
public int GetDataCount() => _datas.Count;
|
|
}
|
|
} |