Unity之使用csv文件
2016-04-11 08:28
579 查看
我们在做游戏时,可以通过在Excel/Number里将数据导出成csv文件,然后在程序里动态读取。
在Unity中可以把csv放到Resources目录或StreamingAsset目录里,然后以文本的方式加载.
网上的csv解析大多没有处理引号,逗号等特殊符号,要是我们在数据配置中还存有json,就需要用下面的代码
using System.Collections;
using System.Collections.Generic;
public class CSV {
public static List<List<string>> Decode(string text)
{
if (text == null)
return null;
List<List<string>> result = new List<List<string>>();
List<string> line = new List<string>();
string field = "";
bool isInQuotation = false;//字符串模式
bool isInField = true;//是否在读取Field,用来表示空Field
int i = 0;
while (i < text.Length)
{
char ch = text[i];
if (isInQuotation)
{
if (ch == '"')
{
if (i < text.Length - 1 && text[i + 1] == '"')//重复"只算一个,切不结束字符串模式
{
field += '"';
i++;
}
else
{
isInQuotation = false;
}
}
else
{
field += ch;//字符串模式中所有字符都要加入
}
}
else
{
switch (ch)
{
case ',':
line.Add(field);
field = "";
isInField = true;
break;
case '"':
if (isInField)
isInQuotation = true;//进入字符串模式
else
field += ch;
break;
case '\r':
if (field.Length > 0 || isInField)
{
line.Add(field);
field = "";
}
result.Add(line);
line =new List<string>();
isInField = true;//下一行首先应该是数据
if (i < text.Length - 1 && text[i + 1] == '\n')//跳过\r\n
i++;
break;
default:
isInField = false;
field += ch;
break;
}
}
i++;
}
//收尾工作
if (field.Length > 0 || isInField && line.Count > 0)//如果是isInField标记的单元格,则要保证这行有其他数据,否则单独一个空单元格的行是没有意义的
line.Add(field);
if (line.Count > 0)
result.Add(line);
return result;
}
}
在Unity中可以把csv放到Resources目录或StreamingAsset目录里,然后以文本的方式加载.
网上的csv解析大多没有处理引号,逗号等特殊符号,要是我们在数据配置中还存有json,就需要用下面的代码
using System.Collections;
using System.Collections.Generic;
public class CSV {
public static List<List<string>> Decode(string text)
{
if (text == null)
return null;
List<List<string>> result = new List<List<string>>();
List<string> line = new List<string>();
string field = "";
bool isInQuotation = false;//字符串模式
bool isInField = true;//是否在读取Field,用来表示空Field
int i = 0;
while (i < text.Length)
{
char ch = text[i];
if (isInQuotation)
{
if (ch == '"')
{
if (i < text.Length - 1 && text[i + 1] == '"')//重复"只算一个,切不结束字符串模式
{
field += '"';
i++;
}
else
{
isInQuotation = false;
}
}
else
{
field += ch;//字符串模式中所有字符都要加入
}
}
else
{
switch (ch)
{
case ',':
line.Add(field);
field = "";
isInField = true;
break;
case '"':
if (isInField)
isInQuotation = true;//进入字符串模式
else
field += ch;
break;
case '\r':
if (field.Length > 0 || isInField)
{
line.Add(field);
field = "";
}
result.Add(line);
line =new List<string>();
isInField = true;//下一行首先应该是数据
if (i < text.Length - 1 && text[i + 1] == '\n')//跳过\r\n
i++;
break;
default:
isInField = false;
field += ch;
break;
}
}
i++;
}
//收尾工作
if (field.Length > 0 || isInField && line.Count > 0)//如果是isInField标记的单元格,则要保证这行有其他数据,否则单独一个空单元格的行是没有意义的
line.Add(field);
if (line.Count > 0)
result.Add(line);
return result;
}
}
相关文章推荐
- mysql load data 导出、导入 csv
- Kinect结合Unity3D引擎开发体感游戏(一)
- C#导出数据到CSV文件的通用类实例
- C#实现导入CSV文件到Excel工作簿的方法
- php操作csv文件代码实例汇总
- php读取csv文件并输出的方法
- C#操作CSV文件类实例
- php使用指定编码导出mysql数据到csv文件的方法
- C#中csv文件与DataTable互相导入处理实例解析
- javascript实现客户端兼容各浏览器创建csv并下载的方法
- 解析csv数据导入mysql的方法
- php导出csv数据在浏览器中输出提供下载或保存到文件的示例
- 如何在symfony中导出为CSV文件中的数据
- Unity3D中脚本的执行顺序和编译顺序
- Unity3D动态对象优化代码分享
- Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法
- MySQL 导出数据为csv格式的方法
- Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作
- Unity3d获取系统时间