c#和Unity,将已更改为CSV文件的记录更新 [英] c# and Unity, update records that have changed to a CSV file

查看:421
本文介绍了c#和Unity,将已更改为CSV文件的记录更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本质上是记录数据库的CSV文件.在加载游戏时,我读了一条记录并实例化了一个预制按钮,每次一次都带有CSV文件中的正确文本.按钮的索引并不总是与在CSV文件中获得信息的记录的索引相同.到目前为止,我设置了一个保存功能,我希望某些记录基于技巧名称"被更改.字段以反映在CSV文件中.有什么简单的方法可以做到这一点?

I'm trying to work with a CSV file that is essentially a database of records. On load of the game I Read in a record and instantiate a prefab button, one at a time with the correct text from the CSV file. It's not always the case that the index of the button will be the same as the index of the record where I got the information in the CSV file. So far I setup a Save function that I want certain records that have changed based on a tricks "name" field to reflect in the CSV file. What would be an easy way accomplish this?

这是将更改保存到CSV的代码,Unity给我一个异常,IOException:在路径上共享冲突:

Here is the code that Saves changes to the CSV and Unity gives me an exception, IOException: Sharing violation on path:

void WriteCSVFile()
{
    StreamReader strReader = new StreamReader("Assets/Resources/characters.csv.txt");
    StreamWriter strWriter = new StreamWriter("Assets/Resources/characters.csv.txt");
        
    bool endOfFile = false;
    string header = strReader.ReadLine();
    strWriter.WriteLine(header);
    string data;
        
    while(!endOfFile)
    {
        data = strReader.ReadLine();
        strWriter.WriteLine(data);
        
        if(data == null)
        {
            endOfFile = true;
            break;
        }
        
        //dataValues is each row
        var dataValues = data.Split(',');
        
        if(dataValues[1] == trickName)
        {               
            dataValues[3] = currentXP.ToString();
            dataValues[4] = currentLevel.ToString();
            dataValues[5] = maximumXP.ToString();
            strWriter.WriteLine(dataValues[3]);
            strWriter.WriteLine(dataValues[4]);
            strWriter.WriteLine(dataValues[5]);             
        }
    }   
}

更新:它现在似乎正在工作,但我需要进一步的帮助.在我的保存函数的更新版本中,您可以看到我将每个记录用逗号分隔成一个名为dataValues的数组.我对dataValues中的正确字段进行了更改,但是现在当我执行Log语句时,文本被分成几行.您如何看待我可以将文件附加回一起以覆盖原始CSV文件?这是我保存功能的更新:'''void WriteCSVFile(){

UPDATE: It seems to be working now but I need help with one more step. In the updated version of my save function you can see that I split each record by the commas into an array called dataValues. I make changes to the right fields in dataValues but now the text is separated into single lines when I do a Log statement. How do you think I can append the file back together to Write over the original CSV file? Here is the update to my save function: '''void WriteCSVFile(){

    string txt = System.IO.File.ReadAllText("Assets/Resources/characters.csv.txt");
    //StreamWriter strWriter = new StreamWriter("Assets/Resources/characters.csv.txt");
    
    string[] data = txt.Split(new char[] { '\n' });
    
    foreach(string record in data){
        var dataValues = record.Split(',');

        if(dataValues.Length >= 6 && dataValues[1] == trickName){
            Debug.Log(dataValues[1]);
            //dataValues[3] = currentXP.ToString();
            //dataValues[4] = currentLevel.ToString();
            //dataValues[5] = maximumXP.ToString();
        } 
    }
    
    '''

推荐答案

在您的代码中,您假设每行至少包含2个字段(通过测试dataValues[1] == trickName).还要添加一个长度检查:

In your code, you are assuming that every line contains at least 2 fields (by testing for dataValues[1] == trickName). Add a check for the length as well:

if (dataValues.Length >=6 && dataValues[1] == trickName)
{
   ...
}

检查长度(至少)为6,以使dataValues[5]的使用有效.

Check for length (at least) 6, so that the use of dataValues[5] is valid.

这篇关于c#和Unity,将已更改为CSV文件的记录更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆