Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Help with dynamic coroutine to update a class

Discussion in 'Scripting' started by joaquinlen, Sep 4, 2020.

  1. joaquinlen

    joaquinlen

    Joined:
    Jun 22, 2020
    Posts:
    1
    Hello, I am creating a dynamic widget based on XML. Basically you open the widget of an author based on XML. In that string you have a tag which instantiates a prefab to display the gallery, which items on the XML are the type of the tag opened previously.
    Up to this point everything works, but I have the following problem: the images of the gallery have to be downloaded from the internet, the path to each image is on the XML. But I can't manage to update the image when it downloads, this is what I've got:

    Code (CSharp):
    1.  
    2. public class IItems
    3. {
    4.     public string itemID { get; private set; }
    5.     public string itemType { get; private set; }
    6.     public string title { get; private set; }
    7.     public string imagePath { get; private set; }
    8.     public Texture2D texture { get; private set; }
    9.  
    10.     public IItems(XmlNode node)
    11.     {
    12.         itemID = node.Attributes["ID"].Value;
    13.         itemType = node.Attributes["Type"].Value;
    14.         title = node["ObraTitle"].InnerText;
    15.         imagePath = node["ImagePath"].InnerText;
    16.     }
    17.  
    18.     public void UpdateInventoryUI(GameObject inventoryUI)
    19.     {
    20.         UITestScript.Instance.Run(GetImage(imagePath));
    21.  
    22.         Transform inventoryUITransform = inventoryUI.transform;
    23.  
    24.         Image itemBGPanel;
    25.         Text itemTitleText;
    26.         RawImage itemRawImage;
    27.  
    28.         itemBGPanel = inventoryUITransform.GetComponent<Image>();
    29.         Transform itemBGPanelTransform = itemBGPanel.GetComponent<Transform>();
    30.         itemTitleText = itemBGPanelTransform.Find("TitleText").GetComponent<Text>();
    31.         itemTitleText.text = title;
    32.         itemRawImage = itemBGPanelTransform.Find("ItemRawImage").GetComponent<RawImage>();
    33.  
    34.         itemRawImage.texture = texture;
    35. //I NEED THIS TO BE THE IMAGE DOWNLOADED ON THE IENUMERATOR
    36.     }
    37.  
    38.     IEnumerator GetImage(string path)
    39.     {
    40.         UnityWebRequest wwwImage = UnityWebRequestTexture.GetTexture(path);
    41.         var operation = wwwImage.SendWebRequest();
    42.         while (!wwwImage.isDone) //este codigo se va a loopear hasta que la textura se termine de descargar y va a traer la info de qu eesta haciendo unitywebrequest
    43.         {
    44.             var progress = operation.progress;
    45.  
    46.             UnityEngine.Debug.Log(progress);
    47.             yield return null;//salto al otro frame para que vuelva a empezar si el www aun no es Done
    48.         }
    49.         yield return wwwImage;
    50.         Texture2D image = ((DownloadHandlerTexture)wwwImage.downloadHandler).texture;
    51.         if (wwwImage.isDone)
    52.         {
    53.             Rect rect = new Rect(0, 0, image.width, image.height);
    54.  
    55.             texture = image;
    56.         }
    57.     }
    58.  
    59. }
    so I don't exactly know how to update the raw image to the instantiated object :( thank you!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Why not just move this code:
    Code (CSharp):
    1.         itemRawImage = itemBGPanelTransform.Find("ItemRawImage").GetComponent<RawImage>();
    2.         itemRawImage.texture = texture;
    to the end of your coroutine?