Greetings everyone! I’ve switched to Unity very recently and I need help with UI.
I made a user interface that holds many tabs , each tab will take the user to specific window (Items , Quests , Gears , Upgrades , Etc) you name it , it is there (seriously, you want a tab for people who like trains it is there). here is the thing , it is taking too much RAM on smart phones. what is the best way to tackle this issue?
The approach that I have in mind is to :
- Disable all the other tabs
- Unload all resources(images) from the closed tab
- load all resources from current tab
- display the tab
Please notify me if there is a better approach or guidlines to follow regarding this issue. also 1 more question :
does disabling an object affects GPU/RAM performance or only CPU? as far as i know, disabling an objects/and scripts reduces the number of Updates being called every frame and drawing calls but pleasse correct me if i am wrong. I’m Very new to unity UI so anything i need to know that would help me in developing good UI for smartphone would be much appreciated
Hi Apro,
I think the cleanest way to do this would be to store each tab as a prefab, and then load the whole tab in when you need to, and destroy it when you switch tabs (see code [I hope you’re using C#]).
public Transform tabParent;
public void LoadAndDraw(string tabName)
{
//Destroy existing tab(s)
DestroyTabs();
//Load the prefab from resources and Instantiate
GameObject instance = Instantiate(Resources.Load(tabName, typeof(GameObject))) as GameObject;
//Make the new instance a child of the correct object in scene
instance.transform.SetParent(tabParent);
}
void DestroyTabs()
{
//This is a quick way to desroy the existing tab(s) assuming that everything which is a child of the tabParent can be deleted
foreach(Transform tab in tabParent)
{
Destroy(tab);
}
//Clean up
Resources.UnloadUnusedAssets();
}
Disabled Objects do not send draw calls, so the GPU should not have any load from a disabled object. as for RAM, all objects in the scene are loaded into memory, active or not. If RAM is a problem, use the code above; if processing speed is an issue you’re better just leaving all the tabs (or the most common ones) in the scene but inactive.
Hope this helps!
Elis
Hey Elis!! Really thanks for this approach! My processing speed is not issue at all so far , I’m just using too much RAM ^^ I’ll be trying this approach tonight and see how it goes , from the looks of it , it should get the exact results I’m hoping for
really appreciate giving me some of your time wishing you nice day and stay cool!