Unity_标记语言_Xml文件

来源:互联网 发布:seo日志分析 编辑:程序博客网 时间:2024/06/06 05:23

可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。
在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如文章等。它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。 它非常适合万维网传输,提供统一的方法来描述和交换独立于应用程序或供应商的结构化数据。是Internet环境中跨平台的、依赖于内容的技术,也是当今处理分布式结构信息的有效工具。早在1998年,W3C就发布了XML1.0规范,使用它来简化Internet的文档信息传输。

Xml (数据交换)文件的创建方式
首先要在程序中创建C#脚本,打开脚本,Ctrl+N创建下图文件
这里写图片描述

创建后代码如下
这里写图片描述
文件格式以为根节

using UnityEngine;
using System.Collections;
public class TestVR : MonoBehaviour {

// Use this for initializationvoid Start () { //新建xml文档对象    TextAsset bossCfgInfo = Resources.Load<TextAsset>("Config/Boosfig");    Debug.Log(bossCfgInfo.text);}

}

这里写图片描述

using UnityEngine;
using System.Collections;
public class VRGameManager : MonoBehaviour {

    //游戏唯一入口,方便程序维护,减少拖拽操作// Use this for initializationvoid Start () {    ConfigManager.Instance.LoadAllConfig();    BossCfg dog = ConfigManager.Instance.GetBossCfgByID(1);}

}
这里创建 VRGameManager时需注意不要与系统Gamemanager重名
这里写图片描述
//单例模式,确保唯一
using UnityEngine;
using System.Collections;
public class Singletonwhere T :class ,new () {

private static T _instance;public static T Instance{    get    {        if (_instance==null)        {            _instance = new T();        }        return _instance;    }}

}

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
//处理加载,缓存,获取配置的管理器
public class ConfigManager:Singleton {

 private  Dictionary<int, BossCfg> _bossCfgDic = new Dictionary<int, BossCfg>();public  void LoadAllConfig(){    LoadBoosCfg();} private void LoadBoosCfg()    TextAsset bossCfgInfo = Resources.Load<TextAsset>("Config/Boosfig");    Debug.Log(bossCfgInfo.text);    //新建xml文档对象    XmlDocument document = new XmlDocument();    //处理(二进制流)流的    //document.Load();    //把字符串加载到对象中    document.LoadXml(bossCfgInfo.text);    //获取根节点.SelectSingleNode(选择单一节点)    XmlNode rootNode = document.SelectSingleNode("Root");    //获取所有子节点列表    XmlNodeList nodeList = rootNode.ChildNodes;    //遍历转型    foreach (XmlNode node in nodeList)    {        //节点 转元素类型        XmlElement element = node as XmlElement;        BossCfg bossCfg = new BossCfg();        bossCfg.ID = int.Parse(element.GetAttribute("ID"));        bossCfg.Name = element.GetAttribute("Name");        bossCfg.Hp = int.Parse(element.GetAttribute("Hp"));        bossCfg.AttackType = element.GetAttribute("AttackType");        _bossCfgDic.Add(bossCfg.ID, bossCfg);        Debug.Log(element.GetAttribute("ID"));        Debug.Log(element.GetAttribute("Name"));        Debug.Log(element.GetAttribute("Hp"));                                      Debug.Log(element.GetAttribute("AttackType"));        Debug.Log(element.InnerText);    }}// Get 读取public BossCfg GetBossCfgByID(int id){    if (_bossCfgDic.ContainsKey(id))    {        return _bossCfgDic[id];    }    return null;}

}

//映射对象+字典=保存 save public class BossCfg{public int ID;public string Name;public int Hp;public string AttackType;}

反射+泛型读取xml文件
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System;
using System.Reflection;
//逻辑相同 类型不同–泛型
///处理加载,缓存,获取配置的管理器
public class Test01 : Singleton
{

private Dictionary<int, BossCfgA> _bossCfgDic = new Dictionary<int, BossCfgA>();private Dictionary<int, WeaponCfgA> _weaponCfgDic = new Dictionary<int, WeaponCfgA>();private Dictionary<int, ShieldCfg> _CfgDic = new Dictionary<int, ShieldCfg>();public void LoadAllConfig(){    _bossCfgDic = LoadConfig<BossCfgA>("BossConfig");    //_weaponCfgDic = LoadConfig<WeaponCfgA>("WeaponConfig");    Debug.Log(123);}//路径不用private Dictionary<int ,T> LoadConfig<T>(string fileName)where T :class ,new (){    Dictionary<int, T> dic = new Dictionary<int, T>();    TextAsset bossCfgInfo = Resources.Load<TextAsset>("Config/Boosfig");    Debug.Log(bossCfgInfo.text);    ///新建xml文档对象    XmlDocument document = new XmlDocument();    ///处理(二进制流)流的    ///document.Load();    ///把字符串加载到对象中    document.LoadXml(bossCfgInfo.text);    ///获取根节点.SelectSingleNode(选择单一节点)    XmlNode rootNode = document.SelectSingleNode("Root");    ///获取所有子节点列表    XmlNodeList nodeList = rootNode.ChildNodes;    ///遍历转型    foreach (XmlNode node in nodeList)    {        ///节点 转元素类型        XmlElement element = node as XmlElement;        T cfgObj = new T();        T obj = CreateAndSetValue<T>(element);        dic.Add(int.Parse(element.GetAttribute("ID")),obj);    }    return dic;}//创建映射对象 以及 设置对象的值public T CreateAndSetValue<T>(XmlElement element)where T:class ,new (){    T obj = new T();//T obj = Activator.CreateInstance<T>();(反射)    FieldInfo[] fieldInfos = typeof(T).GetFields();    foreach (FieldInfo fieldInfo in fieldInfos)    {        string value = element.GetAttribute(fieldInfo.Name);        ParsePropertyValue<T>(fieldInfo, obj, value);    }    return obj;}//转换对应的类型public void ParsePropertyValue<T>(FieldInfo fieldInfo,T obj,string valueString) where T : class, new(){    System.Object value = null;    if (fieldInfo.FieldType.IsEnum)    {        value = Enum.Parse(fieldInfo.FieldType, valueString);    }    else    {        if (fieldInfo.FieldType==typeof(int))        {            value = int.Parse(valueString);        }        if (fieldInfo.FieldType == typeof(float))        {            value = float.Parse(valueString);        }        if (fieldInfo.FieldType == typeof(bool))        {            value = bool.Parse(valueString);        }        if (fieldInfo.FieldType == typeof(string))        {            value = valueString;        }    }    fieldInfo.SetValue(obj, value);} /// Get 读取public BossCfgA GetBossCfgByID(int id){    if (_bossCfgDic.ContainsKey(id))    {        return _bossCfgDic[id];    }    return null;}

}
///映射对象+字典=保存 save
public class BossCfgA {

public int ID;public string Name;public int Hp;public string AttackType;

}
public class WeaponCfgA
{

public int ID;public string Name;public int Hp;public string AttackType;

}
public class ShieldCfg
{

public int ID;public string Name;public int Hp;public string AttackType;

}

using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
public class ReflectionTest : MonoBehaviour {

// Use this for initializationvoid Start () {    GetType<BossCfgA>();    GetType<WeaponCfgA>();    GetType<BossCfgA>();}// Update is called once per framevoid Update () {}public void GetType<T>()where T :class ,new (){    T obj = new T();    //获取类型的关键字    Type type = typeof(T);    //返回所有公共字段, 方法三要素    FieldInfo[] fieldInfos = type.GetFields();    for (int i = 0; i < fieldInfos.Length; i++)    {        Debug.Log(fieldInfos[i].FieldType.Name+"      "+  fieldInfos[i].Name);    }}

}

原创粉丝点击