ByteManagerBase.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using Engine.CLoader;
  6. namespace Engine.Data
  7. {
  8. /// <summary>静态数据管理对象基类</summary>
  9. public class ByteManagerBase
  10. {
  11. /// <summary>数据字典</summary>
  12. protected Dictionary<int, ByteDataBase> DictData = new Dictionary<int, ByteDataBase>();
  13. /// <summary>数据列表</summary>
  14. protected List<ByteDataBase> ListData = new List<ByteDataBase>();
  15. /// <summary>读取角色数据</summary>
  16. public void ReadInfo<T>(UnityEngine.Object dataObj) where T : new()
  17. {
  18. UnityEngine.TextAsset textAsset = (UnityEngine.TextAsset)dataObj;
  19. ByteDataArray byteArr = new ByteDataArray(textAsset.bytes);
  20. int infoCount = byteArr.ReadInt();
  21. for (int i = 0; i < infoCount; i++)
  22. {
  23. //创建Info对象
  24. ByteDataBase infoBase = (new T()) as ByteDataBase;
  25. //读取数据
  26. infoBase.ReadInfo(byteArr);
  27. //添加数据
  28. AddInfo(infoBase);
  29. }
  30. }
  31. /// <summary>添加数据</summary>
  32. protected virtual void AddInfo(ByteDataBase infoBase)
  33. {
  34. if (!DictData.ContainsKey(infoBase.b_id))
  35. {
  36. DictData.Add(infoBase.b_id, infoBase);
  37. ListData.Add(infoBase);
  38. }
  39. else
  40. {
  41. CDebug.LogError("数据重复:type=" + infoBase.GetType().ToString() + " id=" + infoBase.b_id);
  42. }
  43. }
  44. /// <summary>查找一个静态数据对象</summary>
  45. public T FindInfoByID<T>(int id) where T : ByteDataBase
  46. {
  47. if(DictData.ContainsKey(id))
  48. {
  49. return DictData[id] as T;
  50. }
  51. else
  52. {
  53. return null;
  54. }
  55. }
  56. /// <summary>查找一个静态数据对象</summary>
  57. public T FindInfoByIndex<T>(int index) where T : ByteDataBase
  58. {
  59. return ListData[index] as T;
  60. }
  61. /// <summary>获得全部数据</summary>
  62. public List<ByteDataBase> GetAllInfo()
  63. {
  64. return ListData;
  65. }
  66. /// <summary>数据个数</summary>
  67. public int InfoCount
  68. {
  69. get
  70. {
  71. return ListData.Count;
  72. }
  73. }
  74. }
  75. }