LoginSignup
32
23

More than 5 years have passed since last update.

UnityでCSVを読み込むときの軽いメモ

Last updated at Posted at 2016-08-12

Unityで音ゲーを作っていますが、その譜面としてCSVを使っています。
その読み込み方法の個人的なメモです。

前提として、Assets/Resources/CSV下に.csvファイルを配置。

コード
private string musicName; // 読み込む譜面の名前
private string level; // 難易度
private TextAsset csvFile; // CSVファイル
private List<string[]> csvDatas = new List<string[]>(); // CSVの中身を入れるリスト
private int height = 0; // CSVの行数

void Start(){
  musicName = "sample"; // 曲名
  level = "0"; // 難易度
  csvFile = Resources.Load("CSV/" + musicName + level) as TextAsset; /* Resouces/CSV下のCSV読み込み */
  StringReader reader = new StringReader(csvFile.text);

  while(reader.Peek() > -1) {
    string line = reader.ReadLine();
    csvDatas.Add(line.Split(',')); // リストに入れる
    height++; // 行数加算
  }

}

この例では、sample0.csvを読み込み。
将来的に sample0.csv、sample1.csvのように数字で難易度を管理したい。
Resources.LoadでResourcesフォルダ内を探してくれる(?)
CSVを読み込むときに、拡張子(.csv)は書かなくていいみたい。
あとは、

Debug.Log(csvDatas[0][0]);

あたりで、ちゃんと読み込めているか確認すればOK

32
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
23