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); }}
}
- Unity_标记语言_Xml文件
- HTML语言剖析3:文件标记
- HTML语言剖析(三)文件标记
- C语言文件位置标记的定位
- 标记语言
- 案例_xml文件实现学生管理
- C#_xml文件请求及接收数据
- C#_xml文件请求及接收数据
- 29_xml文件的序列化
- python开发_xml.etree.ElementTree_XML文件操作
- Android开发_xml文件的访问
- Unity_FPS游戏_Xml文件读取流程_01
- 标记文件
- HTML标记语言--常用标记
- JSP学习笔记(7)_xml文件解析
- html标记语言
- 关于各种标记语言
- 可扩展标记语言
- 功能:可以将多个输入流进行串联读取
- MAC使用笔记
- Unity_DOTween动画的学习(一)_三种方式_Tween的控制&Call Back
- Struts2拦截器
- [LeetCode]152. Maximum Product Subarray
- Unity_标记语言_Xml文件
- Git的使用--将本地项目上传到Github
- HDU6201树形DP
- 剑指offer 30 最小的k个数
- 100. Same Tree
- 欢迎使用CSDN-markdown编辑器
- 请求转发与重定向的区别;
- 对应HTML dom,学习jQuery常见操作
- Codeforces 852D Exploration plan [spfa+二分+二分图匹配]