How to store LOTS of game assets to QUICKLY find and load in runtime
Hello,
Short Desc
I'm working on a medium to large scale RTS style game in which i will need to store dozens if not hundreds of meshes, textures, sprites etc (units, weapons, icons, skills, items etc).
Requirements
Say I want to store a list of units, each has some atributes (hp, atk, etc) and models/textures, sprites. If Im looking for a specific unit, the system needs to quickly find and retrieve it. But i'll also need to load all of them to do some filtering or whatever. Kind of like a database, but stored locally in unity.
My Implementation so far / Ideas to improve existing solution
Currently, i have a Scriptable Object that stores all the unit's stats, reference to the prefab with meshes/textures, sprites in a Resources folder. Each unit has a unique ID , then if i want to load a specific unit by ID, i first need to load ALL my units, check their IDs, then return the one with the correct ID. Problem is, buy doing this, the game has to load ALL the models, textures etc associated with all the units, which is less than ideal.
My idea for improving this would be to only store lightweight data (ids, names, descriptions, stats) in a ScriptableObject, so when looking through all my units, i only have to load some ints, strings...maybe an icon. But also store a string containing the location of the heavy data (meshes, textures) also in the Resources, but separately. This way, i can also load the actual prefab when i find the correct unit in the list.
This would probably work fine, but i imagine it seems rather messy and inelegant. There has to be a better way of doing this... How do huge RPGs or MMOs do it? Any suggestions?
Answer by agray427 · Jun 14, 2016 at 08:08 PM
Well, you could store each unit's (or whatever else's) data as an individual scriptable object (in separate folders to keep it clean) and then have a scriptable object with a dictionary and pass in the id and have it return the path to the data you're requesting. I'm not sure if you have a resource database or if you have a database for each type of resource, but that would allow you to only search through a very lightweight dicitionary. If you want to further your question, I can try to think of something else, but this is what I would recommend.
Edit: The dictionary of course would have the key be whatever type you're using for the ID and the value be a string for the path.
Yes, this is similar to what i had in $$anonymous$$d, but i was thinking if there was a radically different / better solution to my problem. Thanks for the answer though, you gave me an idea, if nothing else, i'll use a dictionary solution
I made a quick example of what you could do. Unfortunately, Unity doesn't let you edit a Dictionary in the Inspector... Nor does it let you edit a $$anonymous$$eyValuePair. So what you need to do is create a class that acts like a $$anonymous$$eyValuePair and store it within a list. This is just for editing. Then you will translate that over to the Dictionary so you can do the $$anonymous$$ey search.
Alternatively, $$anonymous$$icrosoft does have its source code for a dictionary out on the Web. If you want to create your own class, a "Database" perhaps, that has the same functionality concepts of a dictionary, I recommend looking at that.
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
[CreateAsset$$anonymous$$enu(fileName = "Database", menuName = "Create Database", order = 0)]
[System.Serializable]
public class Database : ScriptableObject
{
[SerializeField]
private List<Entry> entries;
private Dictionary<string, Item> items;
public Dictionary<string, Item> GetDictionary()
{
if (items.Count != entries.Count)
{
items = new Dictionary<string, Item> ();
foreach (Entry entry in entries)
{
items.Add (entry.$$anonymous$$ey, entry.Value);
}
}
return items;
}
[System.Serializable]
public class Entry
{
public string $$anonymous$$ey;
public Item Value;
}
}
[System.Serializable]
public class Item
{
public string Name;
public int Value;
}
I hope this helps you out. If you have any further questions or would like more functionality than what I have provided, leave another comment.
Yeah, using a dictionary is a pretty good solution.
I asked this question because i was wondering if there were other methods of storing/loading assets. Something built into Unity, or i dunno :P
Thanks for your time, i'll wait if anyone else has other suggestions, if not, ill mark this as an answer
@agray427 how to add entries via the script usin gyour code?
Your answer
![](https://koobas.hobune.stream/wayback/20220612082935im_/https://answers.unity.com/themes/thub/images/avi.jpg)