Unity3D使用外部XML

作者: 李亚楠0219 | 来源:发表于2017-06-12 18:08 被阅读115次

很多时候,我们在编写软件的时候,都需要在软件外部对其进行配置。利用本文所述技巧,可以实现在外部XML文件中读取软件进行配置,下面不说废话,直接贴出XML和相应解析代码:

XML文件

<System>
    <Server>
        <ServerIP>192.168.1.252</ServerIP>
        <ServerPort>8080</ServerPort>
        <ServerUrl>/xxxx</ServerUrl>
    </Server>
    <RealSize>
        <XLength>2000</XLength>
        <YLength>2000</YLength>
    </RealSize>
    <Coord>
        <Mode>2</Mode>
    </Coord>
</System>

相应脚本

using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;
public class ParseXML : MonoBehaviour{
    private string serverIP;
    private string serverPort;
    private string serverUrl;
    //实际定位范围
    private string xLength;
    private string yLength;
    //坐标系模式
    private string mode;
    // Use this for initialization
    void Start(){
        string configPath = Application.dataPath;
        int num = configPath.LastIndexOf ("/");
        configPath = configPath.Substring (0,num);
        string url = configPath + "/config/systemconfig.xml";   
        /**
         * 开始解析XML文件 
         */
        XmlDocument xmlDoc = new XmlDocument();   
        xmlDoc.Load (url);
        if(xmlDoc.HasChildNodes){
            /**
             * 获取服务器信息
             */
            XmlNodeList nodeList=xmlDoc.GetElementsByTagName("Server");
            foreach (XmlNode node in nodeList){
                serverIP=node.SelectSingleNode("ServerIP").InnerText;
                serverPort=node.SelectSingleNode("ServerPort").InnerText;
                serverUrl=node.SelectSingleNode("ServerUrl").InnerText;
            }
            nodeList=xmlDoc.GetElementsByTagName("RealSize");
            foreach (XmlNode node in nodeList){
                xLength=node.SelectSingleNode("XLength").InnerText;
                yLength=node.SelectSingleNode("YLength").InnerText;
            }
            nodeList=xmlDoc.GetElementsByTagName("Coord");
            foreach (XmlNode node in nodeList){
                mode=node.SelectSingleNode("Mode").InnerText;
            }
        }   
    }
    public string getServerIP(){
        return serverIP;
    }
    public string getServerPort(){
        return serverPort;
    }
    public string getServerUrl(){
        return serverUrl;
    }
    public string getXLength(){
        return xLength;
    }
    public string getYLength(){
        return yLength;
    }
    public string getMode(){
        return mode;
    }
}

相关文章

  • Unity3D使用外部XML

    很多时候,我们在编写软件的时候,都需要在软件外部对其进行配置。利用本文所述技巧,可以实现在外部XML文件中读取软件...

  • 解决Cocos2d-x中文乱码

    读取外部xml或者Json文件显示中文1.xml方法 使用xml解析中文十分的简单,首先我们需要准备一个xml文件...

  • Springboot 2使用外部Tomcat源码分析

    Springboot 使用外部 Tomcat 修改 pom.xml,改为打 war 包 war 将 Springb...

  • XXE攻击原理

    1简述 XXE(XML External Entity)是指xml外部实体攻击漏洞。XML外部实体攻击是针对解析X...

  • Unquoted attribute value

    解决方法: config.xml 使用外部编辑器进行编辑 内部错误 还原原始版本

  • 移动端外部配置文件读写

    unity3d读取外部文件详细 : http://www.cnblogs.com/murongxiaopifu/...

  • JAVA的XXE漏洞

    1. XXE简介 XXE(XML外部实体注入,XML External Entity) ,漏洞在对不安全的外部实体...

  • XXE

    XXE(Xml external entity -injection) xml外部实体注入XML 指可扩展标记语言...

  • PHP 将xml和数组相互转化

    将xml字符串转化为数组 function xmlToArray($xml){ //禁止引用外部xml实体 l...

  • 2019-08-27

    XXE Injection即XML External Entity Injection,也就是XML外部实体注入攻...

网友评论

    本文标题:Unity3D使用外部XML

    本文链接:https://www.haomeiwen.com/subject/jftrqxtx.html