I’ve been creating (trying to…) sort of hobby/demo game project.
This is not definitely an optimization question as I barely have nothing ready yet - just drag and drop, basic item types, inventory and some visuals. But I would just like to hear opinions on this.
I’ve placed my current prototype objects in sort of item database, which is currently either a GameObject with a script where these items are listed, or a comparable ScriptableObject with same items list. I can then call this Item database and get fresh instances of items, with something like: ItemDatabase.instance.GetItem(“rustySword”).
The question is:
If I were to have 200 items in this item database, would these 200 items (simple C# “data only” classes) be loaded as soon as the game runs?
If these items also contain references to GameObjects for on screen display, would these be consuming memory already as soon as the item database is available in scene?
I haven’t tested this myself, and I only have few temporary objects so far, but was thinking if this is so, maybe I just store the tokens/type names of items and then let the item database do some Resources.Load thing instead.
As soon as whatever asset it’s connected to is loaded. Yes, all 200 items are loaded into memory. When that occurs depends on where the GO/SO is loaded.
I have to assume these GameObjects they reference are ‘prefabs’ (especially if you have an SO option for this). And prefabs in this case will follow the same rules prefabs always follow. This means once some asset that references the prefab, the prefab itself is loaded into memory (but not instantiated in a scene). Unity probably does some optimization of this memory usage in that it’s just the template for it… though the specifics of which aren’t actually documented
You can try profiling to see exactly… just create a scene, reference a bunch of prefabs, never instantiate them though. Then build that scene, run it, attach the profiler, and see how much memory is taken up by them. Then maybe have a button that instantiates all of them and see what happens to the memory then.
I’d profile the memory usage.
Personally we have a ‘ItemSet’ of SO’s and those SO’s reference their menu prefab visual. We have hundreds of items in the game and that whole set is loaded up. We haven’t really had any memory issues doing so. But we also haven’t targeted low-end mobile yet.
But yeah, I’d definitely profile before I went and made that big of a change.
I may check this out and profile it myself later on… at work now though, so can’t.
I’m not sure if I got everything right, but I tried to test and see how memory consumption changes if I add/remove meshes from items. I did this only in editor…
I added one 1.5 million poly sphere into project, and made it a prefab.
Then assigned this one prefab in each 4 of my test items. I attached an image that shows the setup, so it isn’t too unclear what I have…
Simply put, I have a ScriptableObject item database linked into inventory “manager” script, and SO itself has fields for items which are SO with one C# class in each.
I tried to see how the memory use looks in the Profiler. I added/removed the prefab here to see the change. I restarted Unity between each test. First I tested scene without Prefabs assigned for items, and then with Prefabs assigned to each item and finally Prefabs assigned to Items, with option to load each item’s prefab (same shared mesh and prefab) into running scene with a key press.
My understanding of profiling is lacking to say the least, but to me it seems like as soon as I’ve added the Prefab into items the memory is used. It doesn’t seem to matter if the Prefab is added to scene or not.
Well I bet you might have some insight into this, might be that I’ve look the wrong metrics to begin with.
Tomorrow is my JLH day when I work on Unity stuff, so I’ll probably take a look into this tomorrow as it is one of those things I’ve wondered about myself as well.