using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Xml; namespace JC.Unity.Picker { public class LocationPickerGroup : MonoBehaviour { [SerializeField] TextAsset xmlFileText; [HideInInspector] public LocationParser locationParser; public LocationInfo selectLocation; public List locationPickerList; [SerializeField] Button btnClose; [SerializeField] Button btnEnter; public Action onEnter; void Awake() { locationParser = new LocationParser(xmlFileText); selectLocation = new LocationInfo(locationParser); Init(); } void Start() { if (btnClose) { btnClose.onClick.AddListener(() => { Destroy(transform.GetComponentInParent().gameObject); }); } if (btnEnter) { btnEnter.onClick.AddListener(() => { Destroy(transform.GetComponentInParent().gameObject); onEnter?.Invoke(selectLocation); }); } } public void Init() { for (int i = 0; i < locationPickerList.Count; i++) { locationPickerList[i].myGroup = this; locationPickerList[i].Init(); } int languageID = PlayerPrefs.GetInt("Language", 0); string title = languageID == 0 ? "地区选择" : "Region Select"; string title0 = languageID == 0 ? "国家" : "Country"; string title1 = languageID == 0 ? "省" : "State"; string title2 = languageID == 0 ? "市" : "City"; transform.Find("TopBar/Title").GetComponent().text = title; transform.Find("TitleBG/Item").GetComponentInChildren().text = title0; transform.Find("TitleBG/Item (1)").GetComponentInChildren().text = title1; transform.Find("TitleBG/Item (2)").GetComponentInChildren().text = title2; } public void onDateUpdate() { foreach (var item in locationPickerList) { item.Refresh(); } } private float _height = -1; public float GetHeight() { if (_height == -1) { _height = transform.GetComponent().sizeDelta.y; } return _height; } // void Test() { // LocationInfo locationInfo = new LocationInfo(locationParser); // while (true) { // if (!locationInfo.IsValid()) { // break; // } else { // while (true) { // if (!locationInfo.IsValid()) { // locationInfo.StateIndex = 0; // break; // } else { // while (true) { // if (!locationInfo.IsValid()) { // locationInfo.CityIndex = 0; // break; // } else { // Debug.Log(locationInfo.GetCountryRegion().Item1 + "," + locationInfo.GetState().Item1 + "," + locationInfo.GetCity().Item1); // } // locationInfo.CityIndex++; // } // } // locationInfo.StateIndex++; // } // } // locationInfo.CountryRegionIndex++; // } // } } public class LocationParser { public List CountryRegionList = new List(); public LocationParser(TextAsset xmlFileText) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlFileText.text); XmlNode locationNode = xmlDoc.SelectSingleNode("Location"); XmlNodeList xmlNodeList = locationNode.SelectNodes("CountryRegion"); for (int i = 0; i < xmlNodeList.Count; i++) { CountryRegionList.Add(xmlNodeList[i]); } } } public class LocationInfo { private LocationParser locationParser; public int CountryRegionIndex = 0; public int StateIndex = 0; public int CityIndex = 0; public LocationInfo(LocationParser locationParser, int CountryRegionIndex = 0, int StateIndex = 0, int CityIndex = 0) { this.locationParser = locationParser; this.CountryRegionIndex = CountryRegionIndex; this.StateIndex = StateIndex; this.CityIndex = CityIndex; } public LocationInfo ChangeCountryRegionIndex(int num) { return new LocationInfo(locationParser, CountryRegionIndex + num, 0, 0); } public LocationInfo ChangeStateIndex(int num) { return new LocationInfo(locationParser, CountryRegionIndex, StateIndex + num, 0); } public LocationInfo ChangeCityIndex(int num) { return new LocationInfo(locationParser, CountryRegionIndex, StateIndex, CityIndex + num); } public Tuple GetCountryRegion() { string Name = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Name"].Value; string Code = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Code"].Value; return new Tuple(Name, Code); } public Tuple GetState() { string Name = ""; string Code = ""; XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex]; XmlNodeList stateList = countryRegionNode.SelectNodes("State"); if (stateList.Count != 0) { XmlNode stateNode = stateList[StateIndex]; XmlAttribute NameAtb = stateNode.Attributes["Name"]; if (NameAtb != null) { Name = NameAtb.Value; } XmlAttribute CodeAtb = stateNode.Attributes["Code"]; if (CodeAtb != null) { Code = CodeAtb.Value; } } return new Tuple(Name, Code); } public Tuple GetCity() { string Name = ""; string Code = ""; XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex]; XmlNodeList stateList = countryRegionNode.SelectNodes("State"); if (stateList.Count != 0) { XmlNode stateNode = stateList[StateIndex]; XmlNodeList cityList = stateNode.SelectNodes("City"); if (cityList.Count != 0) { XmlNode cityNode = cityList[CityIndex]; XmlAttribute NameAtb = cityNode.Attributes["Name"]; if (NameAtb != null) { Name = NameAtb.Value; } XmlAttribute CodeAtb = cityNode.Attributes["Code"]; if (CodeAtb != null) { Code = CodeAtb.Value; } } } return new Tuple(Name, Code); } public bool IsValid() { if (CountryRegionIndex < 0 || StateIndex < 0 || CityIndex < 0) { return false; } bool res1 = CountryRegionIndex < locationParser.CountryRegionList.Count; if (!res1) return false; XmlNode countryRegionNode = locationParser.CountryRegionList[CountryRegionIndex]; XmlNodeList stateList = countryRegionNode.SelectNodes("State"); if (stateList.Count == 0 && StateIndex == 0 && CityIndex == 0) return true; bool res2 = StateIndex < stateList.Count; if (!res2) return false; XmlNode stateNode = stateList[StateIndex]; XmlNodeList cityList = stateNode.SelectNodes("City"); if (cityList.Count == 0 && CityIndex == 0) return true; bool res3 = CityIndex < cityList.Count; if (!res3) return false; return true; } } }