unity写入、读取csv 您所在的位置:网站首页 unity的数据文件怎么打开 unity写入、读取csv

unity写入、读取csv

2023-08-08 17:09| 来源: 网络整理| 查看: 265

xiUnity-----列表功能实现(数据读取.CSV文件)_一个有梦想的Fighter-CSDN博客Unity-----列表功能实现(数据读取.CSV文件)记录一下.csv文件在unity中使用1.文件路径需要把csv文件放在StreamingAssets这个文件夹中。放在StreamingAssets中二进制文件打包后,Unity会将这些二进制文件放置在对应平台下的路径下。所以根据不同平台,访问的路径是不一样的。所以二进制文件一定要放在StreamingAssets 中。2.代码实现(1)定义与.CSV文件对应的类using System.Collections;using Systemhttps://blog.csdn.net/L950122/article/details/115698370Unity-----列表功能实现(数据读取.CSV文件)记录一下.csv文件在unity中使用1.文件路径需要把csv文件放在StreamingAssets这个文件夹中。放在StreamingAssets中二进制文件打包后,Unity会将这些二进制文件放置在对应平台下的路径下。所以根据不同平台,访问的路径是不一样的。所以二进制文件一定要放在StreamingAssets 中。2.代码实现(1)定义与.CSV文件对应的类using System.Collections;using Systemhttps://blog.csdn.net/L950122/article/details/115698370Unity-----列表功能实现(数据读取.CSV文件)记录一下.csv文件在unity中使用1.文件路径需要把csv文件放在StreamingAssets这个文件夹中。放在StreamingAssets中二进制文件打包后,Unity会将这些二进制文件放置在对应平台下的路径下。所以根据不同平台,访问的路径是不一样的。所以二进制文件一定要放在StreamingAssets 中。2.代码实现(1)定义与.CSV文件对应的类using System.Collections;using Systemhttps://blog.csdn.net/L950122/article/details/115698370Unity-----列表功能实现(数据读取.CSV文件)记录一下.csv文件在unity中使用1.文件路径需要把csv文件放在StreamingAssets这个文件夹中。放在StreamingAssets中二进制文件打包后,Unity会将这些二进制文件放置在对应平台下的路径下。所以根据不同平台,访问的路径是不一样的。所以二进制文件一定要放在StreamingAssets 中。2.代码实现(1)定义与.CSV文件对应的类using System.Collections;using Systemhttps://blog.csdn.net/L950122/article/details/115698370Unity-----列表功能实现(数据读取.CSV文件)记录一下.csv文件在unity中使用1.文件路径需要把csv文件放在StreamingAssets这个文件夹中。放在StreamingAssets中二进制文件打包后,Unity会将这些二进制文件放置在对应平台下的路径下。所以根据不同平台,访问的路径是不一样的。所以二进制文件一定要放在StreamingAssets 中。2.代码实现(1)定义与.CSV文件对应的类using System.Collections;using Systemhttps://blog.csdn.net/L950122/article/details/115698370

 读取csv的代码参考了该博主的经验。

以下提供我的代码。

写入csv

using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Xml; using UnityEngine; public class WriteJointCSV : MonoBehaviour { //记录所有关节数据 public int Step; public double joint1; public double joint2; public double joint3; public double joint4; public double joint5; public double joint6; //inspector创建一个bool按钮 [Header(" Click to write csv ")] public bool OnClickToWriteCSV; //路径 public string BinSourcesFolder { get { return Application.streamingAssetsPath + "/SourcesFolder/"; } } // Update is called once per frame void Update() { //设置文件路径与文件名 string path = BinSourcesFolder + "data.csv"; //将所有的要记录的数据通过tostring转换为字符串 string tempstep = Step.ToString(); string tempjoint1 = joint1.ToString(); string tempjoint2 = joint2.ToString(); string tempjoint3 = joint3.ToString(); string tempjoint4 = joint4.ToString(); string tempjoint5 = joint5.ToString(); string tempjoint6 = joint6.ToString(); //创建一个字符串,单独记录一行数据,每个关节数据通过逗号隔开 string AllJoints = tempstep + "," + tempjoint1 + "," + tempjoint2 + "," + tempjoint3 + "," + tempjoint4 + "," + tempjoint5 + "," +tempjoint6; //如果点击了保存按钮,就记录数据 if (OnClickToWriteCSV) { WriteCsv(AllJoints, path); OnClickToWriteCSV = false; } } //写csv函数 public void WriteCsv(string strs, string path) { if (!File.Exists(path)) { File.Create(path).Dispose(); } //UTF-8方式保存,true表示追加的写入方式,改成false就是直接覆盖写入 using (StreamWriter stream = new StreamWriter(path, true, Encoding.UTF8)) { stream.WriteLine(strs); } } }

读取csv(参考上面连接博主的方法,稍微改了点东西,这样避免对继承mono类报错)

using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Xml; using UnityEngine; public class ReadCSV : MonoBehaviour { [Header(" Click to read csv ")] public bool OnClickToReadCSV; //继承monobehaviour的类不能用new来创建,,,,,CSVInfo本来是继承mono的类,但是在这里面改成了结构体,效果一致 public struct CSVInfo { public int Step; public double joint1; public double joint2; public double joint3; public double joint4; public double joint5; public double joint6; } public string BinSourcesFolder { get { return Application.streamingAssetsPath + "/SourcesFolder/"; } } public Dictionary _CSVInfo = new Dictionary(); public void InitInfo() { _CSVInfo.Clear(); string fullFileName = BinSourcesFolder + "data.csv"; string InfoConfig = LanChange(fullFileName); InfoConfig = InfoConfig.Replace("\r", ""); InfoConfig = InfoConfig.Replace("\"", ""); string[] CSVDatas = InfoConfig.Split('\n'); for (int i = 1; i < CSVDatas.Length; i++) { if (CSVDatas[i] != "") { string[] infos = CSVDatas[i].Split(','); CSVInfo data = new CSVInfo { Step = int.Parse(infos[0]), joint1 = double.Parse(infos[1]), joint2 = double.Parse(infos[2]), joint3 = double.Parse(infos[3]), joint4 = double.Parse(infos[4]), joint5 = double.Parse(infos[5]), joint6 = double.Parse(infos[6]) }; _CSVInfo.Add(data.Step, data); Debug.Log(data.Step + " " + data.joint1 + " " + data.joint2 + " " + data.joint3 + " " + data.joint4 + " " + data.joint5 + " " + data.joint6); } } } string LanChange(string path) { //csv文件打开方式用记事本打开,编码UTF8 Encoding utf = Encoding.GetEncoding("UTF-8"); return File.ReadAllText(path, utf); } void Update() { if (OnClickToReadCSV) { InitInfo(); OnClickToReadCSV = false; } } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有