保存物体下的子物体信息到Xml 里面

<新手上路>(保存物体下的子物体信息到Xml 里面)

2016-12-20

根据个人特殊需要所写

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using UnityEditor;

public class SaveMapCtr
{
[MenuItem("Tools/Creat MapInfo")]
public static void CreatMapInfo()
{
//选中的物体
Transform tParent=((GameObject)Selection.activeObject).transform;
XmlDocument xml = CreatXml();
//遍历所选择物体下的子物体 子物体名作为ID名保存到节点下
foreach (Transform item in tParent)
{
//添加根节点
AddNodeXml(xml, item.name);
//添加子节点
GetRootSetObj(xml, item);
}
SaveXML(xml);
}
//根据物体名 进行分割 区分物体名 和预制物名 和位置参数
public static void GetRootSetObj(XmlDocument xml,Transform root)
{
foreach (Transform item in root)
{
string[] SliptName = DivisionStr(item.name);
AddNodeChildXml(xml, root.name,item.name,SliptName[0], item.transform.position, item.transform.localEulerAngles, item.transform.localScale);
}
}
//创建节点
public static XmlDocument CreatXml()
{
string filepath = Application.dataPath + "/MapInfoXml.xml";
//判断当前路径下是否有该文件 如果存在就删除重新创建
if (File.Exists(filepath))
{
File.Delete(filepath);
}
XmlDocument xml = new XmlDocument();
xml.AppendChild(xml.CreateElement("Root"));
return xml;
}
//添加根节点
public static void AddNodeXml(XmlDocument xml, string id)
{
//获取根节点
XmlNode root = xml.SelectSingleNode("Root");
//添加元素
XmlElement element = xml.CreateElement("Scene");
element.SetAttribute("Id", id);
root.AppendChild(element);
}
//添加子节点
public static void AddNodeChildXml(XmlDocument xml, string id, string objName, string prefabName, Vector3 pos, Vector3 rotate, Vector3 scal)
{
XmlNode root = xml.SelectSingleNode("Root");
foreach (XmlElement element in root)
{
if (element.GetAttribute("Id") == id)
{
//在Node节点下添加子节点
XmlElement titleElelment = xml.CreateElement("object");
titleElelment.InnerText = objName + "," + prefabName + "," + pos.x.ToString() + "," + pos.y.ToString() + "," + pos.z.ToString()+","+ rotate.x.ToString() + "," + rotate.y.ToString() + "," + rotate.z.ToString() +","+ scal.x.ToString() + "," + scal.y.ToString() + "," + scal.z.ToString();
element.AppendChild(titleElelment);
root.AppendChild(element);
}
}
}
public static void SaveXML(XmlDocument xml)
{
//存储xml文件
xml.Save(Application.dataPath + "/MapInfoXml.xml");
}
//根据分号来分割字符串
public static string[] DivisionStr(string str)
{
return str.Split(‘_‘);
}
}

时间: 2024-08-07 04:23:32

保存物体下的子物体信息到Xml 里面的相关文章

将一个物体设置为另一个物体的子物体----NGUITools.AddChild()方法

using UnityEngine; using System.Collections; public class LoadPrefab : MonoBehaviour { //声明UIRoot这个物体的引用,待会儿将会在这个物体下生成子物体 public GameObject uiRoot; //声明要加载的子物体预设的名称 string prefabName = "Template"; void Start () { if (uiRoot != null) { //根据路径将预设加

获取子物体数量---Transform.childCount

如何判断一个物体下是否有子物体?getchild(0)!=null?显然不可取 那去获取拿到子物体数量?transform.GetChildCount();可以解决 但在新版本中已被弃用,可用transform.childCount来直接获取子物体数量. using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { void Example() { print(transform.

怎么在ZBrush中通过遮罩得到子物体

ZBrush® 中通过遮罩为模型添加子物体的方法简单且方便,我们可以通过按住Ctrl键绘制遮罩结合相关命令创建具有抽出厚度的模型提取出作为子物体附在模型表面.本文将详细介绍在Zbrush中如何通过遮罩得到子物体. 若有疑问可直接访问:http://www.zbrushcn.com/changjian/zbrush-4r7-zhezhao-ziwuti.html 1.在Light Box(灯光盒子)默认的3D工具选择PolySphere(多边形球体)物体,单击Tool>Make PolyMesh3

ZBrush通过遮罩得到子物体的方法

ZBrush中通过遮罩为模型添加子物体的方法简单且方便,我们可以通过按住Ctrl键绘制遮罩结合相关命令创建具有抽出厚度的模型提取出作为子物体附在模型表面.本文将详细介绍在Zbrush中如何通过遮罩得到子物体. 1.在Light Box(灯光盒子)默认的3D工具选择PolySphere(多边形球体)物体,单击Tool>Make PolyMesh3D(工具>创建3D多边形网格)命令,将参数化球体转换为多边形网格,并将其SDiv(细分层级)设置为4. 2.接下来通过绘制遮罩来创建子物体,遮罩的绘制相

遍历子物体

2013-03-12 14:11 1439人阅读 评论(0) 收藏 举报 [javascript] view plaincopyprint? //应用户提问 也给大家分享一下 可以查找某个物体下无限层级的子物体名称 并返回该子物体 //把这个函数放到你的代码中 check代表你要查询的物体 name为名称 如return GetTransform(transform,"bone12"); Transform GetTransform(Transform check,string nam

unity批量获取物体组件修改值,拓展子物体查询

using UnityEngine; using System.Collections; public class Game : MonoBehaviour { // Use this for initialization void Start () { FindMaterials(this.transform); } // 根据 物体名称 获取 物体下的任何地方的子物体 void FindChild(Transform go,string name,ref Transform tr) { if

ZBrush通过显示与隐藏得到子物体

在ZBrush?中得到子物体的方法有很多,本文将为大家介绍一种新的创建子物体的方法,通过显示和隐藏得到子物. ZBrush 4R8中文版下载:http://wm.makeding.com/iclk/?zoneid=19415 1.在Light Box(灯光盒子)默认的3D工具中选择人体模型,在视图文档中拖拽出来,进入Edit(编辑)模式,然后展开Geometry(几何体)面板,单击Divide(细分)按钮,将球体模型细分为3级,如下图所示. 2.按住Shift键和Ctrl键,在人体模型的腰部上方

如何 正确 删除 子物体

这几天做项目的时候发现 自己删除子物体之后,再新建子物体,发现原来的子物体的函数依旧被调用了. 后来看了一下,发现是由于子物体绑定了一个父物体的委托,在销毁的时候没有去除父物体的委托. 但是这个调试 引发了我另外一个思考: 我发现在删除子物体之后,调用 tranform.childcout 属性,发现没有变为0. 删除子物体代码如下: 1 void DetoryChilds(Transform tar) 2 { 3 for (int i = tar.childCount - 1; i >= 0;

ZBrush通过绘制层得到子物体方法

本文将为大家介绍在ZBrush软件中第三种创建子物体的方法,即使用绘制层得到子物体. 1.在Light Box(灯光盒子)默认的3D工具中选择SuperAverageMan_low人体模型,在视图文档中拖拽出来,进入Edit(编辑)模式,然后展开Geometry(几何体)面板,单击Divide(细分)按钮,将球体模型细分为5级. 2.单击Tool>Layers(工具>绘制层)面板中单击New(新建)按钮,创建一个新层,选择合适的笔刷和Alpha,在模型上拖拽鼠标左键,得到一个方形纹理,如下图所