現在、Unityにて制作しているゲームのために外部CSVファイルよりデータを読み取り多言語対応を柔軟にする
システムを自作しているところなのですが、このデータ群をカンマの区切りで分けるまでは大丈夫だったのですが、
行ごとに取り込むのではなく、列ごとに配列に取り込みたいです。
https://gyazo.com/06093c2eddee282eda12196ae36f9aaa
例えばこれであれば、
CSV内の"~ItemKey"の列のデータを「ItemKey」という配列に格納したいです。
事前に検索などしてみたのですが、ピンと来る情報がありませんでした。
もし方法がありましたらお願いします。
以下、CSVファイルのロード関数と配列の宣言です。
C#
1public class ItemString : MonoBehaviour { 2 [SerializeField] 3 public List<Type> ItemStringTable = new List<Type>(); // declaration 4 public List<String> ItemKey = new List<String>(1000); 5 public List<String> ItemName = new List<String>(1000); 6 public List<String> ItemDesc = new List<String>(1000); 7 public List<String> ItemType = new List<String>(1000); 8 void Start() 9 { 10 } 11 12}
C#
1 ItemString ItemStr; 2 public const string _Extension = ".csv"; 3 public const char _Split_Char = ','; 4 string HeaderString = "~"; 5 6~中略~ 7 8public void CsvLoadStringEx(string LoadPath) 9 { 10 StreamReader sr = new StreamReader(new FileStream(LoadPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); 11 Debug.Log("File Loaded"); 12 int counter = 0; 13 string line = ""; 14 while((line = sr.ReadLine()) != null) 15 { 16 if (line.Contains(HeaderString)) 17 { 18 continue; 19 } 20 Debug.Log("Start to spliting."); 21 string[] fields = line.Split(_Split_Char); 22 foreach(var key in fields) 23 { 24 if(key.Contains(HeaderString) || key == "") 25 { 26 continue; 27 } 28 ItemStr.ItemKey.Add(key); 29 Debug.Log("Key Added."); 30 } 31 foreach(var name in fields) 32 { 33 if(name.Contains(HeaderString) || name == "") 34 { 35 continue; 36 } 37 ItemStr.ItemName.Add(name); 38 } 39 foreach(var desc in fields) 40 { 41 if(desc.Contains(HeaderString) || desc == "") 42 { 43 continue; 44 } 45 ItemStr.ItemDesc.Add(desc); 46 } 47 foreach(var type in fields) 48 { 49 if(type.Contains(HeaderString)||type == "") 50 { 51 continue; 52 } 53 ItemStr.ItemType.Add(type); 54 } 55 Debug.Log(ItemStr.ItemKey[1]); 56 Debug.Log(ItemStr.ItemName[1]); 57 } 58 59 }
現在は以下のように1文字目から順にデータが格納されてしまっています。
https://gyazo.com/9b5f894ed5237a12824d2fde4f007efe

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。