Adding large amounts of text data

I’ve been trying to figure out what the best way could be to add data to my game, and I’m not entirely sure on how to proceed. There doesn’t seem to be many tutorials covering this.

What I need is to have around, say, 200 entries. All of these contain the same data (name, description, prefab) and they need to be easily added to the player’s inventory.

What I tried first were just simple arrays, one for names, one for descriptions, etc. This looked liked it would work, but the problems started when I tried to add it to the player’s inventory. Having 3+ different arrays meant that I would have needed to add all three of them, and once added I would have no way of connecting the data in the arrays.

So I scrapped that and went for Objects, which seems pretty good, except for the fact that I basically need to create the data every time I start up the game.

    public class figurinesArchive {
        public string Name { get ; set; }
        public string Description { get; set; }

        public figurinesArchive ( string name, string description)
        {
            Name = name;
            Description = description;
        }
    }

And then using

        figurinesArchive figurine1 = new figurinesArchive ("Name #1", "Description #1");

Around 200 times to create all the entries.

Is this the right way to go about it? Can I even assign prefabs to entries like this?

I would suggest starting by loading the text in from an text file. That way you can edit it in something sensible.

1 Like

But with needing hundreds of entries that would require the game to load a lot of different files one after the other.
Is that a good idea? Won’t that slow everything down?

Use a database. MySql and csv grid array solutions are both fast enough to load 5000+ entries in with basically no hiccups.

You should format the data structure in a class, and create those classes from the db when the game starts. Creating the class is the heavy part, loading is pretty fast. I have loaded 500k+ lines with a pretty basic csv grid array solution in like 3 seconds.

1 Like

Do you have anywhere I can read up on this? I’m not entirely sure how to use databases and unity together

Here’s a basic csv read script. This is a pretty straightforward solution.

http://wiki.unity3d.com/index.php?title=CSVReader

Sql is more complex and requires more work to get running but pays off with better organization and more intuitive query potential. Google for MySql and unity. Getting the info for this all in one place is kinda rare, I thought about doing a blog post on it.

Either way, all you are really doing it having one file contain your data, you open a connection to it, peel information out and put that info into a tangible format for your game and read it there.

That looks like what I need, thanks.
Just to ask, would using Mysql require an internet connection? I’ve only ever worked with mysql for webdev so I’m kind of surprised it can be used for unity too

Loading at startup is common practice,. You don’t even have to load everything, only what’s necessary.

The major advantage for externalization is

  • you’re able to seperate the actual data from code
  • you’ll be able to edit it outside (fix typos, adjust names/descriptions and all that stuff) without the need for another compilation of the whole game
  • you’ll be able to easily use localization at a later point in time

MySql is just a plugin to facilitate communication with some sql database you point it at. Internet is not a prerequisite.

Mono strips a couple of dlls you need for it though, so you have to hunt them down and copy into your project folder.

I tend to use json for my data. It’s easy to use, you can add all entries into one file, and just deserialize it into a class, or in a case with multiple entries, a list of that class. Unity has a built in json tool, or there is json.net on the asset store or a free version on the net somewhere that I’ve seen linked a few times.

1 Like

Any automatic way of getting commas between entries once you export the CSV to TXT?
I just get all my entries without any separation

Try harder.