- Home /
Unity Image Loading System optimization request
So I have dialogue system (prefab) that is in every scenes. When a character is speaking, we see a picture of it. There is a lot of characters and each characters have many images depending of the state of the game. So on the code side, it looks like something like this:
aCharacterImageWithThisState.SetActive(true)
Now, the prefab has a lot of game objects with UnityEngine.UI.Image. In fact, it is 1 game object with an Image component per character per state (so a lot of game objects) since every image doesn't necessarily have the same size and position.
I was wondering how I could optimize this system since I'm adding multiple different character images every day. I'm scared that in the long term this prefab takes a lot of memory in game with all his hidden images. I thought of using the Resource.Load
but I read at some places that it was risky to use it.
What can I do to optimize this? Thanks
Answer by lvskiprof · Mar 08 at 03:20 PM
I am not sure about it being the best solution as far as memory use, but in a card game I am creating I am using a prefab ScriptableObject that has a single instance to hold all the card images. So it loads everything when the game loads and I never have more than the one instance. I put the instance in the Dealer GameObject and simply pass the reference to it for other classes that need to use it.
In your case, with the different levels, I would have it as part of a Singleton object that keeps the original reference alive. That way it would persist from level to level, but it would not go through the loading process each time. That only optimizes the time to load all the images though and not the memory.
I am interested to see what someone says for trying to optimize memory usage.
Mike
Thanks for your answer. I like your way of having all the images referenced in a scriptable object. Also no worries, the prefab is the same in every scene (it is DontDestroyOnLoad) so I don't reload all the images at every scene changes.
My problem is more on the UI/Scene side where I don't know if I should have as many UnityEngine.UI.Image s as character images.