using System; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Fort23.GameData { public abstract class ConfigHolder { public abstract Type ConfigType { get; } public abstract IConfig GetFirst(); public abstract List GetAll(); public abstract IConfig TryGet(int type); public abstract void SetConfig(List config); public virtual void Init() { } } /// /// 管理所有的配置 /// /// [Serializable] public abstract class ConfigHolder : ConfigHolder where T : IConfig { public List configList; private Dictionary _dictionary; public override void Init() { _dictionary = new Dictionary(); foreach (var config in configList) { _dictionary.Add(config.GetID(), config); } } public override void SetConfig(List config) { if (configList != null) { return; } configList = new List(); for (int i = 0; i < config.Count; i++) { configList.Add((T) config[i]); } } public override Type ConfigType { get { return typeof(T); } } public override IConfig TryGet(int ID) { if (ID <= 0) { Debug.Log($"在表{typeof(T)}中没有找到ID:{ID}"); return null; } IConfig t; if (!_dictionary.TryGetValue(ID, out t)) { Debug.Log($"在表{typeof(T)}中没有找到ID:{ID}————{_dictionary.Count}"); return null; } return t; } public override List GetAll() { List configs = _dictionary.Values.ToList(); return configs; } public override IConfig GetFirst() { return _dictionary.Values.First(); } } }