Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading from streaming assets with UnityWebRequest fails on mac

Discussion in 'macOS' started by OndraPaska, May 2, 2020.

  1. OndraPaska

    OndraPaska

    Joined:
    Jul 5, 2014
    Posts:
    12
    Code (CSharp):
    1. string path = Application.streamingAssetsPath + Path.DirectorySeparatorChar + "img.jpg";
    2.         Texture2D texture = null;
    3.         using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path))
    4.         {
    5.             yield return uwr.SendWebRequest();
    6.             if (uwr.isNetworkError || uwr.isHttpError)
    7.             {
    8.                 Debug.LogError("Not found image at " + path);
    9.                 Debug.Log(uwr.error);
    10.             }
    11.             else
    12.             {
    13.                 texture = DownloadHandlerTexture.GetContent(uwr);
    14.             }
    15.         }
    I thought this was the way to load a texture from streaming assets. It works on windows, but on Mac I get "Curl error 7: Failed to connect to localhost port 80: Connection refused".
    Does someone have a solution?

    I just need to get an image from streaming assets on all platforms.

    Thanks
     
  2. felo1991

    felo1991

    Joined:
    Sep 24, 2019
    Posts:
    1
    I'm having the same error, did you find the solution?
     
  3. OndraPaska

    OndraPaska

    Joined:
    Jul 5, 2014
    Posts:
    12
    I ended up with this, it seems to work fine
    Code (CSharp):
    1.     Texture2D texture = null;
    2.        
    3. #if UNITY_STANDALONE_OSX
    4.         var fileData = File.ReadAllBytes(path);
    5.         texture = new Texture2D(2,2);
    6.         texture.LoadImage(fileData);
    7. #else
    8.         using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path))
    9.         {
    10.             yield return uwr.SendWebRequest();
    11.             if (uwr.isNetworkError || uwr.isHttpError)
    12.             {
    13.                 Debug.Log("Failed to load:"+path+" error:"+uwr.error);
    14.             }
    15.             else
    16.             {
    17.                 texture = DownloadHandlerTexture.GetContent(uwr);
    18.             }
    19.         }
    20. #endif
    21.        
    22.        
    23.         img.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
    24.      
     
  4. andrews_unity

    andrews_unity

    Unity Technologies

    Joined:
    Dec 11, 2015
    Posts:
    264
    This seems like a regression can you file a bug.
     
  5. tonybf

    tonybf

    Joined:
    Dec 24, 2017
    Posts:
    9
    I get this error on Android too, i am currently using 2019.4.1f1
     
  6. andrews_unity

    andrews_unity

    Unity Technologies

    Joined:
    Dec 11, 2015
    Posts:
    264
    Can you please file a bug for this
     
  7. Dance_M

    Dance_M

    Joined:
    Aug 17, 2014
    Posts:
    15
    Same here (Unity 2020.1.4f1 and Android app). Solved by using "file://" (or "file:///") in the path:
    var filesPath = "file://" + Application.persistentDataPath + "/" + fileName;
     
    Last edited: Oct 20, 2020
  8. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I was using a plugin to get the file path from the android folder but was not working.Getting the error as shown below
    "Error Unity Curl error 7: Failed to connect to localhost port 80: Connection refused for UnityWebRequestMultimedia.GetAudioClip"

    Finally got by using the following code
    Code (CSharp):
    1. using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://"+audioPath, AudioType.MPEG))
    So have to add the "file://" in front of the path .Using Unity 2019.4.17f1.
     
  9. limbicfishnet

    limbicfishnet

    Joined:
    Dec 27, 2016
    Posts:
    1
    Yes! adding "file://" + to the file path did it for me too. I have been struggling with this for days. Thanks for this @Dance_M :)
     
    Dance_M likes this.
  10. c3bgames

    c3bgames

    Joined:
    Jun 14, 2020
    Posts:
    3
    Thank you!! I ran into this same problem in 2021.1.10f1, and adding "file://" fixed the problem.
     
    Dance_M likes this.
  11. Xevoss

    Xevoss

    Joined:
    Jul 12, 2014
    Posts:
    5
    I can't thank you enough @Dance_M since that was my problem as well
     
    Dance_M likes this.
  12. Dan_UA_

    Dan_UA_

    Joined:
    May 12, 2020
    Posts:
    1
    @Dance_M Man i want to say thank you , a lot !!!
     
    Dance_M likes this.
  13. MagroPlay

    MagroPlay

    Joined:
    Sep 16, 2020
    Posts:
    1
    This is the solution! just add the Exception before doing the Web Request for MacOSX
    Code (CSharp):
    1. #UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
    2.         path = Path.Combine("file://",path);
    3. #endif
     
    Dance_M likes this.
  14. blackroseamare

    blackroseamare

    Joined:
    Nov 1, 2023
    Posts:
    1
    God bless you! This solution seems to work for 2022.3 on Mac!
     
    Dance_M likes this.