以下提供我的代码。

写入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<int, CSVInfo> _CSVInfo = new Dictionary<int, CSVInfo>();

    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;
        }
    }



}