- Home /
Best way to change sprite in Image (GUI 4.6) ?
Hi everyone,
I try to convert my NGUI project to Unity 4.6 project. Even if you don't know NGUI you can maybe help me.
In NGUI, to change a sprite by another dynamically, we just simply did "mySprite.spriteName = "myNewImage", and it automaticly find the matching image on the Atlas, and display the new sprite.
In GUI 4.6, I see everywhere in UnityAnswers that we need to link OR load all the sprites, and then do a "spriteList.Find()" to get the new sprite and replace the older one.
My problem : In NGUI, the way it was coded didn't require to load in memory all others images. And I just check the inspector for the 4.6 system and I clearly see the loaded image in memory.
What is the point ? The Atlas (or Pack in 4.6) is already loaded in memory, and I need to RELOAD again all sprites of the atlas/pack to being able to switch between them ? There's no way to access the sprite without loading it ?
My question : Am i missing something about switch image in the same atlas and/or memory usage of 4.6, or "this is how it works, too bad, your project just increase its memory needs" ?
Answer by Priyanshu · Jan 30, 2015 at 11:16 AM
I have never used NGUI and don't know the method used by it for loading sprites at run time.
All unused assets do not require any memory( Hard Disk memory). That is they are not included in the Game Build.
When a scene starts Unity creates instances of Objects in the scene. Memory(RAM) will be allocated to the objects in the scene and not assets. It will only load sprites which are used in the scene. Hence it depends on you. If you want to load them at the start of the scene or at run-time.
You can use two methods to change sprites:
Save them in the beginning of the scene in a list or array. This method is useful when you do not have large number of objects to load in scene i.e. loading time is small.
Load them while your scene is running using this. Useful when your scene takes a lot of time loading and you want to minimize the number of objects to load at the start of scene.
P.S. - I'm just a beginner. Can't help you much in memory section. Maybe someone can correct me if i'm wrong. Or give better insight.
Thanks for the answer. I know all of this, I was just asking if there's a way to NOT load all the sprites you need to switch at runtime, by accessing directly the sprite-pack of GUI 4.6.
But the more I look on Internet, the more my answer seems to be "no".
Your answer
Follow this Question
Related Questions
Unity 4.6: How to fit center byte array png in the Image UI? 1 Answer
UI 4.6 APK size problem : Sprite both in Atlas and Inspector 0 Answers
Loading built-in resources 1 Answer
Animated Sprite in UI Canvas? 4 Answers
Building scenes and load time 1 Answer