LocationPickerGroup.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Xml;
  7. namespace JC.Unity.Picker {
  8. public class LocationPickerGroup : MonoBehaviour
  9. {
  10. [SerializeField] TextAsset xmlFileText;
  11. [HideInInspector] public LocationParser locationParser;
  12. public LocationInfo selectLocation;
  13. public List<LocationPicker> locationPickerList;
  14. [SerializeField] Button btnClose;
  15. [SerializeField] Button btnEnter;
  16. public Action<LocationInfo> onEnter;
  17. void Awake()
  18. {
  19. locationParser = new LocationParser(xmlFileText);
  20. selectLocation = new LocationInfo(locationParser);
  21. Init();
  22. }
  23. void Start()
  24. {
  25. if (btnClose) {
  26. btnClose.onClick.AddListener(() => {
  27. Destroy(transform.GetComponentInParent<Canvas>().gameObject);
  28. });
  29. }
  30. if (btnEnter) {
  31. btnEnter.onClick.AddListener(() => {
  32. Destroy(transform.GetComponentInParent<Canvas>().gameObject);
  33. onEnter?.Invoke(selectLocation);
  34. });
  35. }
  36. }
  37. public void Init() {
  38. for (int i = 0; i < locationPickerList.Count; i++) {
  39. locationPickerList[i].myGroup = this;
  40. locationPickerList[i].Init();
  41. }
  42. int languageID = PlayerPrefs.GetInt("Language", 0);
  43. string title = languageID == 0 ? "地区选择" : "Region Select";
  44. string title0 = languageID == 0 ? "国家" : "Country";
  45. string title1 = languageID == 0 ? "省" : "State";
  46. string title2 = languageID == 0 ? "市" : "City";
  47. transform.Find("TopBar/Title").GetComponent<Text>().text = title;
  48. transform.Find("TitleBG/Item").GetComponentInChildren<Text>().text = title0;
  49. transform.Find("TitleBG/Item (1)").GetComponentInChildren<Text>().text = title1;
  50. transform.Find("TitleBG/Item (2)").GetComponentInChildren<Text>().text = title2;
  51. }
  52. public void onDateUpdate() {
  53. foreach (var item in locationPickerList) {
  54. item.Refresh();
  55. }
  56. }
  57. private float _height = -1;
  58. public float GetHeight() {
  59. if (_height == -1) {
  60. _height = transform.GetComponent<RectTransform>().sizeDelta.y;
  61. }
  62. return _height;
  63. }
  64. // void Test() {
  65. // LocationInfo locationInfo = new LocationInfo(locationParser);
  66. // while (true) {
  67. // if (!locationInfo.IsValid()) {
  68. // break;
  69. // } else {
  70. // while (true) {
  71. // if (!locationInfo.IsValid()) {
  72. // locationInfo.StateIndex = 0;
  73. // break;
  74. // } else {
  75. // while (true) {
  76. // if (!locationInfo.IsValid()) {
  77. // locationInfo.CityIndex = 0;
  78. // break;
  79. // } else {
  80. // Debug.Log(locationInfo.GetCountryRegion().Item1 + "," + locationInfo.GetState().Item1 + "," + locationInfo.GetCity().Item1);
  81. // }
  82. // locationInfo.CityIndex++;
  83. // }
  84. // }
  85. // locationInfo.StateIndex++;
  86. // }
  87. // }
  88. // locationInfo.CountryRegionIndex++;
  89. // }
  90. // }
  91. }
  92. public class LocationParser {
  93. public List<XmlNode> CountryRegionList = new List<XmlNode>();
  94. public LocationParser(TextAsset xmlFileText) {
  95. XmlDocument xmlDoc = new XmlDocument();
  96. xmlDoc.LoadXml(xmlFileText.text);
  97. XmlNode locationNode = xmlDoc.SelectSingleNode("Location");
  98. XmlNodeList xmlNodeList = locationNode.SelectNodes("CountryRegion");
  99. for (int i = 0; i < xmlNodeList.Count; i++) {
  100. CountryRegionList.Add(xmlNodeList[i]);
  101. }
  102. }
  103. }
  104. public class LocationInfo {
  105. private LocationParser locationParser;
  106. public int CountryRegionIndex = 0;
  107. public int StateIndex = 0;
  108. public int CityIndex = 0;
  109. public LocationInfo(LocationParser locationParser,
  110. int CountryRegionIndex = 0, int StateIndex = 0, int CityIndex = 0) {
  111. this.locationParser = locationParser;
  112. this.CountryRegionIndex = CountryRegionIndex;
  113. this.StateIndex = StateIndex;
  114. this.CityIndex = CityIndex;
  115. }
  116. public LocationInfo ChangeCountryRegionIndex(int num) {
  117. return new LocationInfo(locationParser, CountryRegionIndex + num, 0, 0);
  118. }
  119. public LocationInfo ChangeStateIndex(int num) {
  120. return new LocationInfo(locationParser, CountryRegionIndex, StateIndex + num, 0);
  121. }
  122. public LocationInfo ChangeCityIndex(int num) {
  123. return new LocationInfo(locationParser, CountryRegionIndex, StateIndex, CityIndex + num);
  124. }
  125. public Tuple<string, string> GetCountryRegion() {
  126. string Name = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Name"].Value;
  127. string Code = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Code"].Value;
  128. return new Tuple<string, string>(Name, Code);
  129. }
  130. public Tuple<string, string> GetState() {
  131. string Name = "";
  132. string Code = "";
  133. XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex];
  134. XmlNodeList stateList = countryRegionNode.SelectNodes("State");
  135. if (stateList.Count != 0) {
  136. XmlNode stateNode = stateList[StateIndex];
  137. XmlAttribute NameAtb = stateNode.Attributes["Name"];
  138. if (NameAtb != null) {
  139. Name = NameAtb.Value;
  140. }
  141. XmlAttribute CodeAtb = stateNode.Attributes["Code"];
  142. if (CodeAtb != null) {
  143. Code = CodeAtb.Value;
  144. }
  145. }
  146. return new Tuple<string, string>(Name, Code);
  147. }
  148. public Tuple<string, string> GetCity() {
  149. string Name = "";
  150. string Code = "";
  151. XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex];
  152. XmlNodeList stateList = countryRegionNode.SelectNodes("State");
  153. if (stateList.Count != 0) {
  154. XmlNode stateNode = stateList[StateIndex];
  155. XmlNodeList cityList = stateNode.SelectNodes("City");
  156. if (cityList.Count != 0) {
  157. XmlNode cityNode = cityList[CityIndex];
  158. XmlAttribute NameAtb = cityNode.Attributes["Name"];
  159. if (NameAtb != null) {
  160. Name = NameAtb.Value;
  161. }
  162. XmlAttribute CodeAtb = cityNode.Attributes["Code"];
  163. if (CodeAtb != null) {
  164. Code = CodeAtb.Value;
  165. }
  166. }
  167. }
  168. return new Tuple<string, string>(Name, Code);
  169. }
  170. public bool IsValid() {
  171. if (CountryRegionIndex < 0 || StateIndex < 0 || CityIndex < 0) {
  172. return false;
  173. }
  174. bool res1 = CountryRegionIndex < locationParser.CountryRegionList.Count;
  175. if (!res1) return false;
  176. XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex];
  177. XmlNodeList stateList = countryRegionNode.SelectNodes("State");
  178. if (stateList.Count == 0 && StateIndex == 0 && CityIndex == 0) return true;
  179. bool res2 = StateIndex < stateList.Count;
  180. if (!res2) return false;
  181. XmlNode stateNode = stateList[StateIndex];
  182. XmlNodeList cityList = stateNode.SelectNodes("City");
  183. if (cityList.Count == 0 && CityIndex == 0) return true;
  184. bool res3 = CityIndex < cityList.Count;
  185. if (!res3) return false;
  186. return true;
  187. }
  188. }
  189. }