Unity的CSVReader“脚本序列化错误”

5fjcxozz  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(88)

我有个大问题我尝试使用CSVReader读取我的csv文件,但出现错误,“脚本serailzation”我该怎么办?请帮帮我!这是我的代码。我试图检查我的路径和文件夹名称,但无法修复。

//CSVReader Script

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class CSVReader
{
    static string SPLIT_RE = @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))";
    static string LINE_SPLIT_RE = @"\r\n|\n\r|\n|\r";
    static char[] TRIM_CHARS = { '\"' };

    public static List<Dictionary<string, object>> Read(string file)
    {
        var list = new List<Dictionary<string, object>>();
        TextAsset data = Resources.Load(file) as TextAsset;

        var lines = Regex.Split(data.text, LINE_SPLIT_RE);

        if (lines.Length <= 1) return list;

        var header = Regex.Split(lines[0], SPLIT_RE);
        for (var i = 1; i < lines.Length; i++)
        {

            var values = Regex.Split(lines[i], SPLIT_RE);
            if (values.Length == 0 || values[0] == "") continue;

            var entry = new Dictionary<string, object>();
            for (var j = 0; j < header.Length && j < values.Length; j++)
            {
                string value = values[j];
                value = value.TrimStart(TRIM_CHARS).TrimEnd(TRIM_CHARS).Replace("\\", "");
                object finalvalue = value;
                int n;
                float f;
                if (int.TryParse(value, out n))
                {
                    finalvalue = n;
                }
                else if (float.TryParse(value, out f))
                {
                    finalvalue = f;
                }
                entry[header[j]] = finalvalue;
            }
            list.Add(entry);
        }
        return list;
    }
}

pes8fvy9

pes8fvy91#

正如我在第一张图中看到的那样,您在控制台中清楚地描述了问题。
您正在尝试在language脚本中对data_lang变量赋值时进行有问题的调用。换成类似

class language : MonoBehavuiour
{
    List<Dictionary<string, object>> data_lang;

    void Awake()
    {
        data_lang = CSVReader.Read("lang");
        
        // do your other staff here
    }
}

错误消息中的建议以及它应该正常工作

相关问题