Display loading indicator during onClick event?
Hi,
I'm working on a function where user could load a song by clicking a button and the system will generate some data based on the song. Everthing works fine but I found the time takes to generate data from song maybe long. Thus I'm thinking create a "loading indicator" during the data generating process. My current code is as follows:
public void OpenFile () {
var extensions = new [] {
new ExtensionFilter("Music Files", "ogg"),
};
var path = StandaloneFileBrowser.OpenFilePanel(
"Open File",
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop),
extensions,
false);
if (path[0].Length != 0) {
songLocation = new System.Uri(path[0]).AbsolutePath;
songLocation = WWW.UnEscapeURL (songLocation);
StartCoroutine (LoadSongCoroutine (path[0]));
}
}
IEnumerator LoadSongCoroutine(string path)
{
var audioLocation = new WWW (path);
yield return audioLocation;
StartIndicator ();
audioSource.clip = audioLocation.GetAudioClip (false, false);
GenerateData ();
StopIndicator ();
}
The OpenFile ()
function is called whenever user clicked a button.
I intend the StartIndicator ()
function to cover the screen with a blur image and display a spining indicator on it. However, this simply does not work.
I tried to use Debug.Log to see when the two StartIndicator ()
and StopIndicator ()
functions work. It turn out they are called only after everything has done. I tried to log something directly after the OpenFile()
function, the log only display in the console after file opened and data collected.
I'm thinking this problem maybe related to how Unity handles OnClick
event? How can I work this out?
Thank you.
Call StartIndicator () before creating a new object.
IEnumerator LoadSongCoroutine(string path)
{
StartIndicator (); // should be here
var audioLocation = new WWW (path);
yield return audioLocation;
audioSource.clip = audioLocation.GetAudioClip (false, false);
GenerateData ();
StopIndicator ();
}
Thank you, it works!
Could you please explain why I need to call StartIndicator () before creating a new object?
its because when you var audioLocation = new WWW (path); create new object of www request then its response returns after some time not immediately so i said call StartIndicator before creating object.
Your answer
Follow this Question
Related Questions
UI button not working (2D) [Solved] 2 Answers
check if-statement/bool when button is pressed? 1 Answer
Button reacts six times upon one click (undesired) 0 Answers
OnClick() event script from a prefab 0 Answers