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

UnityWebRequest broken on MacOS?

Discussion in 'Scripting' started by Zdravko_FRG, Jul 18, 2019.

  1. Zdravko_FRG

    Zdravko_FRG

    Joined:
    Jun 10, 2019
    Posts:
    3
    Hi,
    Encountered a strange bug today: images weren't properly loading in our game, but only on Mac hardware.

    The images are first loaded from a web server, then stored locally for faster access.
    Fetching the images (partial code):

    Code (CSharp):
    1.  
    2. using (var webRequest = UnityWebRequestTexture.GetTexture(url))
    3.         {
    4.             yield return webRequest.SendWebRequest();
    5.  
    6.             if (ErrorUtl.CheckWebRequestError(webRequest))
    7.             {
    8.             [error handling code here, return out of the method]
    9.             }
    10.             [success code here]
    11. }
    12.  

    Input to this bit of code is the "url" variable. When we're fetching from server, url is an external web adress, everything goes OK and we're in success code territory.

    However, when we want to load a locally cached image, the url points to a local file, and starts with "/var/folders..." on Mac OS.
    After the using statement, using the debugger tells me that webRequest has interpreted this as:
    Screenshot 2019-07-18 at 16.24.09.png
    which of course, does not exist and will throw an error.
    (putting this as an image because Unity forums do not let me post this in text because it's too, quote, "spam-like"? wtf?)


    The temporary (and very ugly fix) for this so far was:

    Code (CSharp):
    1.  
    2. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
    3.                 url = url.Insert(0, "file://");
    4. #endif
    5.  
    While this does fix the problem, it's hacky and ugly solution. I'm wondering if there's another alternative method I'm not seeing. Or is this a bug in Unity's WebRequest code?

    This is on Unity 2018 LTS.

    Thanks for any help!
     
    neoRiley and turbolek like this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,722
    It's not a bug - UnityWebRequest takes URI as an argument, not file path.
    A simple fix is to create a Syste.Uri object passing path to it's constructor, then pass an Uri object to UWR.
     
  4. Zdravko_FRG

    Zdravko_FRG

    Joined:
    Jun 10, 2019
    Posts:
    3
    But it works on Windows. If the supplied URL starts with "C:/" or similar, it works.
    It's just on Mac where it becomes a problem.
    I'll take a look at System.Uri object.
     
    turbolek likes this.
  5. neoRiley

    neoRiley

    Joined:
    Dec 12, 2008
    Posts:
    158
    Might be a hack, but it works.