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?
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.
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
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.