目录
一、前言:
许多配置文件的做法有脚本化物体,直接配置字典,或者自制工具进行转换等,这篇文章带来的是用Excel配置数据,转换为二进制文件,同时配对二进制读取工具方法。使用这种工具方法可以实现存档读档功能,也可以实现数据的便捷转换。二进制数据的好处是网络传输快,轻便,安全性较高。
二、实现
1.前提:
需要安装Excel的dll文件到Unity中,这是Unity读取Excel的插件。

2.ExcelDataToByteEditor
原理是依次读取Excel文件中的Table表数据,表名作为二进制文件名,然后对每个Table中读取其中的每一行数据,当然里面有一些提示作用的数据可以增加,具体就是从第几行开始读取实际的数据罢了

namespace Project.Editor
{
using System;
using UnityEngine;
using System.Data;
using System.IO;
using Excel;
using UnityEditor;
using System.Text;
public class ExcelDataToByteEditor:Editor
{
private const string settingPath="Assets/Editor/ExcelDataToByteEditor/Setting/PathSetting.asset";
[MenuItem("Tools/ExcelDataToByte")]
private static void ExcelDataToByte()
{
PathSetting pathSetting=(PathSetting)AssetDatabase.LoadAssetAtPath(settingPath,typeof(PathSetting));
if(pathSetting==null)
{
Debug.LogError("不存在PathSetting文件:"+settingPath);
return;
}
string excelDirPath=pathSetting.excelDirectionaryPath;
if(!Directory.Exists(excelDirPath))
{
Debug.LogError("不存在Excel文件夹路径:"+excelDirPath);
return;
}
string targetByteCreatePath=pathSetting.targetByteCreatePath;
if(!Directory.Exists(targetByteCreatePath))
{
Debug.LogError("不存在Byte文件生成文件夹:"+targetByteCreatePath);
return;
}
string[] excelPaths=Directory.GetFiles(excelDirPath);
int totalData=0;
foreach(string path in excelPaths)
{
string pathExtension=Path.GetExtension(path);
if (pathExtension != ".xls" && pathExtension != ".xlsx")
continue;
using(FileStream fs=File.Open(path,FileMode.Open,FileAccess.Read))
{
IExcelDataReader excelDataReader=ExcelReaderFactory.CreateOpenXmlReader(fs);
DataSet result=excelDataReader.AsDataSet();
for(int i=0;i<result.Tables.Count;i++)
{
if(!pathSetting.useFirstExcelTable&&i==0)
continue;
DataTable table=result.Tables[i];
int rowCount=table.Rows.Count;//行数
int columnCount=table.Columns.Count;//列数
string bitFilePath = targetByteCreatePath+"/"+table.TableName+".nai";//生成二进制文件的路径
using(FileStream bitFS=File.Open(bitFilePath,FileMode.CreateNew,FileAccess.Write))
{
bitFS.Seek(0,SeekOrigin.Begin);
bitFS.SetLength(0);
//获取变量类型行数据
DataRow typeRow = table.Rows[pathSetting.propertyTypeStartInExcel-1];
//存储实际数据的个数
int dataRowCount =rowCount-pathSetting.dataStartInExcel+1;
bitFS.Write(BitConverter.GetBytes(dataRowCount),0,4);
for(int row=pathSetting.dataStartInExcel-1;row<table.Rows.Count;row++)
{
//从实际数据行开始进行数据存储
DataRow dataRow=table.Rows[row];
for(int column=0;column<table.Columns.Count;column++)
{
switch(typeRow[column].ToString())
{
case "int":
bitFS.Write(BitConverter.GetBytes(int.Parse(dataRow[column].ToString())),0,4);
break;
case "float":
bitFS.Write(BitConverter.GetBytes(float.Parse(dataRow[column].ToString())),0,4);
break;
case "bool":
bitFS.Write(BitConverter.GetBytes(bool.Parse(dataRow[column].ToString())),0,1);
break;
case "string":
string str = dataRow[column].ToString();
var bytes = Encoding.UTF8.GetBytes(str);
//先把字符串的字节数组的长度写入
int lengh = bytes.Length;
bitFS.Write(BitConverter.GetBytes(lengh), 0, 4);
//然后把字符串写入
bitFS.Write(bytes, 0, lengh);
break;
case "double":
bitFS.Write(BitConverter.GetBytes(double.Parse(dataRow[column].ToString())),0,8);
break;
case "long":
bitFS.Write(BitConverter.GetBytes(long.Parse(dataRow[column].ToString())),0,8);
break;
case "short":
bitFS.Write(BitConverter.GetBytes(short.Parse(dataRow[column].ToString())),0,2);
break;
case "int[]":
string[] intValues = dataRow[column].ToString().Split(",",StringSplitOptions.RemoveEmptyEntries);
int count = intValues.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(string value in intValues)
bitFS.Write(BitConverter.GetBytes(int.Parse(value)),0,4);
break;
case "float[]":
string[] floatValues = dataRow[column].ToString().Split(",",StringSplitOptions.RemoveEmptyEntries);
int floatCount = floatValues.Length;
bitFS.Write(BitConverter.GetBytes(floatCount), 0, 4);
foreach(string value in floatValues)
bitFS.Write(BitConverter.GetBytes(float.Parse(value)),0,4);
break;
case "string[]":
string strValue = dataRow[column].ToString();
string[] strValues=strValue.Split(",",StringSplitOptions.RemoveEmptyEntries);
bitFS.Write(BitConverter.GetBytes(strValues.Length), 0, 4);
for(int strIndex=0;strIndex<strValues.Length;strIndex++)
{
var stebytes = Encoding.UTF8.GetBytes(strValues[strIndex]);
int strlengh = stebytes.Length;
bitFS.Write(BitConverter.GetBytes(strlengh), 0, 4);
bitFS.Write(stebytes, 0, strlengh);
}
break;
case "bool[]":
string[] boolValues = dataRow[column].ToString().Split(",",StringSplitOptions.RemoveEmptyEntries);
int boolCount = boolValues.Length;
bitFS.Write(BitConverter.GetBytes(boolCount), 0, 4);
foreach(string value in boolValues)
bitFS.Write(BitConverter.GetBytes(bool.Parse(value)),0,1);
break;
case "double[]":
string[] doubleValues = dataRow[column].ToString().Split(",",StringSplitOptions.RemoveEmptyEntries);
int doubleCount = doubleValues.Length;
bitFS.Write(BitConverter.GetBytes(doubleCount), 0, 4);
foreach(string value in doubleValues)
bitFS.Write(BitConverter.GetBytes(double.Parse(value)),0,8);
break;
case "long[]":
string[] longValues = dataRow[column].ToString().Split(",",StringSplitOptions.RemoveEmptyEntries);
int longCount = longValues.Length;
bitFS.Write(BitConverter.GetBytes(longCount), 0, 4);
foreach(string value in longValues)
bitFS.Write(BitConverter.GetBytes(long.Parse(value)),0,8);
break;
case "short[]":
string[] shortValues = dataRow[column].ToString().Split(",",StringSplitOptions.RemoveEmptyEntries);
int shortCount = shortValues.Length;
bitFS.Write(BitConverter.GetBytes(shortCount), 0, 4);
foreach(string value in shortValues)
bitFS.Write(BitConverter.GetBytes(short.Parse(value)),0,2);
break;
}
}
}
bitFS.Close();
totalData++;
}
}
fs.Close();
}
}
Debug.Log($"成功将Excel表转换为Byte数据,共计生成数据表{totalData}个");
AssetDatabase.Refresh();
}
}
}
3.PathSetting
脚本化物体,存放读取设置的全局数据,可以进行配置和扩展
namespace Project.Editor
{
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[CreateAssetMenu(fileName = "PathSetting", menuName = "ExcelDataToByte_SettingAsset", order = 0)]
public class PathSetting : ScriptableObject
{
[ReadOnly]public string excelDirectionaryPath;
[ReadOnly]public string targetByteCreatePath;
public int propertyTypeStartInExcel=3;
public int dataStartInExcel=4;
public bool useFirstExcelTable=false;
}
#if UNITY_EDITOR
[CustomEditor(typeof(PathSetting))]
public class PathSettingEditor:Editor
{
SerializedProperty excelDirectionaryPath;
SerializedProperty targetByteCreatePath;
SerializedProperty propertyTypeStartInExcel;
SerializedProperty dataStartInExcel;
SerializedProperty useFirstExcelTable;
void OnEnable()
{
excelDirectionaryPath=serializedObject.FindProperty("excelDirectionaryPath");
targetByteCreatePath=serializedObject.FindProperty("targetByteCreatePath");
propertyTypeStartInExcel=serializedObject.FindProperty("propertyTypeStartInExcel");
dataStartInExcel=serializedObject.FindProperty("dataStartInExcel");
useFirstExcelTable=serializedObject.FindProperty("useFirstExcelTable");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
GUILayout.BeginVertical();
excelDirectionaryPath.stringValue = Application.dataPath+"/Editor/ExcelDataToByteEditor/Excels";
EditorGUILayout.PropertyField(excelDirectionaryPath,new GUIContent("Excel文件夹路径"));
GUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(targetByteCreatePath,new GUIContent("二进制文件夹路径"));
if (GUILayout.Button("浏览", GUILayout.Width(50f)))
{
targetByteCreatePath.stringValue = EditorUtility.OpenFolderPanel("选择路径", Application.dataPath, "");
}
GUILayout.EndHorizontal();
EditorGUILayout.PropertyField(propertyTypeStartInExcel,new GUIContent("数据变量类型在Excel中的行"));
EditorGUILayout.PropertyField(dataStartInExcel,new GUIContent("第一个实际数据在Excel中的行"));
EditorGUILayout.PropertyField(useFirstExcelTable,new GUIContent("Excel第一张表参与数据生成"));
GUILayout.EndVertical();
serializedObject.ApplyModifiedProperties();
}
}
public class ReadOnlyAttribute : PropertyAttribute
{
}
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property,GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position,SerializedProperty property,GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
#endif
}
4.ByteTool
二进制工具,包含Excel表导出的二进制文件数据读取,可转换为列表,字典。同时此工具还包含了游戏内自定义数据的读写,当然只能对基础类型进行读写,有需要的可以自己扩展
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEngine;
/// <summary>
/// 二进制文件读取以及转换数据类
/// </summary>
public class ByteTool
{
/// <summary>
/// 将Excel转化的二进制数据转换为指定数据类型的列表形式
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="byteFilePath">二进制文件路径</param>
/// <returns></returns>
public static List<T> ReadExcelByteAsList<T>(string byteFilePath)
{
if(!File.Exists(byteFilePath))
return null;
if(Path.GetExtension(byteFilePath)!=".nai")
return null;
List<T> finalvalues=new List<T>();
Byte[] bytes;
using(FileStream fs=File.Open(byteFilePath,FileMode.Open,FileAccess.Read))
{
bytes=new byte[fs.Length];
fs.Read(bytes,0,bytes.Length);
fs.Close();
}
//当前读取的位数标记
int index=0;
//读取行数数据,共计4个位
int rowCount = BitConverter.ToInt32(bytes,index);
index+=4;
Type dataStructType=typeof(T);
FieldInfo[] fieldInfos = dataStructType.GetFields();
//读取实际数据
for(int row=0;row<rowCount;row++)
{
object data=Activator.CreateInstance(dataStructType);
for (int i = 0; i < fieldInfos.Length; i++)
{
if(index>=bytes.Length)
break;
Type fieldType = fieldInfos[i].FieldType;
if(fieldType==typeof(int))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt32(bytes,index));
index+=4;
}
else if(fieldType==typeof(float))
{
fieldInfos[i].SetValue(data,BitConverter.ToSingle(bytes,index));
index+=4;
}
else if(fieldType==typeof(bool))
{
fieldInfos[i].SetValue(data,BitConverter.ToBoolean(bytes,index));
index+=1;
}
else if(fieldType==typeof(string))
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
if (strLengh < 0 || index + strLengh > bytes.Length) break;
string str = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
fieldInfos[i].SetValue(data, str);
index += strLengh;
}
else if(fieldType==typeof(double))
{
fieldInfos[i].SetValue(data,BitConverter.ToDouble(bytes,index));
index+=8;
}
else if(fieldType==typeof(long))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt64(bytes,index));
index+=8;
}
else if(fieldType==typeof(short))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt16(bytes,index));
index+=2;
}
else if(fieldType==typeof(int[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
int[] values=new int[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(float[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
float[] values=new float[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(string[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
string[] values=new string[valueLengh];
for(int j=0;j<valueLengh;j++)
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
values[j] = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
index+=strLengh;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(bool[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
bool[] values=new bool[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToBoolean(bytes, index);
index+=1;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(double[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
double[] values=new double[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToDouble(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(long[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
long[] values=new long[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt64(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(short[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
short[] values=new short[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt16(bytes, index);
index+=2;
}
fieldInfos[i].SetValue(data,values);
}
}
T finalData = (T)data;
finalvalues.Add(finalData);
}
return finalvalues;
}
/// <summary>
/// 将Excel转换的二进制数据转换为指定数据类型的字典形式,如果给与的key下标与字典的key类型不一致则会返回null,value始终为V类型
/// </summary>
/// <typeparam name="K">键值类型</typeparam>
/// <typeparam name="V">值类型</typeparam>
/// <param name="byteFilePath">二进制文件路径</param>
/// <param name="key">键值</param>
/// <returns></returns>
public static Dictionary<K,V> ReadExcelByteAsDictionary<K,V>(string byteFilePath,int key=0)
{
if(!File.Exists(byteFilePath))
return null;
if(Path.GetExtension(byteFilePath)!=".nai")
return null;
Type dataStructType=typeof(V);
FieldInfo[] fieldInfos = dataStructType.GetFields();
if(fieldInfos[key].FieldType!=typeof(K))
return null;
Dictionary<K,V> dir=new Dictionary<K, V>();
Byte[] bytes;
using(FileStream fs=File.Open(byteFilePath,FileMode.Open,FileAccess.Read))
{
bytes=new byte[fs.Length];
fs.Read(bytes,0,bytes.Length);
fs.Close();
}
//当前读取的位数标记
int index=0;
//读取行数数据,共计4个位
int rowCount = BitConverter.ToInt32(bytes,index);
index+=4;
//读取实际数据
for(int row=0;row<rowCount;row++)
{
object data=Activator.CreateInstance(dataStructType);
for (int i = 0; i < fieldInfos.Length; i++)
{
if(index>=bytes.Length)
break;
Type fieldType = fieldInfos[i].FieldType;
if(fieldType==typeof(int))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt32(bytes,index));
index+=4;
}
else if(fieldType==typeof(float))
{
fieldInfos[i].SetValue(data,BitConverter.ToSingle(bytes,index));
index+=4;
}
else if(fieldType==typeof(bool))
{
fieldInfos[i].SetValue(data,BitConverter.ToBoolean(bytes,index));
index+=1;
}
else if(fieldType==typeof(string))
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
if (strLengh < 0 || index + strLengh > bytes.Length) break;
string str = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
fieldInfos[i].SetValue(data, str);
index += strLengh;
}
else if(fieldType==typeof(double))
{
fieldInfos[i].SetValue(data,BitConverter.ToDouble(bytes,index));
index+=8;
}
else if(fieldType==typeof(long))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt64(bytes,index));
index+=8;
}
else if(fieldType==typeof(short))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt16(bytes,index));
index+=2;
}
else if(fieldType==typeof(int[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
int[] values=new int[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(float[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
float[] values=new float[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(string[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
string[] values=new string[valueLengh];
for(int j=0;j<valueLengh;j++)
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
values[j] = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
index+=strLengh;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(bool[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
bool[] values=new bool[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToBoolean(bytes, index);
index+=1;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(double[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
double[] values=new double[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToDouble(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(long[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
long[] values=new long[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt64(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(short[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
short[] values=new short[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt16(bytes, index);
index+=2;
}
fieldInfos[i].SetValue(data,values);
}
}
V finalData = (V)data;
K dirkey=(K)fieldInfos[key].GetValue(data);
if(!dir.ContainsKey(dirkey))
dir.Add(dirkey,finalData);
else
dir[dirkey]=finalData;
}
return dir;
}
/// <summary>
/// 将Excel转换的二进制数据转换为指定数据类型的字典形式
/// </summary>
/// <typeparam name="K">键值类型</typeparam>
/// <typeparam name="V">值类型<</typeparam>
/// <param name="byteFilePath">二进制文件路径</param>
/// <param name="keys">键值数组</param>
/// <returns></returns>
public static Dictionary<K,V> ReadExcelByteAsDictionary<K,V>(string byteFilePath,K[] keys)
{
if(!File.Exists(byteFilePath))
return null;
if(Path.GetExtension(byteFilePath)!=".nai")
return null;
if(keys==null)
return null;
Type dataStructType=typeof(V);
FieldInfo[] fieldInfos = dataStructType.GetFields();
Dictionary<K,V> dir=new Dictionary<K, V>();
foreach(K key in keys)
{
dir.Add(key,default);
}
Byte[] bytes;
using(FileStream fs=File.Open(byteFilePath,FileMode.Open,FileAccess.Read))
{
bytes=new byte[fs.Length];
fs.Read(bytes,0,bytes.Length);
fs.Close();
}
//当前读取的位数标记
int index=0;
//读取行数数据,共计4个位
int rowCount = BitConverter.ToInt32(bytes,index);
index+=4;
int indexKey=0;
//读取实际数据
for(int row=0;row<rowCount;row++)
{
if(indexKey>=keys.Length)
break;
object data=Activator.CreateInstance(dataStructType);
for (int i = 0; i < fieldInfos.Length; i++)
{
if(index>=bytes.Length)
break;
Type fieldType = fieldInfos[i].FieldType;
if(fieldType==typeof(int))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt32(bytes,index));
index+=4;
}
else if(fieldType==typeof(float))
{
fieldInfos[i].SetValue(data,BitConverter.ToSingle(bytes,index));
index+=4;
}
else if(fieldType==typeof(bool))
{
fieldInfos[i].SetValue(data,BitConverter.ToBoolean(bytes,index));
index+=1;
}
else if(fieldType==typeof(string))
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
if (strLengh < 0 || index + strLengh > bytes.Length) break;
string str = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
fieldInfos[i].SetValue(data, str);
index += strLengh;
}
else if(fieldType==typeof(double))
{
fieldInfos[i].SetValue(data,BitConverter.ToDouble(bytes,index));
index+=8;
}
else if(fieldType==typeof(long))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt64(bytes,index));
index+=8;
}
else if(fieldType==typeof(short))
{
fieldInfos[i].SetValue(data,BitConverter.ToInt16(bytes,index));
index+=2;
}
else if(fieldType==typeof(int[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
int[] values=new int[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(float[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
float[] values=new float[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(string[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
string[] values=new string[valueLengh];
for(int j=0;j<valueLengh;j++)
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
values[j] = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
index+=strLengh;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(bool[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
bool[] values=new bool[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToBoolean(bytes, index);
index+=1;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(double[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
double[] values=new double[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToDouble(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(long[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
long[] values=new long[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt64(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(short[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
short[] values=new short[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt16(bytes, index);
index+=2;
}
fieldInfos[i].SetValue(data,values);
}
}
V finalData = (V)data;
K dirkey=keys[indexKey];
if(!dir.ContainsKey(dirkey))
dir.Add(dirkey,finalData);
else
dir[dirkey]=finalData;
indexKey++;
}
return dir;
}
/// <summary>
/// 将一个类型的数据写入到指定文件路径中
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="byteFilePath">二进制文件路径</param>
/// <param name="data">实际数据</param>
public static void WriteDataInByteFile<T>(string byteFilePath,T data)
{
if(Path.GetExtension(byteFilePath)!=".nai")
return;
Type dataStructType=typeof(T);
using(FileStream bitFS=File.Open(byteFilePath,FileMode.OpenOrCreate,FileAccess.Write))
{
bitFS.Seek(0,SeekOrigin.End);
if(dataStructType==typeof(int))
{
object fieldValue = (object)data;
bitFS.Write(BitConverter.GetBytes((int)fieldValue),0,4);
}
else if(dataStructType==typeof(float))
{
object fieldValue = (object)data;
bitFS.Write(BitConverter.GetBytes((float)fieldValue),0,4);
}
else if(dataStructType==typeof(bool))
{
object fieldValue = (object)data;
bitFS.Write(BitConverter.GetBytes((bool)fieldValue),0,1);
}
else if(dataStructType==typeof(string))
{
object fieldValue = (object)data;
string str = (string)fieldValue;
var bytes = Encoding.UTF8.GetBytes(str);
//先把字符串的字节数组的长度写入
int lengh = bytes.Length;
bitFS.Write(BitConverter.GetBytes(lengh), 0, 4);
//然后把字符串写入
bitFS.Write(bytes, 0, lengh);
}
else if(dataStructType==typeof(short))
{
object fieldValue = (object)data;
bitFS.Write(BitConverter.GetBytes((short)fieldValue),0,2);
}
else if(dataStructType==typeof(long))
{
object fieldValue = (object)data;
bitFS.Write(BitConverter.GetBytes((long)fieldValue),0,8);
}
else if(dataStructType==typeof(double))
{
object fieldValue = (object)data;
bitFS.Write(BitConverter.GetBytes((double)fieldValue),0,8);
}
else if(dataStructType==typeof(int[]))
{
object fieldValue = (object)data;
int[] intValues=(int[])fieldValue;
int count = intValues.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(int value in intValues)
bitFS.Write(BitConverter.GetBytes(value),0,4);
}
else if(dataStructType==typeof(float[]))
{
object fieldValue = (object)data;
float[] intValues=(float[])fieldValue;
int count = intValues.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(float value in intValues)
bitFS.Write(BitConverter.GetBytes(value),0,4);
}
else if(dataStructType==typeof(string[]))
{
object fieldValue = (object)data;
string[] strValues=(string[])fieldValue;
bitFS.Write(BitConverter.GetBytes(strValues.Length), 0, 4);
for(int strIndex=0;strIndex<strValues.Length;strIndex++)
{
var stebytes = Encoding.UTF8.GetBytes(strValues[strIndex]);
int strlengh = stebytes.Length;
bitFS.Write(BitConverter.GetBytes(strlengh), 0, 4);
bitFS.Write(stebytes, 0, strlengh);
}
}
else if(dataStructType==typeof(bool[]))
{
object fieldValue = (object)data;
bool[] Values=(bool[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(bool value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,1);
}
else if(dataStructType==typeof(long[]))
{
object fieldValue = (object)data;
long[] Values=(long[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(long value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,8);
}
else if(dataStructType==typeof(short[]))
{
object fieldValue = (object)data;
short[] Values=(short[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(short value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,2);
}
else if(dataStructType==typeof(double[]))
{
object fieldValue = (object)data;
double[] Values=(double[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(double value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,8);
}
else if(dataStructType==typeof(Vector2))
{
object fieldValue = (object)data;
Vector2 Values=(Vector2)fieldValue;
bitFS.Write(BitConverter.GetBytes(Values.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.y), 0, 4);
}
else if(dataStructType==typeof(Vector3))
{
object fieldValue = (object)data;
Vector3 Values=(Vector3)fieldValue;
bitFS.Write(BitConverter.GetBytes(Values.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.z), 0, 4);
}
else if(dataStructType==typeof(Vector4))
{
object fieldValue = (object)data;
Vector4 Values=(Vector4)fieldValue;
bitFS.Write(BitConverter.GetBytes(Values.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.z), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.w), 0, 4);
}
else if(dataStructType==typeof(Vector2[]))
{
object fieldValue = (object)data;
Vector2[] Values=(Vector2[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(Vector2 mvalue in Values)
{
bitFS.Write(BitConverter.GetBytes(mvalue.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.y), 0, 4);
}
}
else if(dataStructType==typeof(Vector3[]))
{
object fieldValue = (object)data;
Vector3[] Values=(Vector3[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(Vector3 mvalue in Values)
{
bitFS.Write(BitConverter.GetBytes(mvalue.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.z), 0, 4);
}
}
else if(dataStructType==typeof(Vector4[]))
{
object fieldValue = (object)data;
Vector4[] Values=(Vector4[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(Vector4 mvalue in Values)
{
bitFS.Write(BitConverter.GetBytes(mvalue.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.z), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.w), 0, 4);
}
}
else
{
FieldInfo[] fieldInfos = dataStructType.GetFields();
for(int i=0;i<fieldInfos.Length;i++)
{
Type fieldType = fieldInfos[i].FieldType;
object fieldValue = fieldInfos[i].GetValue(data);
if (fieldValue == null)
continue;
if(fieldType==typeof(int))
{
bitFS.Write(BitConverter.GetBytes((int)fieldValue),0,4);
}
else if(fieldType==typeof(float))
{
bitFS.Write(BitConverter.GetBytes((float)fieldValue),0,4);
}
else if(fieldType==typeof(bool))
{
bitFS.Write(BitConverter.GetBytes((bool)fieldValue),0,1);
}
else if(fieldType==typeof(string))
{
string str = (string)fieldValue;
var bytes = Encoding.UTF8.GetBytes(str);
//先把字符串的字节数组的长度写入
int lengh = bytes.Length;
bitFS.Write(BitConverter.GetBytes(lengh), 0, 4);
//然后把字符串写入
bitFS.Write(bytes, 0, lengh);
}
else if(fieldType==typeof(short))
{
bitFS.Write(BitConverter.GetBytes((short)fieldValue),0,2);
}
else if(fieldType==typeof(long))
{
bitFS.Write(BitConverter.GetBytes((long)fieldValue),0,8);
}
else if(fieldType==typeof(double))
{
bitFS.Write(BitConverter.GetBytes((double)fieldValue),0,8);
}
else if(fieldType==typeof(int[]))
{
int[] intValues=(int[])fieldValue;
int count = intValues.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(int value in intValues)
bitFS.Write(BitConverter.GetBytes(value),0,4);
}
else if(fieldType==typeof(float[]))
{
float[] intValues=(float[])fieldValue;
int count = intValues.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(float value in intValues)
bitFS.Write(BitConverter.GetBytes(value),0,4);
}
else if(fieldType==typeof(string[]))
{
string[] strValues=(string[])fieldValue;
bitFS.Write(BitConverter.GetBytes(strValues.Length), 0, 4);
for(int strIndex=0;strIndex<strValues.Length;strIndex++)
{
var stebytes = Encoding.UTF8.GetBytes(strValues[strIndex]);
int strlengh = stebytes.Length;
bitFS.Write(BitConverter.GetBytes(strlengh), 0, 4);
bitFS.Write(stebytes, 0, strlengh);
}
}
else if(fieldType==typeof(bool[]))
{
bool[] Values=(bool[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(bool value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,1);
}
else if(fieldType==typeof(long[]))
{
long[] Values=(long[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(long value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,8);
}
else if(fieldType==typeof(short[]))
{
short[] Values=(short[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(short value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,2);
}
else if(fieldType==typeof(double[]))
{
double[] Values=(double[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(double value in Values)
bitFS.Write(BitConverter.GetBytes(value),0,8);
}
else if(fieldType==typeof(Vector2))
{
Vector2 Values=(Vector2)fieldValue;
bitFS.Write(BitConverter.GetBytes(Values.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.y), 0, 4);
}
else if(fieldType==typeof(Vector3))
{
Vector3 Values=(Vector3)fieldValue;
bitFS.Write(BitConverter.GetBytes(Values.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.z), 0, 4);
}
else if(fieldType==typeof(Vector4))
{
Vector4 Values=(Vector4)fieldValue;
bitFS.Write(BitConverter.GetBytes(Values.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.z), 0, 4);
bitFS.Write(BitConverter.GetBytes(Values.w), 0, 4);
}
else if(fieldType==typeof(Vector2[]))
{
Vector2[] Values=(Vector2[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(Vector2 mvalue in Values)
{
bitFS.Write(BitConverter.GetBytes(mvalue.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.y), 0, 4);
}
}
else if(fieldType==typeof(Vector3[]))
{
Vector3[] Values=(Vector3[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(Vector3 mvalue in Values)
{
bitFS.Write(BitConverter.GetBytes(mvalue.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.z), 0, 4);
}
}
else if(fieldType==typeof(Vector4[]))
{
Vector4[] Values=(Vector4[])fieldValue;
int count = Values.Length;
bitFS.Write(BitConverter.GetBytes(count), 0, 4);
foreach(Vector4 mvalue in Values)
{
bitFS.Write(BitConverter.GetBytes(mvalue.x), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.y), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.z), 0, 4);
bitFS.Write(BitConverter.GetBytes(mvalue.w), 0, 4);
}
}
}
}
bitFS.Close();
}
}
/// <summary>
/// 从二进制文件中读取指定数据结构,需要指定读取位置,确保数据中与数据结构数据一一对应
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="byteFilePath">二进制文件路径</param>
/// <param name="byteIndex">实参,用于标记当前读取的字节下表</param>
/// <param name="offset">二进制文件中读取的其实位置</param>
/// <param name="length">读取长度,0默认表示一直读取到数据长度结束</param>
/// <returns></returns>
public static T ReadDataInByteFile<T>(string byteFilePath, ref int byteIndex,int offset=0,int length=0)
{
Type dataStructType=typeof(T);
object data;
if(!File.Exists(byteFilePath))
data=null;
else if(Path.GetExtension(byteFilePath)!=".nai")
data=null;
else
{
Byte[] bytes;
using(FileStream fs=File.Open(byteFilePath,FileMode.Open,FileAccess.Read))
{
bytes=new byte[fs.Length];
fs.Read(bytes,0,bytes.Length);
fs.Close();
}
int index=offset;
if(dataStructType==typeof(int))
{
int value=BitConverter.ToInt32(bytes,index);
data=value;
index+=4;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(float))
{
float value=BitConverter.ToSingle(bytes,index);
data=value;
index+=4;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(string))
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
string value = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
data=value;
index += strLengh;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(double))
{
double value=BitConverter.ToDouble(bytes,index);
data=value;
index+=8;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(long))
{
long value=BitConverter.ToInt64(bytes,index);
data=value;
index+=8;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(bool))
{
bool value=BitConverter.ToBoolean(bytes,index);
data=value;
index+=1;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(short))
{
short value=BitConverter.ToInt16(bytes,index);
data=value;
index+=2;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(int[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
int[] values=new int[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(float[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
float[] values=new float[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToSingle(bytes, index);
index+=4;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(string[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
string[] values=new string[valueLengh];
for(int j=0;j<valueLengh;j++)
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
values[j] = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
index+=strLengh;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(bool[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
bool[] values=new bool[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToBoolean(bytes, index);
index+=1;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(double[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
double[] values=new double[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToDouble(bytes, index);
index+=8;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(long[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
long[] values=new long[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt64(bytes, index);
index+=8;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(short[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
short[] values=new short[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt16(bytes, index);
index+=2;
}
data=values;
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(Vector2))
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
data=new Vector2(valueX,valueY);
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(Vector3))
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
data=new Vector3(valueX,valueY,valueZ);
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(Vector4))
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
float valueW=BitConverter.ToSingle(bytes,index);
index+=4;
data=new Vector4(valueX,valueY,valueZ,valueW);
byteIndex=index;
return (T)data;
}
else if(dataStructType==typeof(Vector2[]))
{
int valueLengh = BitConverter.ToInt32(bytes,index);
index+=4;
Vector2[] values=new Vector2[valueLengh];
for(int j=0;j<valueLengh;j++)
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
values[j]=new Vector2(valueX,valueY);
}
byteIndex=index;
data=values;
return (T)data;
}
else if(dataStructType==typeof(Vector3[]))
{
int valueLengh = BitConverter.ToInt32(bytes,index);
index+=4;
Vector3[] values=new Vector3[valueLengh];
for(int j=0;j<valueLengh;j++)
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
values[j]=new Vector3(valueX,valueY,valueZ);
}
byteIndex=index;
data=values;
return (T)data;
}
else if(dataStructType==typeof(Vector4[]))
{
int valueLengh = BitConverter.ToInt32(bytes,index);
index+=4;
Vector4[] values=new Vector4[valueLengh];
for(int j=0;j<valueLengh;j++)
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
float valueW=BitConverter.ToSingle(bytes,index);
index+=4;
values[j]=new Vector4(valueX,valueY,valueZ,valueW);
}
byteIndex=index;
data=values;
return (T)data;
}
data=Activator.CreateInstance(typeof(T));
FieldInfo[] fieldInfos = dataStructType.GetFields();
for(int i=0;i<fieldInfos.Length;i++)
{
if(length>0&&index>=length)
break;
Type fieldType=fieldInfos[i].FieldType;
if(fieldType==typeof(int))
{
int value=BitConverter.ToInt32(bytes,index);
fieldInfos[i].SetValue(data,value);
index+=4;
}
else if(fieldType==typeof(float))
{
float value=BitConverter.ToSingle(bytes,index);
fieldInfos[i].SetValue(data,value);
index+=4;
}
else if(fieldType==typeof(string))
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
if (strLengh < 0 || index + strLengh > bytes.Length) break;
string str = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
fieldInfos[i].SetValue(data, str);
index += strLengh;
}
else if(fieldType==typeof(double))
{
double value=BitConverter.ToDouble(bytes,index);
fieldInfos[i].SetValue(data,value);
index+=8;
}
else if(fieldType==typeof(long))
{
long value=BitConverter.ToInt64(bytes,index);
fieldInfos[i].SetValue(data,value);
index+=8;
}
else if(fieldType==typeof(bool))
{
bool value=BitConverter.ToBoolean(bytes,index);
fieldInfos[i].SetValue(data,value);
index+=1;
}
else if(fieldType==typeof(short))
{
short value=BitConverter.ToInt16(bytes,index);
fieldInfos[i].SetValue(data,value);
index+=2;
}
else if(fieldType==typeof(int[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
int[] values=new int[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt32(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(float[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
float[] values=new float[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToSingle(bytes, index);
index+=4;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(string[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
string[] values=new string[valueLengh];
for(int j=0;j<valueLengh;j++)
{
int strLengh = BitConverter.ToInt32(bytes, index);
index += 4;
values[j] = System.Text.Encoding.UTF8.GetString(bytes, index, strLengh);
index+=strLengh;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(bool[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
bool[] values=new bool[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToBoolean(bytes, index);
index+=1;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(double[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
double[] values=new double[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToDouble(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(long[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
long[] values=new long[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt64(bytes, index);
index+=8;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(short[]))
{
int valueLengh = BitConverter.ToInt32(bytes, index);
index+=4;
short[] values=new short[valueLengh];
for(int j=0;j<valueLengh;j++)
{
values[j]=BitConverter.ToInt16(bytes, index);
index+=2;
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(Vector2))
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
Vector2 value=new Vector2(valueX,valueY);
fieldInfos[i].SetValue(data,value);
}
else if(fieldType==typeof(Vector3))
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
Vector3 value=new Vector3(valueX,valueY,valueZ);
fieldInfos[i].SetValue(data,value);
}
else if(fieldType==typeof(Vector4))
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
float valueW=BitConverter.ToSingle(bytes,index);
index+=4;
Vector4 value=new Vector4(valueX,valueY,valueZ,valueW);
fieldInfos[i].SetValue(data,value);
}
else if(fieldType==typeof(Vector2[]))
{
int valueLengh = BitConverter.ToInt32(bytes,index);
index+=4;
Vector2[] values=new Vector2[valueLengh];
for(int j=0;j<valueLengh;j++)
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
values[j]=new Vector2(valueX,valueY);
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(Vector3[]))
{
int valueLengh = BitConverter.ToInt32(bytes,index);
index+=4;
Vector3[] values=new Vector3[valueLengh];
for(int j=0;j<valueLengh;j++)
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
values[j]=new Vector3(valueX,valueY,valueZ);
}
fieldInfos[i].SetValue(data,values);
}
else if(fieldType==typeof(Vector4[]))
{
int valueLengh = BitConverter.ToInt32(bytes,index);
index+=4;
Vector4[] values=new Vector4[valueLengh];
for(int j=0;j<valueLengh;j++)
{
float valueX=BitConverter.ToSingle(bytes,index);
index+=4;
float valueY=BitConverter.ToSingle(bytes,index);
index+=4;
float valueZ=BitConverter.ToSingle(bytes,index);
index+=4;
float valueW=BitConverter.ToSingle(bytes,index);
index+=4;
values[j]=new Vector4(valueX,valueY,valueZ,valueW);
}
fieldInfos[i].SetValue(data,values);
}
}
byteIndex=index;
}
return (T)data;
}
}