|
- using System;
- using System.Collections.Generic;
- using SUISS.Core;
- using UnityEngine;
-
- namespace CIG
- {
- public abstract class AssetCollection<TSerializable, TKey, TValue, TSelf> : SingletonMonobehaviour<TSelf> where TSerializable : class where TSelf : AssetCollection<TSerializable, TKey, TValue, TSelf>
- {
- protected override void Awake()
- {
- base.Awake();
- this.BuildAssetCache();
- }
-
- public bool ContainsAsset(TKey key)
- {
- return this._cache.ContainsKey(key);
- }
-
- public TValue GetAsset(TKey key)
- {
- TValue result;
- if (this._cache.TryGetValue(key, out result))
- {
- return result;
- }
- return default(TValue);
- }
-
- protected abstract void GetAssetKeyValue(TSerializable asset, out TKey key, out TValue value);
-
- private void RemoveNullElements()
- {
- if (this._assets.RemoveAll((TSerializable a) => a == null) > 0)
- {
- UnityEngine.Debug.LogErrorFormat("There were missing elements or null elements in AssetCollection '{0}'", new object[]
- {
- base.name
- });
- }
- }
-
- private void BuildAssetCache()
- {
- this.RemoveNullElements();
- int count = this._assets.Count;
- for (int i = 0; i < count; i++)
- {
- TSerializable asset = this._assets[i];
- TKey tkey;
- TValue value;
- this.GetAssetKeyValue(asset, out tkey, out value);
- if (this._cache.ContainsKey(tkey))
- {
- UnityEngine.Debug.LogErrorFormat("Duplicate asset '{0}' in '{1}'", new object[]
- {
- tkey,
- base.name
- });
- }
- else
- {
- this._cache[tkey] = value;
- }
- }
- }
-
- [SerializeField]
- protected List<TSerializable> _assets;
-
- protected Dictionary<TKey, TValue> _cache = new Dictionary<TKey, TValue>();
- }
- }
|