Search Unity

Text UI Element Not Updating As Expected

Discussion in 'UGUI & TextMesh Pro' started by landaras, Mar 12, 2019.

  1. landaras

    landaras

    Joined:
    Mar 12, 2019
    Posts:
    1
    I have a Button that launches a file processing task that sometimes takes up to 30 seconds to complete. I want to give the user feedback through a Text element, such as "Beginning processing for " + fileName + ".", as well as feedback when the task is done such as "Processing complete for " + fileName + "." This will be especially important for another Button in the same program that will process a whole folder of files at once.

    Unfortunately, I never see the Beginning Processing update to the Text element. When a user clicks the button, the program appears to just freeze with the button visibly pressed in until processing is complete. Once processing is complete, the Processing Complete update occurs in the Text element.

    I've commented out the ProcessFile() line and the Processing Complete update. In those circumstances, I can see the Beginning Processing update.

    I've added the Sleep commands and a Debug.Break to try to slow things down for a second to make sure my human eyes can see and process the change if it happens quickly. However, I'm not seeing the Beginning Processing text when I add ProcessTheFile() back in, even when everything is paused one line after the Beginning Processing text is supposed to update.

    The script below is attached to the Button that processes a single file, and the Text element is associated with the script through the Inspector.

    Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using Crosstales.FB;
    5. using UnityEngine.EventSystems;
    6. using System.IO;
    7. using System.Diagnostics;
    8. using System;
    9. using System.Threading;
    10.  
    11. public class ProcessFile : MonoBehaviour, IPointerClickHandler
    12. {
    13.     public Text status;
    14.  
    15.     public void OnPointerClick(PointerEventData pointerEventData)
    16.     {    
    17.         // obtain a single file name by launching a file browser set to single
    18.         // file, restricted to those with the pdf extension
    19.         // the FileBrowser asset brings in the path with forward slashes instead of backslashes
    20.         // so we replace them with backslashes to be consistent with the local disk OS' style
    21.         string incomingPath = FileBrowser.OpenSingleFile("pdf").Replace('/', '\\');
    22.  
    23.         if (incomingPath != "")
    24.         {
    25.             Thread.Sleep(1000);
    26.             status.text = "Beginning processing...";
    27.             UnityEngine.Debug.Break();
    28.             Thread.Sleep(1000);
    29.         }
    30.         else
    31.         {
    32.             status.text = "";
    33.         }
    34.  
    35.       // file name and path manipulation lines removed for clarity
    36.  
    37.         Thread.Sleep(1000);
    38.         ProcessTheFile(incomingPath, destinationPath);
    39.         Thread.Sleep(1000);
    40.          
    41.         status.text = "Processing complete for " + fileName + ".";      
    42.     }
    43. }
    44.