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

Question WebRequest doesn't work after build on Android

Discussion in 'Editor & General Support' started by afurioso, Aug 31, 2022.

  1. afurioso

    afurioso

    Joined:
    Jan 12, 2022
    Posts:
    88
    Hi fellas,

    I'm trying to download images from the cloud through UnityWebRequest. If I play it in the editor it works fine and I don't get any errors. But, after I build it for Android my app doesn't show me anything.

    How can I solve it?
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    The first thing you should try is to log everything related to your web request. Check for URI if it is correct, response status and, if succeeded, check the string result.

    Code snippet would be also helpful, otherwise we can only guess what went wrong
     
  3. afurioso

    afurioso

    Joined:
    Jan 12, 2022
    Posts:
    88

    As I said there is no error in the editor, but I will check again.

    Also, I'm using this function in an Enumeretor and StartCourtrutine in a for loop to retrieve images from Firebase: this is the code.

    Code (CSharp):
    1. public class GeneratingTest : MonoBehaviour
    2. {
    3.  
    4.     public Transform itemContent;
    5.     public GameObject inventoryItem;
    6.  
    7.     FirebaseFirestore db;
    8.  
    9.  
    10.     private Sprite mySprite;
    11.     Texture2D myTexture;
    12.  
    13.     GameObject obj;
    14.     private string fileExstesnsion;
    15.  
    16.     private void Awake()
    17.     {
    18.         db = FirebaseFirestore.DefaultInstance;
    19.  
    20.         // Create a reference from a Google Cloud Storage URI
    21.         Query allCompanies = db.Collection("companies");
    22.         allCompanies.GetSnapshotAsync().ContinueWithOnMainThread(task =>
    23.         {
    24.             QuerySnapshot allCompaniesSnapshot = task.Result;
    25.             foreach (DocumentSnapshot documentSnapshot in allCompaniesSnapshot.Documents)
    26.             {
    27.  
    28.                 Query allProducts = db.Collection("companies").Document(documentSnapshot.Id).Collection("products");
    29.                 allProducts.GetSnapshotAsync().ContinueWithOnMainThread(task =>
    30.                 {
    31.                     QuerySnapshot allProductsSnapshot = task.Result;
    32.                     foreach (DocumentSnapshot documentSnapshot in allProductsSnapshot.Documents)
    33.                     {
    34.  
    35.                         obj = Instantiate(inventoryItem, itemContent);
    36.                         Dictionary<string, object> product = documentSnapshot.ToDictionary();
    37.                         foreach (KeyValuePair<string, object> pair in product)
    38.                         {
    39.                            
    40.  
    41.                             if (pair.Key == "productImage")
    42.                             {
    43.                                 var imageToView = (string)pair.Value;
    44.                                 StartCoroutine(GetImage(imageToView));
    45.                             }
    46. }
    47.                             }
    48.                 });
    49.             }
    50.         });
    51.     }
    52.  
    53.     public IEnumerator GetImage(string image)
    54.     {
    55.         var itemIcon = obj.transform.Find("Icon").GetComponent<Image>();
    56.  
    57.         UnityWebRequest www = UnityWebRequestTexture.GetTexture(image);
    58.  
    59.         yield return www.SendWebRequest();
    60.  
    61.         myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
    62.  
    63.         mySprite = Sprite.Create((Texture2D)myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(.5f, .5f));
    64.  
    65.         itemIcon.sprite = mySprite;
    66.         yield return itemIcon.sprite;
    67.     }
    68. }
     
  4. ErezShahaf

    ErezShahaf

    Joined:
    Feb 3, 2022
    Posts:
    70
    Have you found the solution? I am experiencing the same thing
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,639
    Check the logcat. I also recommend adding a log in GetImage() to print out the image uri that you pass to UnityWebRequest. And for sure add error checking after the download.
     
  6. afurioso

    afurioso

    Joined:
    Jan 12, 2022
    Posts:
    88
    Hi, for me it was just a build problem solved by correcting the connection with the database