Unity快速读取表格文件(支持多种格式)

背景

在开发过程中,Unity可能要读取策划写好的配表,这个工具就可以用到了(当然对于小团队使用的,更多的使用SQLite,有机会介绍)
核心代码:

using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

public static class CSVParser
{
	public enum Delimiter
	{
		Comma,
		Tab
	}

	static readonly Dictionary<Delimiter, char> Delimiters = new Dictionary<Delimiter, char>() {{Delimiter.Comma, ','}, {Delimiter.Tab, '\t'}};

	/// <summary>
	/// Load CSV data from specified path.
	/// </summary>
	/// <param name="path">CSV file path.</param>
	/// <param name="delimiter">Delimiter.</param>
	/// <param name="encoding">Type of text encoding. (default UTF-8)</param>
	/// <returns>Nested list that CSV parsed.</returns>
	public static List<List<string>> LoadFromPath(string path, Delimiter delimiter = Delimiter.Comma, Encoding encoding = null)
	{
		encoding = encoding ?? Encoding.UTF8;
		var data = File.ReadAllText(path, encoding);
		return Parse(data, delimiter);
	}

	/// <summary>
	/// Load CSV data from string.
	/// </summary>
	/// <param name="data">CSV string</param>
	/// <param name="delimiter">Delimiter.</param>
	/// <returns>Nested list that CSV parsed.</returns>
	public static List<List<string>> LoadFromString(string data, Delimiter delimiter = Delimiter.Comma)
	{
		return Parse(data, delimiter);
	}

	static List<List<string>> Parse(string data, Delimiter delimiter)
	{
		var sheet = new List<List<string>>();
		var row = new List<string>();
		var cell = new StringBuilder();
		var afterQuote = false;
		var insideQuoteCell = false;
		var readyToEndQuote = false;
		var delimiterChar = Delimiters[delimiter];

		// TODO : コードパスがひじょーにアレなので見やすく改良

		ConvertToCrlf(ref data);

		foreach (var character in data)
		{
			// Inside the quotation marks.
			if (insideQuoteCell)
			{
				if (afterQuote)
				{
					if (character == '"')
					{
						// Consecutive quotes : A quotation mark.
						cell.Append("\"");
						afterQuote = false;
					}
					else if (readyToEndQuote && character != '"')
					{
						// Non-consecutive quotes : End of the quotation.
						afterQuote = false;
						insideQuoteCell = false;

						if (character == delimiterChar)
						{
							AddCell(row, cell);
						}
					}
					else
					{
						cell.Append(character);
						afterQuote = false;
					}

					readyToEndQuote = false;
				}
				else
				{
					if (character == '"')
					{
						// A quot mark inside the quotation.
						// Determine by the next character.
						afterQuote = true;
						readyToEndQuote = true;
					}
					else
					{
						cell.Append(character);
					}
				}
			}
			else
			{
				// Outside the quotation marks.
				if (character == delimiterChar)
				{
					AddCell(row, cell);
				}
				else if (character == '\n')
				{
					AddCell(row, cell);
					AddRow(sheet, ref row);
				}
				else if (character == '"')
				{
					insideQuoteCell = true;
				}
				else
				{
					cell.Append(character);
				}
			}
		}

		// Add last line except blank line
		if (row.Count != 0 || cell.Length != 0)
		{
			AddCell(row, cell);
			AddRow(sheet, ref row);
		}

		return sheet;
	}

	static void AddCell(List<string> row, StringBuilder cell)
	{
		row.Add(cell.ToString());
		cell.Length = 0; // Old C#.
	}

	static void AddRow(List<List<string>> sheet, ref List<string> row)
	{
		sheet.Add(row);
		row = new List<string>();
	}

	static void ConvertToCrlf(ref string data)
	{
		data = Regex.Replace(data, @"\r\n|\r|\n", "\r\n");
	}
}

升级版链接:
https://blog.csdn.net/qq_25122429/article/details/110139312
有需要Demo_005的自取:
https://blog.csdn.net/qq_25122429/article/details/114098885

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W低小调W

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值