1

All options to read .csv files in Unity that I have found online involve implementing it yourself.

(E.g. https://answers.unity.com/questions/782965/reading-data-from-a-csv-file.html, https://forum.unity.com/threads/how-to-read-a-dataset-from-a-csv.858814/ , https://www.theappguruz.com/blog/unity-csv-parsing-unity, and many more...)

In .NET, you can use the VisualBasic library to parse CSVs

But the VisualBasic library is not a part of Unity's ecosystem. You need to manually import Microsoft.VisualBasic.dll and dependencies, and update csc.rp

I would think that CSV parsing would be a "basic" function for C# to have. It has a JSON parser, .csv isn't that too far off. Is there really no .csv parser that works out of the box in Unity?

Null Salad
  • 765
  • 2
  • 16
  • 31
  • Doesn't CsvHelper.IO work with Unity? – ProgrammingLlama Dec 27 '21 at 07:01
  • 1
    *But the VisualBasic library is not a part of Unity's ecosystem* - there are loads of things not "built into c#" (whatever that means). Microsoft give you the tools to roll your own stuff, so you get on and do that - lots of people have already done it with CSV and I'd be amazed if the assertion "there isn't a single csv reading library in the world that works in a unity project" is true.. – Caius Jard Dec 27 '21 at 08:37
  • Right so for example, UnityEngine contains a JSON reader. Other default libraries like System.IO contain file stream readers. As I can only see external libraries to solve this, I must conclude there's no "off the shelf" solution within unity or any default libraries – Null Salad Dec 27 '21 at 09:48
  • Also the provided duplicate question doesn't really answer my question. The top rated answer links to another 3rd party library. Again, only more evidence that Unity does not contain a CSV reader, not is it provided with any default C# libraries – Null Salad Dec 27 '21 at 09:52

1 Answers1

-7

Csv files are plain text, they are very simple. I think it would be faster to write a few csv parsing classes than post on SO.

Doesnt File.ReadAllLines + String.Split(',') = csv parser?

Guye Incognito
  • 2,726
  • 6
  • 38
  • 72
  • 2
    "Doesn't `File.ReadAllLines + String.Split(',')` = csv parser?" - **no, it doesn't**. It doesn't handle enquoted text fields with embedded commas. It doesn't handle more than 1 row of data. It doesn't handle `NULL` literals. And `String.Split` is amongst the worst ways to "parse" a string due to all the new string allocations and copies it performs, which is definitely something to avoid in a game engine. – Dai Dec 27 '21 at 08:55
  • 1
    It also doesn't handle a cell of text that goes over multiple lines (which is something that CsvHelper.io handles). – ProgrammingLlama Dec 27 '21 at 09:10
  • 1
    Ideally I would also like to encapsulate the csv into some sort of data structure to make indexing easier. It's a pretty big todo to reimplement a CSV parser. Sadly it seems like the only solutions exist in 3rd party libraries and, .NET natively, within VisualBasic – Null Salad Dec 27 '21 at 09:55