Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Display a loading logo while the rest of the execution is working

Discussion in 'Scripting' started by Cherrygolo, May 14, 2021.

  1. Cherrygolo

    Cherrygolo

    Joined:
    Apr 27, 2021
    Posts:
    4
    Hello,

    I would like to display a loading logo when the user click on a button (in my project : a image with a text as children component) while an 3D object is imported but for the moment the loading appears at the same time that the object appears, i don't know why because I display the image before the importation :

    Code (CSharp):
    1.  
    2.     public void OnPointerClick(PointerEventData pointerEventData)
    3.     {
    4.         GameObject.Find(name).GetComponentInChildren<Image>().color = new Color(0.8f, 0.4f, 0.1f, 1f);
    5.         GameObject.Find(name).GetComponentInChildren<Text>().color = new Color(0.8f, 0.4f, 0.1f, 1f);
    6.        
    7.         //importation of the object corresponding to the button clicked
    8.         ImportFile importFile = GameObject.FindObjectOfType(typeof(ImportFile)) as ImportFile;
    9.  
    10.         // displays the loading logo
    11.         importFile.loading.SetActive(true);
    12.  
    13.         Debug.Log(name + " lxd archive clicked!");
    14.  
    15.        ....
    16.  
    17.         importFile.ImportObject(name);
    18.  
    19.         GameObject.Find(name).GetComponentInChildren<Image>().color = Color.white;
    20.         GameObject.Find(name).GetComponentInChildren<Text>().color = Color.black;
    21.  
    22.     }

    Do you have any idea about how can I display the logo before the execution of the function " ImportObject()" ?

    Thank you in advance ! ^-^
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Without a pause, your code just keeps going. You should call a coroutine from your OnPointerClick and after turning on importFile, call a yield to wait or just a yield null to pause a frame. This will set it active, then pause and let it render before continuing on with the rest of your code.
     
  3. Cherrygolo

    Cherrygolo

    Joined:
    Apr 27, 2021
    Posts:
    4
    Thank you for your response, I tried to modify my code like this :

    Code (CSharp):
    1. public void OnPointerClick(PointerEventData pointerEventData)
    2.     {
    3.         importFile = GameObject.FindObjectOfType(typeof(ImportFile)) as ImportFile;
    4.  
    5.         StartCoroutine(selectedEffects(name));      
    6.  
    7.         importFile.ImportObject(name);
    8.  
    9.     }
    10.  
    11.     IEnumerator selectedEffects(string name)
    12.     {
    13.         GameObject.Find(name).GetComponentInChildren<Image>().color = new Color(0.8f, 0.4f, 0.1f, 1f);
    14.         GameObject.Find(name).GetComponentInChildren<Text>().color = new Color(0.8f, 0.4f, 0.1f, 1f);
    15.  
    16.         importFile.loading.SetActive(true);
    17.  
    18.         Debug.Log(name + " lxd archive clicked!");
    19.  
    20.         DisplayMenu displayMenu = GameObject.FindObjectOfType(typeof(DisplayMenu)) as DisplayMenu;
    21.         displayMenu.ShowHideExamplesList();
    22.  
    23.         yield return new WaitForSeconds(3.0f);
    24.  
    25.     }
    But it changes nothing unluckily... Maybe I misunderstood your advice or tutorials on coroutines...
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    When the wait call happens, the code returns to the original method and still doesn't render your importFile.Loading before calling the next line of code.

    Try putting importFile.ImportObject(name); right after the yield within the coroutine. See if that helps.
     
  5. Cherrygolo

    Cherrygolo

    Joined:
    Apr 27, 2021
    Posts:
    4
    So I modified my code again :

    Code (CSharp):
    1. public void OnPointerClick(PointerEventData pointerEventData)
    2.     {
    3.         importFile = GameObject.FindObjectOfType(typeof(ImportFile)) as ImportFile;
    4.  
    5.         StartCoroutine(selectedEffects(name));      
    6.  
    7.     }
    8.  
    9.     IEnumerator selectedEffects(string name)
    10.     {
    11.         GameObject.Find(name).GetComponentInChildren<Image>().color = new Color(0.8f, 0.4f, 0.1f, 1f);
    12.         GameObject.Find(name).GetComponentInChildren<Text>().color = new Color(0.8f, 0.4f, 0.1f, 1f);
    13.  
    14.         importFile.loading.SetActive(true);
    15.  
    16.         Debug.Log(name + " lxd archive clicked!");
    17.  
    18.         DisplayMenu displayMenu = GameObject.FindObjectOfType(typeof(DisplayMenu)) as DisplayMenu;
    19.         displayMenu.ShowHideExamplesList();
    20.  
    21.         yield return new WaitForSeconds(2.0f);
    22.  
    23.         importFile.ImportObject(name);
    24.  
    25.     }
    And now the logo is displayed but ImportObject is not executed anymore...
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    After the yield finishes, ImportObject will execute. Unless you are interrupting the coroutine in some way.

    In which case, look for errors. Add Debug.Log statements in your code to see what is executing. I don't know what your ImportObject code looks like, but add those Debug.Log statements and see where it is executing to.