Asynchronous load screen
Let's say i have a UI button, and when clicked, this button calls a method which takes a long time (20 seconds) to resolve what it's doing.
I want to display a simple loading screen UI element while the method resolves.
I tried simply enabling a hidden UI element in the code being run, but this doesn't work. The UI element does not appear at all, i assume because the code is not being executed asynchronously and therefore the UI element doesn't get a chance to update its visibility ingame.
What is the best practice approach to this situation?
Answer by Hellium · Nov 15, 2018 at 02:35 PM
Without any code, it will be difficult to answer you...
Depending on your setup I would run a coroutine to wait one (or 2) frame(s) so that the loading panel can appear:
public void FunctionToRunOnClick()
{
StartCoroutine( ShowLoadingPanelForHeavyFunction( () => YourHeavyFunction() ) ;
}
public void ShowLoadingPanel()
{
// Show the loading panel
}
public void HideLoadingPanel()
{
// Hide the loading panel
}
public void YourHeavyFunction()
{
// Do your heavy stuff
}
public IEnumerator ShowLoadingPanelForHeavyFunction( System.Action heavyFunction )
{
ShowLoadingPanel();
yield return null ; // Wait a frame to let the panel to be visible on screen
yield return null ; // Eventually wait a 2nd frame
if( heavyFunction != null )
heavyFunction();
HideLoadingPanel();
}