###前提・実現したいこと
unitydで以下のCSVを読み込んでdictionaryを用いて連想配列にしようと思いました。
1回目foreachのでkeyを宣言し2回目のforeachで値を上書きして出力しようと思いましたがうまくいきませんでした。
key,valueの値が正しく入るようにするにはどうすればいいでしょうか?
csv
1HP,IP,Power,defense,Brainpower,Lack,Speed 230,8,10,6,6,6,9
###発生している問題
###該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3using System.IO; 4using System.Collections.Generic; 5using System.Linq; 6using System.Text; 7using System.Threading; 8 9public class readCSV : MonoBehaviour 10{ 11 int count = 1; 12 // Use this for initialization 13 void Start() 14 { 15 //TextBox1に入力されている文字列から一行ずつ読み込む 16 //文字列(statusに入力された文字列)からStringReaderインスタンスを作成 17 TextAsset csv = Resources.Load("CSV/status") as TextAsset; 18 StringReader reader = new StringReader(csv.text); 19 Dictionary<string, int> map = new Dictionary<string, int>(); 20 //ストリーム(今回はCSV)の末端まで繰り返す 21 while (reader.Peek() > -1) 22 { 23 string line = reader.ReadLine(); 24 string[] splitLine = line.Split(','); 25 if (count == 1) 26 { 27 28 foreach (string stData in splitLine){ 29 map.Add(stData, 0); 30 } 31 count++; 32 }else{ 33 foreach (string stData in splitLine) 34 { 35 map["{0}"] = int.Parse(stData); 36 } 37 } 38 } 39 foreach (KeyValuePair<string, int> pair in map) 40 { 41 Debug.Log(pair.Key + " : " + pair.Value); 42 } 43 } 44}
回答2件
あなたの回答
tips
プレビュー