加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Unity 动态生成 xml文档

发布时间:2020-12-16 06:00:36 所属栏目:百科 来源:网络整理
导读:现在来记录一下:Unity 中动态生成 xml文件: //xml保存的路径,这里放在Assets路径 注意路径。string filepath = Application.dataPath + @"/my.xml"; //继续判断当前路径下是否有该文件if(!File.Exists (filepath)){ //创建XML文档实例 XmlDocument xmlDoc


现在来记录一下:Unity 中动态生成 xml文件:

     //xml保存的路径,这里放在Assets路径 注意路径。
		string filepath = Application.dataPath + @"/my.xml";
               //继续判断当前路径下是否有该文件
		if(!File.Exists (filepath))
		{
                         //创建XML文档实例
			 XmlDocument xmlDoc = new XmlDocument();
                         //创建root节点,也就是最上一层节点
			 XmlElement root = xmlDoc.CreateElement("transforms");
                          //继续创建下一层节点
			 XmlElement elmNew = xmlDoc.CreateElement("rotation");
                        //设置节点的两个属性 ID 和 NAME
		     elmNew.SetAttribute("id","0");
 		     elmNew.SetAttribute("name","momo");
		       //继续创建下一层节点
        	     XmlElement rotation_X = xmlDoc.CreateElement("x");
                     //设置节点中的数值
                     rotation_X.InnerText = "0";
                    XmlElement rotation_Y = xmlDoc.CreateElement("y");
                    rotation_Y.InnerText = "1";
                    XmlElement rotation_Z = xmlDoc.CreateElement("z");
                     rotation_Z.InnerText = "2";
                    //这里在添加一个节点属性,用来区分。。
   		   rotation_Z.SetAttribute("id","1");
 
             //把节点一层一层的添加至XMLDoc中 ,请仔细看它们之间的先后顺序,这将是生成XML文件的顺序
             elmNew.AppendChild(rotation_X);
             elmNew.AppendChild(rotation_Y);
             elmNew.AppendChild(rotation_Z);
	     root.AppendChild(elmNew);
             xmlDoc.AppendChild(root);
             //把XML文件保存至本地
             xmlDoc.Save(filepath);
			 Debug.Log("createXml OK!");
		}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读