using AX.MessageSystem; using System; using System.Collections.Generic; using System.IO; using System.Xml; using UnityEngine; using UnityEngine.UI; /// /// 队伍名称设置 /// public class TeamNameSetting : ResourceLoadPanel { private Transform Father; private InputField TeamName; private static List TeamNameList = new List(); private void Start() { Init(); MessageDispatcher.AddListener("DisasterUpdateCompelete", ResetData); } private void OnDestroy() { MessageDispatcher.RemoveListener("DisasterUpdateCompelete", ResetData); } void Init() { TeamName = transform.Find("InputField").GetComponent(); Father = transform.Find(GlobalVariable.ContentPath); LoadTeamName(); } //创建Item private void CreateTeamNameItem() { GameObject TeamItem = Instantiate(Resources.Load("UI/TeamNameItem"), Father) as GameObject; TeamItem.GetComponent().SetTeamName(TeamName.text); TeamNameList.Add(TeamName.text); } //检测同名 public bool CheckSameName(string Name) { if (TeamNameList.Contains(Name)) { ResourceLoadWindow.Instance.LoadTextHintWindow("已有相同名称", 1f); return true; } //foreach (Transform child in Father) //{ // if (Name.Equals(child.GetComponent().GetTeamName())) // { // ResourceLoadWindow.Instance.LoadTextHintWindow("已有相同名称", 1f); // return true; // } //} return false; } //输入名字点击确定按钮 public void NameSureBtn() { if (string.IsNullOrEmpty(TeamName.text)) { return; } //判断空输入 if (CheckSameName(TeamName.text)) { return; } //判断相同名字 CreateTeamNameItem(); //生成条目 } //获取选中的队伍力量名称 public string GetPresentTeamName() { return GlobalVariable.TeamName = TeamNameItem.TeamName; } //获取队伍名称列表 public List GetTeamNameList() { return TeamNameList; } public void Close() { gameObject.SetActive(false); } public void Sure() { GetPresentTeamName(); gameObject.SetActive(false); SaveXml(); } private void SaveXml() { var id = DisasterManager.disaster.Id.ToString(); var filepath = Application.dataPath + "/Data/" + id + "/File/"+"TeamName.xml"; if (File.Exists(filepath)) { File.Delete(filepath); } File.Create(filepath).Dispose(); XmlDocument document = new XmlDocument(); XmlDeclaration xmldecl = document.CreateXmlDeclaration("1.0", "UTF-8", "yes"); document.AppendChild(xmldecl); XmlElement nodes = document.CreateElement("TeamNameList"); document.AppendChild(nodes); foreach (string teamName in TeamNameList) { XmlElement node = document.CreateElement("TeamName"); node.SetAttribute("Name", teamName); if (teamName == GlobalVariable.TeamName) { node.SetAttribute("Selected", 1.ToString()); } else { node.SetAttribute("Selected", 0.ToString()); } nodes.AppendChild(node); } document.Save(filepath); } private void ResetData(IMessage obj) { var id = DisasterManager.disaster.Id.ToString(); var filepath = Application.dataPath + "/Data/" + id + "/File/" + "TeamName.xml"; if (File.Exists(filepath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNode nodes = xmlDoc.SelectSingleNode("TeamNameList"); TeamNameList.Clear(); foreach (XmlElement node in nodes) { var teamName = node.GetAttribute("Name"); TeamNameList.Add(teamName); var selected = node.GetAttribute("Selected"); if (selected == "1") { TeamNameItem.TeamName = teamName; GlobalVariable.TeamName = teamName; } } RecreateTeamNameItems(); } } private void RecreateTeamNameItems() { foreach(Transform item in Father) { Destroy(item.gameObject); } foreach(string teamName in TeamNameList) { GameObject TeamItem = Instantiate(Resources.Load("UI/TeamNameItem"), Father) as GameObject; TeamItem.GetComponent().SetTeamName(teamName); } } private void LoadTeamName() { var id = DisasterManager.disaster.Id.ToString(); var filepath = Application.dataPath + "/Data/" + id + "/File/" + "TeamName.xml"; if (File.Exists(filepath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNode nodes = xmlDoc.SelectSingleNode("TeamNameList"); TeamNameList.Clear(); foreach (XmlElement node in nodes) { var teamName = node.GetAttribute("Name"); TeamNameList.Add(teamName); var selected = node.GetAttribute("Selected"); if (selected == "1") { TeamNameItem.TeamName = teamName; GlobalVariable.TeamName = teamName; } } RecreateTeamNameItems(); } } }