Unity 动态生成 xml文档

本文档介绍了如何在Unity游戏中动态生成XML文件,详细阐述了利用字符串操作和XML类库进行文件创建的过程,适用于需要在游戏中保存数据或实现配置文件动态更新的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


现在来记录一下: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!");
		}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值