unity创建xml与加载xml

摘要:
publicvoidCreateConfigFile(TransformcellParent){XmlDocumentxmlDoc=newXmlDocument();stringrootName=“ChessBoard”;XmlDeclarationxmldecl=xmlDoc.CreateXmlDeclaration(“1.0”,“UTF-8”,“”);xmlDoc.AppendChild(
  public void CreateConfigFile (Transform cellParent)
    {
        XmlDocument xmlDoc = new XmlDocument ();
        string rootName = "ChessBoard";
        XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration ("1.0", "UTF-8", "");
        xmlDoc.AppendChild (xmldecl);
        XmlElement root = xmlDoc.CreateElement (rootName);

        UICellItem[] itemArray = cellParent.GetComponentsInChildren<UICellItem> ();
        for (int i = 0, length = itemArray.Length; i < length; i++) {
            XmlElement item = xmlDoc.CreateElement ("Item");
            item.SetAttribute ("Pos", itemArray [i].coords.ToString ());
            item.SetAttribute ("TargetPos", itemArray [i].targetPos.ToString ());
            item.SetAttribute ("Direction", _getDirection (itemArray [i].directions).ToString ());
            item.SetAttribute ("Priority", _getPriority (itemArray [i].directions));
            root.AppendChild (item);
        }
        xmlDoc.AppendChild (root);
        xmlDoc.Save (GetFilePath ());
        MainController.instance.UiDialogBoxPanel.ShowMsgOneBtn ("文件生成成功,对应路径:" + GetFilePath (), null);
    }

    public void LoadConfigFile (Transform cellParent)
    {
        XmlNodeList nodes;
        if (!hasFile (GetFilePath ())) {
            return;
        } else {
            XmlDocument xml = new XmlDocument ();
            xml.Load (@GetFilePath ());
            nodes = xml.SelectNodes ("ChessBoard/Item");
        }

        UICellItem[] itemArray = cellParent.GetComponentsInChildren<UICellItem> ();
        for (int i = 0; i < itemArray.Length; i++) {
            itemArray [i].ResetItem ();
        }
        string[] strTemp;
        for (int i = 0; i < itemArray.Length; i++) {
            strTemp = nodes [i].Attributes ["TargetPos"].Value.Split (',');
            itemArray [i].SetTargetPosInfo (strTemp);
            strTemp = nodes [i].Attributes ["Priority"].Value.Split (',');
            itemArray [i].SetItemDirections (strTemp);
        }
    }

免责声明:文章转载自《unity创建xml与加载xml》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Visual Studio 2013 错误系统找不到指定文件,0x80070002反演小结下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[Unity基础]移动平台下的文件读写

From:http://blog.csdn.net/lyh916/article/details/52161633 参考链接: http://www.cnblogs.com/murongxiaopifu/p/4199541.html?utm_source=tuicool#autoid-3-2-0 http://zhaolongchn.blog.163...

Unity UGUI-Raw Image 组件(生图/未加工图片)

许久没有添加U3D随笔了,今补一条关于U3D亲儿子的东东 Unity UGUI-Raw Image 组件   Raw Image 组件是一个显示纹理贴图的组件   搭配 RenderTexture 将会有奇效   Raw Image 与 Image 的区别:   Image 显示的是 Sprite 精灵图片   Raw Image 可显示任何纹理贴图 (...

【Unity插件】NGUI核心组件之UIPanel .

转自:http://blog.csdn.net/daiguangda/article/details/7840084   UIPanel负责创建实际的集合图形。你不需要手动的添加UIPanel-一旦你创建一个控件,它会自动被添加。如果你想将你的UI渲染拆分到不同的Draw Call中,你可以手动创建你自己的UIPanel,例如你要创建一个分屏的游戏,每个屏...

Unity调用安卓Android的Toast

需求:在游戏中弹窗消息,调起安卓的Toast 项目中需要做Unity和安卓交互时,经常需要通过安卓Toast来做简单的输出,以便于测试。 方法一:Unity中,C#主导 //Unity调用安卓的土司 public static void MakeToast(stringinfo) { AndroidJavaClass unityPla...

UE4/Unity绘制地图基础元素-面和体

前言 绘制地图基础元素-线(上篇) 绘制地图基础元素-线(下篇) 搞定地图画线之后,接下来就是绘制面和体了: 面作为地图渲染的基本元素之一,在地图中可以代表各种形式的区域,例如海面、绿地等。面数据通常以离散点串形式存储,因此渲染时最关注的是如何将其展现为闭合的图形。 体可以理解为带有高度的面,在地图中代表各种建筑,通常是由其顶部面数据和高度数据处理得到。...

Unity3D中使用委托和事件

Unity3D中使用委托和事件  c#语言规范 阅读目录 1.C#中的委托、事件引入 2.方法的参数是方法 前言: 本来早就想写写和代码设计相关的东西了,以前做2DX的时候就有过写写观察者设计模式的想法,但是实践不多。现在转到U3D的怀抱中,倒是接触了不少委托事件的写法,那干脆就在此总结一下吧。 回到目录 1.C#中的委托、事件引入 本想去找...