スクリプト
CSVLoader.cs
using UnityEngine;
using System.Collections;
using System.IO;
public class CSVLoader : MonoBehaviour {
// Use this for initialization
void Start () {
int i=0, j;
TextAsset csv = Resources.Load ("CSV/sample") as TextAsset;
StringReader reader = new StringReader (csv.text);
while (reader.Peek () > -1) {
i++;
Debug.Log (i+"回目");
string line = reader.ReadLine ();
string[] values = line.Split (',');
for (j = 0; j < values.Length; j++) {
Debug.Log (values [j]);
}
}
}
}
処理の流れ
- TextAsset型にCSVファイルをロードする.
- StringReader型でCSVファイルを読むやつを作成する.
- reader.Peek()>-1になるまで以下の処理
- string line に reader.ReadLine()で一行ずつ読み込む
- values[]に列を格納
- valuesの値をどっかにいれる
注意点
- CSVの日本語が文字化けする
対処法探し中 - using System.IO;