Unity 通过服务器加载配置文件(Text/XML/Json)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leonardo_Davinci/article/details/78505336

思路:上传文件到本地服务器,通过www获取本地服务器中的配置文件数据,将配置文件中的数据存放到字典中方便使用


1.Json的写入格式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//通过放在Pluglns下的LitJson.dll自动搜索配置的
using LitJson;

public class JsonTest : MonoBehaviour {
    //创建Json文件路劲
    private string path = "Information.json";
    // Use this for initialization
    //用于接收json01字符串
    private string Json1;

    public void CreateJson()
    {
        JsonWriter writer = new JsonWriter();
        //开启Json的写入
        writer.WriteObjectStart();
        //英雄王昭君的信息
        writer.WritePropertyName("Name");
        writer.Write("王昭君");
        writer.WritePropertyName("Property");
        writer.Write("法师");
        writer.WritePropertyName("Skill");
        writer.Write("冰封千里");

        writer.WritePropertyName("Name");
        writer.Write("凯特琳");
        writer.WritePropertyName("Property");
        writer.Write("射手");
        writer.WritePropertyName("Skill");
        writer.Write("让子弹飞");

        //写入完成关闭写入
        writer.WriteObjectEnd();

        //把writer里面的所有Json文件
        Json1 = writer.ToString();
    }
    //通过OnGUI
    void OnGUI()
    {
        if (GUI.Button(new Rect(100, 100, 100, 50), "打印"))
        {
            CreateJson();
        }
        GUI.Label(new Rect(100, 200, 100, 100), json01);
    }
}


2.XML的文档写入格式

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

public class XMLTest : MonoBehaviour {
    //保存XML文件的路径
    private string path = "Information1.XML";
    private int xmlfile;


	// Use this for initialization
	void Start () {
        //合并路径
        path = Path.Combine(Application.streamingAssetsPath, path);

        CreateXML();
	}
	
    private void CreateXML()
    {
        //如果不存在这个路就创建这个文件路径
        if(!File.Exists(path))
        {
            File.Create(path);
        }
        //实例化XML文档
        XmlDocument xmlwriter = new XmlDocument();
        //写入根节点
        XmlElement root = xmlwriter.CreateElement("Character_Information");
        //写入子节点
        XmlElement characterMessager01 = xmlwriter.CreateElement("王昭君");
        //为子节点添加属性
        characterMessager01.SetAttribute("Property", "法师");
        characterMessager01.SetAttribute("Skill","千里冰封");

        //写入子节点
        XmlElement characterMessager02 = xmlwriter.CreateElement("凯特琳");
        //为子节点添加属性
        characterMessager02.SetAttribute("Property", "射手");
        characterMessager02.SetAttribute("Skill","让子弹飞");

        //给父节点添加子节点

        root.AppendChild(characterMessager01);
        root.AppendChild(characterMessager02);

        //把父节点添加到根节点
        xmlwriter.AppendChild(root);
        //文件保存
        xmlwriter.Save(path);
    }

}

3.从本地服务器读取文件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

public class WWWTest : MonoBehaviour {
    private WWW www;
    private string Url="http://localhost:8080/GoodsTable.txt";
    private string Url01 = "http://localhost:8080/Test.xml";
    public Dictionary<string, Dictionary<string, string>> inforAll =
        new Dictionary<string, Dictionary<string, string>>();
	// Use this for initialization
	IEnumerator Start () {
        www = new WWW(Url01);
        //等待www从服务器里面下载完成
        yield return www;
        //
        if(!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError(www.error);
        }
        else
        {
            ConfigXML(www.text);
        }
	}
    //XML文件提取
    public void ConfigXML(string xml)
    {
        XmlDocument xmlDoc = new XmlDocument();
        //加载传进来的字符串转化为XML文件
        xmlDoc.LoadXml(xml);
        //用于接收子节点的数据
        XmlElement element = null;
        //获取到Character_Information根节点下的所有子节点
        XmlNodeList list = xmlDoc.SelectSingleNode("Character_Information").ChildNodes;
        for (int i = 0; i < list.Count;i++)
        {
            element = list[i] as XmlElement;
            //把信息添加到字典中
            inforAll.Add(element.Name,new Dictionary<string, string>());
            inforAll[element.Name].Add(element.GetAttribute("Property"),
                                                            element.GetAttribute("Skill"));
        }
    }
    //TXT文件提取
    public void ConfigTXT(string config)
    {
        string[] items = null;
        string[] lines = null;
        //读出来的TXT文件是一长串字符串
        //按 # 将每行切分出来
        lines = (config.Trim()).Split('#');
        for (int i = 0; i < lines.Length;i++)
        {
            items = lines[i].Trim().Split('|');
            inforAll.Add(items[0],new Dictionary<string, string>());
            inforAll[items[0]].Add(items[1], items[2]);

        }
    }
}



猜你喜欢

转载自blog.csdn.net/leonardo_Davinci/article/details/78505336