CSVHelper.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class CSVHelper
  6. {
  7. public static IList<T> Csv2List<T>(string fileName, Func<string[], T> setFunc)
  8. {
  9. List<T> list = new List<T>();
  10. TextAsset textAsset = Asset.LoadFromResources<TextAsset>("Conf/DB", fileName);
  11. using (StringReader stringReader = new StringReader(textAsset.text))
  12. {
  13. string text;
  14. while ((text = stringReader.ReadLine()) != null)
  15. {
  16. string[] arg = text.Split(new char[]
  17. {
  18. ','
  19. });
  20. list.Add(setFunc(arg));
  21. }
  22. }
  23. return list;
  24. }
  25. public static IDictionary<TKey, TValue> Csv2Dictionary<TKey, TValue>(string fileName, Func<string, TKey> setKey, Func<string[], TValue> setValue)
  26. {
  27. Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
  28. TextAsset textAsset = Asset.LoadFromResources<TextAsset>("Conf/DB", fileName);
  29. using (StringReader stringReader = new StringReader(textAsset.text))
  30. {
  31. string text;
  32. while ((text = stringReader.ReadLine()) != null)
  33. {
  34. string[] array = text.Split(new char[]
  35. {
  36. ','
  37. });
  38. dictionary.Add(setKey(array[0]), setValue(array));
  39. }
  40. }
  41. return dictionary;
  42. }
  43. }