CSVDataTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.IO;
  6. using System;
  7. using System.Text;
  8. public class CSVDataTest : MonoBehaviour
  9. {
  10. string _loadPath;
  11. string _savePath;
  12. [SerializeField] private string _fileName = "测试";
  13. private const string EXTENSION = ".csv";
  14. [SerializeField] private Button _saveBtn;
  15. [SerializeField] private Button _loadBtn;
  16. [SerializeField] private Text _display;
  17. private CSVTable _table;
  18. // Bind Component
  19. void Awake()
  20. {
  21. _loadPath = Application.streamingAssetsPath + "/Load/";
  22. _savePath = Application.streamingAssetsPath + "/Load/";
  23. _saveBtn.onClick.AddListener(Save);
  24. _loadBtn.onClick.AddListener(Load);
  25. }
  26. /// <summary>
  27. /// 加载文件
  28. /// </summary>
  29. private void Load()
  30. {
  31. if (!Directory.Exists(_loadPath))
  32. {
  33. Debug.LogError("The file not be found in this path. path:" + _loadPath);
  34. return;
  35. }
  36. string fullFileName = _loadPath + _fileName + EXTENSION;
  37. StreamReader sr;
  38. sr = File.OpenText(fullFileName);
  39. string content = sr.ReadToEnd();
  40. sr.Close();
  41. sr.Dispose();
  42. _table = CSVTable.CreateTable(_fileName, content);
  43. // 添加测试
  44. //Test();
  45. }
  46. /// <summary>
  47. /// 存储文件
  48. /// </summary>
  49. private void Save()
  50. {
  51. if (_table == null)
  52. {
  53. Debug.LogError("The table is null.");
  54. return;
  55. }
  56. string tableContent = _table.GetContent();
  57. if (!Directory.Exists(_savePath))
  58. {
  59. Debug.Log("未找到路径, 已自动创建");
  60. Directory.CreateDirectory(_savePath);
  61. }
  62. string fullFileName = _savePath + _fileName + EXTENSION;
  63. StreamWriter sw;
  64. sw = File.CreateText(fullFileName);
  65. //sw = new StreamWriter(fullFileName, false, Encoding.UTF8);
  66. sw.Write(tableContent);
  67. sw.Close();
  68. sw.Dispose();
  69. _table = null;
  70. _display.text = "Save.";
  71. }
  72. /// <summary>
  73. /// 测试方法
  74. /// </summary>
  75. private void Test()
  76. {
  77. // 显示所有数据(以调试格式显示)
  78. Debug.Log(_table.ToString());
  79. // 显示所有数据(以存储格式显示)
  80. //_display.text = _table.GetContent();
  81. _display.text = _table["25"]["stepname"] + "\n";
  82. // 修改某一数据
  83. _table["26"]["toolname"] = "胶带";
  84. _display.text += _table["26"]["toolname"] + "\n";
  85. // // 拿到某一数据
  86. // _display.text += "\n" + "1001的年龄: " + _table["1001"]["年龄"];
  87. // // 拿到数据对象
  88. // _display.text += "\n" + "1002的数据: " + _table["1002"].ToString();
  89. // // 修改某一数据
  90. // _table["1003"]["年龄"] = "10000";
  91. // _display.text += "\n" + "1003新的年龄: " + _table["1003"]["年龄"];
  92. // // 添加一条数据
  93. // CSVDataObject data = new CSVDataObject("1005",
  94. // new Dictionary<string, string>()
  95. // {
  96. // { "姓名","hahaha" },
  97. // { "年龄","250" },
  98. // { "性别","随便吧" },
  99. // },
  100. // new string[] { "编号", "姓名", "年龄", "性别" });
  101. // _table[data.ID] = data;
  102. // _display.text += "\n" + "新添加的1005的数据: " + _table["1005"].ToString();
  103. // // 删除数据
  104. // _table.DeleteDataObject("1001");
  105. // _table.DeleteDataObject("1002");
  106. // _display.text += "\n" + "删了两个之后:" + "\n" + _table.GetContent();
  107. // // 删除所有数据
  108. // _table.DeleteAllDataObject();
  109. // _display.text += "\n" + "还剩下:" + "\n" + _table.GetContent();
  110. }
  111. }