Unity_FPS游戏_Xml文件读取流程_01
来源:互联网 发布:mcake 知乎 编辑:程序博客网 时间:2024/06/14 17:31
首先我们要导入staemVR(此时我们已经连接好了THC)
并将测试场景拖入进行一系列的测试(其中的小黑快是定位器),检验设备是否能正常使用。
检验完毕之后我们需要再导入场景
并换成VR视角(失活上面的视角,使用下面的视角)
以上的操作就是实现VR游戏步骤。
调试完成之后我们就要开始进行框架构造和逻辑编写了,
之后我们就可以进行配置文件的存储和读取的编码,
如上图,我们将配置文件写在Resources/Config文件下,
读取逻辑写在Scripts下。
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System;
using System.Reflection;
//逻辑相同 类型不同 —- 泛型
//处理加载,缓存,获取配置的管理器
public class ConfigManager : Singleton {
private Dictionary<int, BossCfg> _bossCfgDic = new Dictionary<int, BossCfg>();private Dictionary<int, WeaponCfg> _weaponCfgDic = new Dictionary<int, WeaponCfg>();private Dictionary<int, ShieldCfg> _shieldCfgDic = new Dictionary<int, ShieldCfg>();public void LoadAllConfig(){ _bossCfgDic = LoadConfig<BossCfg>("BossConfig"); _weaponCfgDic = LoadConfig<WeaponCfg>("WeaponConfig"); _shieldCfgDic = LoadConfig<ShieldCfg>("ShieldConfig");}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/" + fileName); Debug.Log(bossCfgInfo.text); //新建xml文档对象 XmlDocument document = new XmlDocument(); //把字符串加载到对象中 document.LoadXml(bossCfgInfo.text); //获取根节点 XmlNode rootNode = document.SelectSingleNode("Root"); //获取根节点的子节点列表 XmlNodeList nodeList = rootNode.ChildNodes; //遍历 foreach (XmlNode node in nodeList) { //节点 转 元素类型 XmlElement element = node as XmlElement; 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 obj1 = 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);}//getpublic BossCfg GetBossCfgByID(int id){ if (_bossCfgDic.ContainsKey(id)) { return _bossCfgDic[id]; } return null;}
}
//映射对象 + 字典 = savepublic class BossCfg{ public int ID; public string Name; public int Hp; public string AttackType;}//武器数据映射的对象模板public class WeaponCfg{ public int ID; public string Name; public int EffectiveBulletCount; public int EffectiveTime; public int Hurt; public float AttackDistance; public string Display;}//盾牌数据映射的对象模板public class ShieldCfg{ public int ID; public string Name; public int EffectiveParryTime; public int EffectiveTime; public string Display;}
using UnityEngine;
using System.Collections;
public class Singleton where 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;
//游戏入口
public class VRGameManager : MonoBehaviour {
// Use this for initialization
void Start () {
ConfigManager.Instance.LoadAllConfig();
BossCfg dog = ConfigManager.Instance.GetBossCfgByID(1);
BossCfg bigdog = ConfigManager.Instance.GetBossCfgByID(5);
Debug.Log(1234);
}
// Update is called once per frame
void Update () {
}
}
- Unity_FPS游戏_Xml文件读取流程_01
- Unity_FPS游戏_Boss模块流程_01
- Unity_FPS游戏_Boss模块流程01脚本
- Unity_FPS游戏_Boss模块_03
- Unity_FPS游戏中设置子弹轨迹和红外线瞄准
- HDFS-文件读取流程
- HDFS文件读取流程
- Unity_标记语言_Xml文件
- Mybatis上路_01-基本流程
- GPU渲染流程学习_01
- 黑马程序员_xml读取写入方法
- nginx读取文件代码流程
- MP4文件sample读取流程
- MP4文件sample读取流程
- MP4文件sample读取流程
- hadoop读取文件流程分析
- 案例_xml文件实现学生管理
- C#_xml文件请求及接收数据
- 数据挖掘算法(二)--决策树
- 解决BMap is not defined?
- 区块链初识
- UNIX网络编程学习之一:socket的简介
- DAY2 模拟考试第三题 情书的代价
- Unity_FPS游戏_Xml文件读取流程_01
- Day07 set集合与map集合
- 针对多字段对单字段查询的解决方案
- Java任务超时处理
- 机器学习:L1与L2正则化项
- hbase GC时间过程导致进程挂掉问题
- C++之快速排序(3)---《那些奇怪的算法》
- vue入门
- DNS域传送漏洞