Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

haifuri
情報系の院生です
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした