You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 line
1.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Core;
  4. using UnityEngine;
  5. namespace CIG
  6. {
  7. public abstract class AssetCollection<TSerializable, TKey, TValue, TSelf> : SingletonMonobehaviour<TSelf> where TSerializable : class where TSelf : AssetCollection<TSerializable, TKey, TValue, TSelf>
  8. {
  9. protected override void Awake()
  10. {
  11. base.Awake();
  12. this.BuildAssetCache();
  13. }
  14. public bool ContainsAsset(TKey key)
  15. {
  16. return this._cache.ContainsKey(key);
  17. }
  18. public TValue GetAsset(TKey key)
  19. {
  20. TValue result;
  21. if (this._cache.TryGetValue(key, out result))
  22. {
  23. return result;
  24. }
  25. return default(TValue);
  26. }
  27. protected abstract void GetAssetKeyValue(TSerializable asset, out TKey key, out TValue value);
  28. private void RemoveNullElements()
  29. {
  30. if (this._assets.RemoveAll((TSerializable a) => a == null) > 0)
  31. {
  32. UnityEngine.Debug.LogErrorFormat("There were missing elements or null elements in AssetCollection '{0}'", new object[]
  33. {
  34. base.name
  35. });
  36. }
  37. }
  38. private void BuildAssetCache()
  39. {
  40. this.RemoveNullElements();
  41. int count = this._assets.Count;
  42. for (int i = 0; i < count; i++)
  43. {
  44. TSerializable asset = this._assets[i];
  45. TKey tkey;
  46. TValue value;
  47. this.GetAssetKeyValue(asset, out tkey, out value);
  48. if (this._cache.ContainsKey(tkey))
  49. {
  50. UnityEngine.Debug.LogErrorFormat("Duplicate asset '{0}' in '{1}'", new object[]
  51. {
  52. tkey,
  53. base.name
  54. });
  55. }
  56. else
  57. {
  58. this._cache[tkey] = value;
  59. }
  60. }
  61. }
  62. [SerializeField]
  63. protected List<TSerializable> _assets;
  64. protected Dictionary<TKey, TValue> _cache = new Dictionary<TKey, TValue>();
  65. }
  66. }