unity写入、读取csv

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



}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值