using UnityEngine; using System.Collections; using AX.TrackRecord; using UnityEngine.UI; using AX.NetworkSystem; using System.IO; using System.Collections.Generic; using AX.MessageSystem; using System.Xml; using System; using HelpXHSxml; using System.Xml.Serialization; public class SavePrepareCourse : MonoBehaviour { /// <summary> /// 保存备课课件 /// </summary> private InputField CourseName; private Text TheText; void Start() { CourseName = this.gameObject.transform.Find("InputField").GetComponent<InputField>(); TheText = CourseName.transform.Find("Text").GetComponent<Text>(); CourseName.onValidateInput += ValidCharDetect._OnValidateInput; } public void SaveCourseBtn() { if (TheText.text==null || TheText.text=="") { MessageDispatcher.SendMessage("Operatinghints", (object)"请输灾情名称"); return; } string ThePath = ""; //string path = ExamInfoHelpClass.CurrentWorkPath + "/" + "Coursewares/"; //string fileDir = Path.GetDirectoryName(path); //if (Directory.Exists(fileDir)) //{ // string[] files = Directory.GetFiles(fileDir); // foreach (string file in files) // { // string str = Path.GetFileName(file); // string ext = Path.GetExtension(file); // if (ext == ".xml") // { // ThePath += str + ";";//这种方式最后会多加一个‘;’ // } // } //} if (TheText.text.Contains("/") || TheText.text.Contains("\\")) { TheText.text = ""; MessageDispatcher.SendMessage("Operatinghints", (object)"名字不能包含/或\\"); return; } string path_ = Application.dataPath + @"/ExtendFolder/xml/SaveCoursewares/"; string fileDir_ = Path.GetDirectoryName(path_); if (Directory.Exists(fileDir_)) { string[] files = Directory.GetDirectories(path_); foreach (string file in files) { string str = Path.GetFileName(file); if (str.Equals(TheText.text.ToString().Trim())) { TheText.text = ""; MessageDispatcher.SendMessage("Operatinghints", (object)"已经有此名字"); return; } } } if (ExamInfoHelpClass.loadSceneMode == ExamInfoHelpClass.LoadSceneMode.PrepareMode) { string path = Application.dataPath + @"/ExtendFolder/xml/SaveCoursewares/"; string fileDir = Path.GetDirectoryName(path); if (Directory.Exists(fileDir)) { string[] files = Directory.GetDirectories(path); foreach (string file in files) { string str = Path.GetFileName(file); if (str.Equals(TheText.text.ToString().Trim())) { TheText.text = ""; MessageDispatcher.SendMessage("Operatinghints", (object)"已经有此名字"); return; } } } //创建该文件夹 string Firstpath = path + TheText.text.ToString().Trim() + "/"; Directory.CreateDirectory(Firstpath); //创建子存放xml的文件夹 string Secondpath = path + TheText.text.ToString().Trim() + "/" + "savexml/"; Directory.CreateDirectory(Secondpath); //移动namelistxml和xml文件 string namexml = Application.dataPath + @"/ExtendFolder/xml/NodeNameList.xml"; string fileDirnamexml = Path.GetDirectoryName(namexml); if (File.Exists(namexml)) { File.Move(namexml, Firstpath + "NodeNameList.xml"); } if (Directory.Exists(Secondpath)) { string[] files = Directory.GetFiles(ExamInfoHelpClass.CurrentWorkPath); foreach (string file in files) { string str = Path.GetFileName(file); File.Move(file, Secondpath + str); } } //图片xml路径 string picXmlDirPath = Application.dataPath + @"/ExtendFolder/xml/Coursewares/TemporaryPicfile/PicData.xml"; if (File.Exists(picXmlDirPath)) { File.Move(picXmlDirPath, Firstpath + "PicData.xml"); } for (int i = 0; i < NodeSet.Instance.NodeListName.Count; i++) { string name = NodeSet.Instance.NodeListName[i].ToString(); ThePath += name + ";";//这种方式最后会多加一个‘;’ } XML(TheText.text.ToString().Trim()); } else { string Filepath = NodeSet.Cousrsewse.FilePath.Substring(0, NodeSet.Cousrsewse.FilePath.Length - 1); string thepath = Path.GetFileNameWithoutExtension(Filepath); changenamexml(thepath, TheText.text.ToString().Trim());//修改XML的名称 string path = Application.dataPath + @"/ExtendFolder/xml/SaveCoursewares/"; File.Move(NodeSet.Cousrsewse.FilePath, path + TheText.text.ToString().Trim());//修改文件夹名称 } ///创建场景中XHS文件 XHSdata(TheText.text.ToString().Trim()); this.gameObject.SetActive(false); MySceneManager.BackToLastScene(); } public void XHSdata(string CourseName) { string XHSpath = Application.dataPath + @"/ExtendFolder/xml/SaveCoursewares/" + CourseName + "/" + "XHS.xml"; if (File.Exists(XHSpath)) { File.Delete(XHSpath); } RootRecordItem Root = new RootRecordItem(); List<GameObject> XHSlist = new List<GameObject>(); XHSlist = CreateXHS.instance.XHSList; if (XHSlist.Count == 0) { return; } for (int i = 0; i < XHSlist.Count; i++) { GameObject XHS = XHSlist[i] as GameObject; BaseDataAttri Attri = new BaseDataAttri(); Attri.buidType = XHS.GetComponent<CengID>().CengIDBuildType; Attri.CengID = XHS.GetComponent<CengID>().cengID; Attri.Name = XHS.name; Attri.position = XHS.transform.localPosition; Attri.scale = XHS.transform.localScale; Attri.rotation = XHS.transform.localEulerAngles; Root.BaseList.Add(Attri); } //序列化为xml XmlSerializer xml = new XmlSerializer(typeof(RootRecordItem)); StreamWriter writer = new StreamWriter(XHSpath); xml.Serialize(writer, Root); writer.Close(); } public void changenamexml(string name,string Name_) { string namexml = Application.dataPath + @"/ExtendFolder/xml/CourseXML.xml"; if (File.Exists(namexml)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(namexml); XmlNode root = xmlDoc.SelectSingleNode("CourseNameList"); XmlNodeList nodeList = root.ChildNodes; foreach (XmlElement elem in nodeList) { if (elem.Name == "CourseName") { foreach (XmlElement node in elem) { if (node.Name == "Name" && node.InnerText == name) { node.InnerText = Name_; } } } } xmlDoc.Save(namexml); } } public void XML(string name) { string namexml = Application.dataPath + @"/ExtendFolder/xml/CourseXML.xml"; //if (!File.Exists(namexml)) //{ // CreateXML(); //} WriteXML(namexml, name); } public void WriteXML(string path,string name) { string filepath = path; if (File.Exists(filepath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNode root = xmlDoc.SelectSingleNode("CourseNameList"); XmlNodeList nodeList = root.ChildNodes; foreach (XmlElement elem in nodeList) { if (elem.Name == "CourseName") { XmlElement jiedian = xmlDoc.CreateElement("Name"); jiedian.SetAttribute("ID", MySceneManager.GetActiveScene().name); jiedian.InnerText = name; elem.AppendChild(jiedian); root.AppendChild(elem); } } xmlDoc.AppendChild(root); xmlDoc.Save(filepath); } } public void CreateXML() { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); //根 XmlElement root = doc.CreateElement("CourseNameList"); doc.AppendChild(root); //创建节点(二级) XmlElement Name = doc.CreateElement("CourseName"); root.AppendChild(Name); doc.Save(Application.dataPath + @"/ExtendFolder/xml/CourseXML.xml"); Console.Write(doc.OuterXml); } public void CloseBtn() { RecordManager.Instance.IsPause = false; CourseName.text = ""; } public void GoBackSence() { } void OnDestroy() { //NodeSet.Instance.NodeListName.Clear(); } }