首先我们要导入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);
}
//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;
}
//武器数据映射的对象模板
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 () {
}
}