Reputation: 13551
I have been using scriptable objects as the model (data) layer for my game. I store things like unit stats there. I also started putting the sprites for the unit in this as well.
My question is: If I load a scriptable object which has a reference to a sprite, is the sprite automatically loaded as well? If I load 1000 scriptable objects with sprite references but I am not using those sprites (e.g. no GameObject is using that sprite to render), is there still a memory penalty for these sprite references? Or does the memory use only occur once a GameObject starts using the sprite to actually render? If so, if I have multiple scriptable objects with references to the same sprite, does this increase the memory as well?
I tried doing some memory inspection using the inspector but I was getting sporadic results and the memory footprint was changing too much between runs so I couldn't figure out what was going on (without changing anything I would get 2.2gb in use, then 3.1gb, then 2.6gb).
Upvotes: 2
Views: 1889
Reputation: 1
I used the profiler to run some experiments. And got the following result: No. A sprite is not loaded into memory if it is referenced by a Scriptable Object that is referenced from the scene.
If you want to test this, be sure to restart the editor as the profiler will show that the file is in memory even if you have previewed it in the inspector. In addition, sprites are not unloaded from memory unnecessarily, and each time you need to restart the editor if the sprite is in memory.
Unity 2021.3.10f1
Upvotes: 0
Reputation: 181
Just as Dave said in his comment, Sprite is a reference type, so Unity loads the sprite in the memory once and then all other instances of Objects that reference the specific Sprite point to its memory address (just as a C pointer system would work). So in your case, even if you create 1000 Objects from your Scriptable Object, all of them would point to the same Sprite.
Generally, if you're using Resources.Load()
on an Object, then every loaded Object will use the same variable reference that the parent Scriptable Object had. But, if you're creating a new Object each time you want to create one, say with a class constructor, then each Object (and subsequently, their variables) will have their own space in the memory. In your case, that would happen if you were to use Sprite mySprite = new Sprite()
every time you created a Scriptable Object.
So, there is no memory penalty for adding multiple instances of the same Sprite in your Scriptable Objects (CPU/GPU usage is another issue, not related to your question but worth mentioning nonetheless). Your memory inspection might have fluctuating values if you are performing it on your game, together with all the other operations that are being performed, so I suggest you create a new Project and then try to measure the values from the Inspector. If you do that, please share your findings here, since this is an interesting topic.
Also, check out this post from the GameDev stack exchange for more info on your question.
Upvotes: 1