Search Unity

UnityWebRequest and local area network

Discussion in 'Scripting' started by Quent01120, Jul 22, 2019.

  1. Quent01120

    Quent01120

    Joined:
    Apr 1, 2017
    Posts:
    3
    Hello,

    I'm in enterprise environnement for develop a solution on Unity 3D. My document are in local area network (on a NAS). And I want to read image in this folder and load it in my solution.

    Here a sample of what I want do :

    Code (CSharp):
    1.  
    2.  
    3. // string file = "file://C:/Medias/m_7.jpg"; => WORK
    4. string file = "file://192.168.1.150/MyDoc/Medias/m_7.jpg"; // => NOT WORK
    5.  
    6. Texture tex = null;
    7.  
    8. Uri uri = new Uri(file);
    9.  
    10. using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(uri))
    11. {
    12.     yield return uwr.SendWebRequest();
    13.     if (string.IsNullOrEmpty(uwr.error))
    14.     {
    15.         tex = DownloadHandlerTexture.GetContent(uwr);
    16.     }
    17.     else
    18.     {
    19.         Debug.Log(uwr.error);
    20.     }
    21.  
    22. }
    23.  
    The adress server is a sample but look like of what I have here.

    I can load texture on local drive or on the internet with "UnityWebRequestTexture.GetTexture()" but the method seems not work when the file is on the local network.

    Did I miss something ?
     
  2. Lysander

    Lysander

    Joined:
    Feb 24, 2013
    Posts:
    1,650
    I can't really comment on the LAN aspect specifically, but from what I recall about issues with "file://" I've seen around previously, in Windows you actually need to use three /// slashes- two will work in some instances but not others. Can you just quickly verify if that fixes it or not? Hopefully someone with more familiarity with this issue than I will be by soon, if not.
     
  3. Quent01120

    Quent01120

    Joined:
    Apr 1, 2017
    Posts:
    3
    Thank you,
    I test with three slashes and also on a mac or Android I want load resources with this way... If someone has an idea my previous code was based on WWW which is obsolete now.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,248
    The part about three slashes is not correct. The slashes are required on Windows for local files, for network stuff two is actually correct.
    file protocol for non-local files is in general not well defined. Try using computer name instead of IP address. Another thing you can try is smb protocol instead of file, should work for Windows/Samba shares, but be aware that this is not supported across all platforms (should be ok on desktops though).
     
    Lysander likes this.
  5. Quent01120

    Quent01120

    Joined:
    Apr 1, 2017
    Posts:
    3
    Thank you,

    We find the problem we must put a 4rd slash :

    "file:////192.168.1.150/MyDoc/Medias/m_7.jpg"

    It's works !!

    smb and computer name doesn't work.

    Thanks for the help !
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    2,248
    Now when you wrote it, I did remember it: the conforming URI actually requires file slashes, four is a "relaxed version" :)
    Basically it's like this:
    - you have an absolute path, which is \\192.168.1.150\MyDoc\...
    - you replace backslashes with forward slashes, so you get //192.168.1.150/MyDoc/...
    - now you have to make it to the absolute path in a UNIX form, which means prepending another slash (because "//192.168.1.150" is a directory name in this case), so it's now ///192.168.1.150/MyDoc/...
    - finally you prepend the file:// shame to it: file://///192.168.1.150/MyDoc/...

    Looks very weird, but that's what it is. Though I think our code supports the relaced form with four slashes just fine (added along with local files with just two slashes due to a very common "mistake").