ShopManager.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. public class ShopManager
  6. {
  7. public Dictionary<int, ShopData> dataDict;
  8. public Dictionary<string, ShopData> codeDict;
  9. private ShopData vipData;
  10. private List<ShopData> coinList;
  11. private List<ShopData> cubeList;
  12. private List<ShopData> list;
  13. public ShopManager()
  14. {
  15. dataDict = new Dictionary<int, ShopData>();
  16. codeDict = new Dictionary<string, ShopData> ();
  17. coinList = new List<ShopData>();
  18. cubeList = new List<ShopData> ();
  19. list = new List<ShopData> ();
  20. InitByXML("XML/Config/All/shop_config");
  21. }
  22. public void InitByXML(string path)
  23. {
  24. TextAsset textAsset = (TextAsset)Resources.Load(path);
  25. XmlDocument xml = new XmlDocument();
  26. xml.LoadXml(textAsset.text);
  27. XmlNode mapNode = xml.SelectSingleNode("data");
  28. XmlNodeList nodeList = mapNode.SelectNodes("item");
  29. for(int i=0; i<nodeList.Count; i++)
  30. {
  31. ShopData data = new ShopData(nodeList[i]);
  32. if(!dataDict.ContainsKey(data.id))
  33. {
  34. dataDict.Add(data.id, data);
  35. if (data.sort == BuyUtil.ItemType.VIP.GetHashCode ())
  36. vipData = data;
  37. else if (data.sort == BuyUtil.ItemType.Coin.GetHashCode ())
  38. coinList.Add (data);
  39. else if (data.sort == BuyUtil.ItemType.Diam.GetHashCode ())
  40. cubeList.Add (data);
  41. if (!codeDict.ContainsKey (data.name)) {
  42. codeDict.Add (data.name, data);
  43. list.Add (data);
  44. }
  45. }
  46. else
  47. {
  48. Debuger.LogError("shop " + data.id + " has duplicate");
  49. }
  50. }
  51. }
  52. private static ShopManager instance;
  53. public static ShopManager GetInstance()
  54. {
  55. if(instance == null)
  56. {
  57. instance = new ShopManager();
  58. }
  59. return instance;
  60. }
  61. public ShopData GetVIPData()
  62. {
  63. return vipData;
  64. }
  65. public List<ShopData> GetCoinDataList()
  66. {
  67. return coinList;
  68. }
  69. public List<ShopData> GetCubeDataList()
  70. {
  71. return cubeList;
  72. }
  73. public List<ShopData> GetDataList()
  74. {
  75. return list;
  76. }
  77. public ShopData GetDataByCode(string code)
  78. {
  79. ShopData data = null;
  80. codeDict.TryGetValue (code, out data);
  81. return data;
  82. }
  83. }