Unity 通过XML保存场景物体信息
在项目工程中,将此脚本放置Editor文件夹中即可,在菜单栏中可以使用该命令导出场景信息内容保存为xml,文件存储到StreamingAssets文件夹中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | using System.IO; using System.Xml; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; public class ExportSceneInfoToXML : Editor { [MenuItem( "Tool/ExportSceneInfoToXML" )] static void ExportXML() { string filepath = Application.streamingAssetsPath + @"/sceneInfoXML.xml" ; if (File.Exists(filepath)) { File.Delete(filepath); } XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement( "scenes" ); //遍历所有的游戏场景 foreach (UnityEditor.EditorBuildSettingsScene s in UnityEditor.EditorBuildSettings.scenes) { //当关卡启用 if (s.enabled) { //得到当前场景的名称 string name = s.path; //打开这个场景 EditorSceneManager.OpenScene(name); XmlElement scene = xmlDoc.CreateElement( "scene" ); scene.SetAttribute( "name" , name); foreach (GameObject obj in Object.FindObjectsOfType( typeof (GameObject))) { //因为场景内物体都制作为prefab,所以只需遍历父物体而无需遍历子物体(prefab已包含子物体的信息) if (obj.transform.parent == null ) { XmlElement gameObject = xmlDoc.CreateElement( "gameObject" ); gameObject.SetAttribute( "name" , obj.name); gameObject.SetAttribute( "asset" , obj.name + ".prefab" ); XmlElement transform = xmlDoc.CreateElement( "transform" ); XmlElement position = xmlDoc.CreateElement( "position" ); XmlElement position_x = xmlDoc.CreateElement( "x" ); position_x.InnerText = obj.transform.position.x + "" ; XmlElement position_y = xmlDoc.CreateElement( "y" ); position_y.InnerText = obj.transform.position.y + "" ; XmlElement position_z = xmlDoc.CreateElement( "z" ); position_z.InnerText = obj.transform.position.z + "" ; position.AppendChild(position_x); position.AppendChild(position_y); position.AppendChild(position_z); XmlElement rotation = xmlDoc.CreateElement( "rotation" ); XmlElement rotation_x = xmlDoc.CreateElement( "x" ); rotation_x.InnerText = obj.transform.rotation.eulerAngles.x + "" ; XmlElement rotation_y = xmlDoc.CreateElement( "y" ); rotation_y.InnerText = obj.transform.rotation.eulerAngles.y + "" ; XmlElement rotation_z = xmlDoc.CreateElement( "z" ); rotation_z.InnerText = obj.transform.rotation.eulerAngles.z + "" ; rotation.AppendChild(rotation_x); rotation.AppendChild(rotation_y); rotation.AppendChild(rotation_z); XmlElement scale = xmlDoc.CreateElement( "scale" ); XmlElement scale_x = xmlDoc.CreateElement( "x" ); scale_x.InnerText = obj.transform.localScale.x + "" ; XmlElement scale_y = xmlDoc.CreateElement( "y" ); scale_y.InnerText = obj.transform.localScale.y + "" ; XmlElement scale_z = xmlDoc.CreateElement( "z" ); scale_z.InnerText = obj.transform.localScale.z + "" ; scale.AppendChild(scale_x); scale.AppendChild(scale_y); scale.AppendChild(scale_z); transform.AppendChild(position); transform.AppendChild(rotation); transform.AppendChild(scale); gameObject.AppendChild(transform); scene.AppendChild(gameObject); root.AppendChild(scene); xmlDoc.AppendChild(root); xmlDoc.Save(filepath); } } } } //刷新Project视图, 不然需要手动刷新哦 AssetDatabase.Refresh(); Debug.Log( "保存完成:" + filepath); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!