-1

I have some really big problem. I try to read my csv files using CSVReader, but I have an error, "Script serailzation" What should I do? Please help me! Here is my code. I'm trying to check my path and folders name, but I can't fix it.

Error screenshot

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

Code screenshot 1 (CSV Reader class)

Code screenshot 2 (language : MonoBehaviour class

derHugo
  • 83,094
  • 9
  • 75
  • 115
BlackSperm
  • 108
  • 5
  • in general [don't use `Ressources`](https://learn.unity.com/tutorial/assets-resources-and-assetbundles#5c7f8528edbc2a002053b5a7) .. if you anyway have to put a path in there rather go for e.g. [`StreamingAssets`](https://docs.unity3d.com/Manual/StreamingAssets.html) – derHugo May 08 '23 at 11:03
  • Imho you should split this out ... the `CSVReader` should **not** be responsible for getting the file ... but do what it's name says: Convert a given CSV string input into the dictionary .. makes it way more flexible and has no hidden dependencies on some resources/assets your `language` would rather read the file and pass in the content / you could even go for streamed file access without loading it into memory (https://stackoverflow.com/a/13168006/7111561 bottom of the answer) – derHugo May 08 '23 at 11:13

1 Answers1

3

You have a clear description of the problem in the console as I see on the first image.

You are trying to make the problematic call on assigning your data_lang variable in the language script. Change it with something like

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

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

how it is advised in the error message and it should work fine

Morion
  • 10,495
  • 1
  • 24
  • 33