Last active
October 24, 2017 04:23
-
-
Save dskjal/18d49197e8b59635f3dc506e7a7a1f81 to your computer and use it in GitHub Desktop.
Unity で CSV をロードするスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BEGIN MIT LICENSE BLOCK // | |
// | |
// Copyright (c) 2016 dskjal | |
// This software is released under the MIT License. | |
// http://opensource.org/licenses/mit-license.php | |
// | |
// END MIT LICENSE BLOCK // | |
// ******************************使い方************************************* | |
// csv のロード(Resources/test.csv を読み込む場合) | |
// var csv = CSVLoader.Load("test.csv"); | |
// 内容の出力 | |
// foreach(var t in csv){ | |
// Debug.Log(t); | |
// } | |
// データにアクセス | |
// Debug.Log(csv[2, 1]); | |
using UnityEngine; | |
using System.Collections.Generic; | |
public static class CSVLoader{ | |
public class CSV { | |
public CSV() { | |
columns = new List<List<string>>(); | |
} | |
public int GetColumnCount() { return columns.Count; } | |
public int GetRowCount() { return columns.Count > 0 ? columns[0].Count : 0; } | |
public List<string> GetRow(int i) { | |
if(i >= columns.Count) { | |
throw new System.Exception("無効な列が指定されました."); | |
} | |
return columns[i]; | |
} | |
public string this[int row, int column] { | |
get { return this.columns[row][column]; } | |
} | |
public IEnumerator<string> GetEnumerator() { | |
foreach(var row in columns) { | |
foreach(var t in row) { | |
yield return t; | |
} | |
} | |
} | |
internal List<List<string>> columns; | |
} | |
private static List<string> getColumn(string text, int startPos, out int endPos) { | |
var row = new List<string>(); | |
int elemStart = startPos; | |
int i = startPos; | |
bool isContinue = true; | |
while(isContinue) { | |
switch (text[i]) { | |
case '"': | |
++elemStart; | |
// 対応する " まで読み込む | |
while(++i < text.Length) { | |
if(text[i] == '"') { | |
break; | |
} | |
} | |
break; | |
case '\n': | |
isContinue = false; | |
goto case ','; | |
case ',': | |
// クォート時の文字列の長さの調整 フォールスルーがあるため改行もチェック | |
var offset = 1; | |
while(text[i - offset] == '"' || text[i-offset] == '\r' || text[i-offset] == '\n') { | |
++offset; | |
} | |
row.Add( text.Substring(elemStart, i - elemStart - offset + 1) ); | |
elemStart = i + 1; | |
break; | |
} | |
++i; | |
} | |
endPos = i; | |
return row; | |
} | |
// Resources フォルダから読み込む場合はこちらを使う | |
public static CSV LoadFromResources(string path) { | |
var ta = Resources.Load<TextAsset>(path); | |
if (ta == null) { | |
throw new System.Exception("CSV ファイル:" + path + "が見つかりません."); | |
} | |
var csv = new CSV(); | |
using (var sr = new System.IO.StringReader(ta.text)) { | |
var text = sr.ReadToEnd(); | |
for (int i = 0; i < text.Length;) { | |
csv.columns.Add(getColumn(text, i, out i)); | |
} | |
} | |
return csv; | |
} | |
// Resources ファイル以外の場所から読み込む場合はこちらを使う | |
public static CSV Load(string path) { | |
var csv = new CSV(); | |
using (var sr = new System.IO.StreamReader(Application.dataPath + path, System.Text.Encoding.GetEncoding("Shift_JIS"))) { | |
var text = sr.ReadToEnd(); | |
for (int i = 0; i < text.Length;) { | |
csv.columns.Add(getColumn(text, i, out i)); | |
} | |
} | |
return csv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment