| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- 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<LocationPicker> locationPickerList;
- [SerializeField] Button btnClose;
- [SerializeField] Button btnEnter;
- public Action<LocationInfo> onEnter;
-
- void Awake()
- {
- locationParser = new LocationParser(xmlFileText);
- selectLocation = new LocationInfo(locationParser);
- Init();
- }
- void Start()
- {
- if (btnClose) {
- btnClose.onClick.AddListener(() => {
- Destroy(transform.GetComponentInParent<Canvas>().gameObject);
- });
- }
- if (btnEnter) {
- btnEnter.onClick.AddListener(() => {
- Destroy(transform.GetComponentInParent<Canvas>().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>().text = title;
- transform.Find("TitleBG/Item").GetComponentInChildren<Text>().text = title0;
- transform.Find("TitleBG/Item (1)").GetComponentInChildren<Text>().text = title1;
- transform.Find("TitleBG/Item (2)").GetComponentInChildren<Text>().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<RectTransform>().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<XmlNode> CountryRegionList = new List<XmlNode>();
- 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<string, string> GetCountryRegion() {
- string Name = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Name"].Value;
- string Code = locationParser.CountryRegionList[CountryRegionIndex].Attributes["Code"].Value;
- return new Tuple<string, string>(Name, Code);
- }
- public Tuple<string, string> 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<string, string>(Name, Code);
- }
- public Tuple<string, string> 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<string, string>(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;
- }
- }
- }
|