本文仅作笔记学习和分享,不用做任何商业用途
本文包括但不限于unity官方手册,unity唐老狮等教程知识,如有不足还请斧正
Unity数据持久化 之 一个通过2进制读取Excel并存储的轮子(3)-CSDN博客
这节就是真正的存储数据了
理清一下思路:
1.存储路径并检查
//2进制文件类存储
private static string Data_Binary_Path = Application.streamingAssetsPath + "/Binary/";
//路径检查
if (!Directory.Exists(Data_Binary_Path))
Directory.CreateDirectory(Data_Binary_Path);
Debug.Log(Data_Binary_Path);
2. 存入key所对应的变量名字
Q:为什么?
A:因为使用的是字典存储,所以就需要键值对,所以读的时候,就可以采用将key的变量名作为字典key这一方式进行存储
顺带,先存储一下行数
#region 存入主键的变量名
//先存入表的有效数据的行数,并且不需要表头那那些行数所以减去4
fs.Write(BitConverter.GetBytes(dataTable.Rows.Count - 4), 0, 4);
//再存入key所在的列的变量名
string keyName = GetVariableNameRow(dataTable)[GetKey(dataTable)].ToString();
Byte[] bytes = Encoding.UTF8.GetBytes(keyName);
//之后写入字符串,先长度
fs.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
fs.Write(bytes, 0, bytes.Length);//再内容
3.按行列循环存入已经转为2进制数组的数据
//取行存列,依次排开,按类型存储
DataRow row;
DataRow dataType =GetVariableDataTypeRow(dataTable);
for (int i = Begin_Index; i < dataTable.Rows.Count; i++)
{
row = dataTable.Rows[i];
for (int j = 0; j < dataTable.Columns.Count; j++)
{
switch (dataType[j].ToString())
{
case "int":
fs.Write(BitConverter.GetBytes(int.Parse(row[j].ToString())), 0, 4);
break;
case "string":
bytes =Encoding.UTF8.GetBytes((string)row[j].ToString());
//还是先写入长度,后写入内容
fs.Write(BitConverter.GetBytes(bytes.Length),0,4);
fs.Write(bytes,0,bytes.Length);
break;
case "bool":
fs.Write(BitConverter.GetBytes(bool.Parse(row[j].ToString())), 0, 1);
break;
case "float":
fs.Write(BitConverter.GetBytes(float.Parse(row[j].ToString())), 0, 4);
break;
}
}
#endregion
}
fs.Close();
}
}
4.总览
//生成二进制数据的方法-z
private static void GenerateExcelBinary(DataTable dataTable)
{
//路径检查
if (!Directory.Exists(Data_Binary_Path))
Directory.CreateDirectory(Data_Binary_Path);
Debug.Log(Data_Binary_Path);
//创建2进制文件存储
using (FileStream fs = new FileStream(Data_Binary_Path + dataTable.TableName + ".json", FileMode.OpenOrCreate, FileAccess.Write))
{
#region 存入主键的变量名
//先存入表的有效数据的行数,并且不需要表头那那些行数所以减去4
fs.Write(BitConverter.GetBytes(dataTable.Rows.Count - 4), 0, 4);
//再存入key所在的列的变量名
string keyName = GetVariableNameRow(dataTable)[GetKey(dataTable)].ToString();
Byte[] bytes = Encoding.UTF8.GetBytes(keyName);
//之后写入字符串,先长度
fs.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
fs.Write(bytes, 0, bytes.Length);//再内容
#endregion
#region 存入数据
//取行存列,依次排开,按类型存储
DataRow row;
DataRow dataType =GetVariableDataTypeRow(dataTable);
for (int i = Begin_Index; i < dataTable.Rows.Count; i++)
{
row = dataTable.Rows[i];
for (int j = 0; j < dataTable.Columns.Count; j++)
{
switch (dataType[j].ToString())
{
case "int":
fs.Write(BitConverter.GetBytes(int.Parse(row[j].ToString())), 0, 4);
break;
case "string":
bytes =Encoding.UTF8.GetBytes((string)row[j].ToString());
//还是先写入长度,后写入内容
fs.Write(BitConverter.GetBytes(bytes.Length),0,4);
fs.Write(bytes,0,bytes.Length);
break;
case "bool":
fs.Write(BitConverter.GetBytes(bool.Parse(row[j].ToString())), 0, 1);
break;
case "float":
fs.Write(BitConverter.GetBytes(float.Parse(row[j].ToString())), 0, 4);
break;
}
}
#endregion
}
fs.Close();
}
}