Search Unity

Possible 2017.3 Hololens/UWP image loading regression?

Discussion in 'AR/VR (XR) Discussion' started by cellarmation, Jan 4, 2018.

  1. cellarmation

    cellarmation

    Joined:
    Jan 17, 2015
    Posts:
    30
    I have a project that loads some image files jpg & png from the streaming assets folder at runtime which then uses them as textures. This works in the editor, emulator and Hololens on 2017.2 and prior versions. Since migrating to 2017.3 it has stopped working in the emulator and the Hololens device, but it still works in the editor.

    While trying to isolate the issue I have discovered that the WWW class appears to be successfully downloading the file (the coroutine returns, error=null, progess=1, isDone=true, keepWaiting=false and the length of bytes is correct) but the texture property is still null.

    This can be replicated in a default project by attaching the following (ImageLoader.cs) script to the camera. You would also have to make a StreamingAssets folder and add in a image named "Image.png". (you also need to have UWP support installed and do the basic configuration settings to build a Hololens project).

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using UnityEngine;
    5.  
    6. public class ImageLoader : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         StartCoroutine(LoadImage());
    11.     }
    12.  
    13.     IEnumerator LoadImage()
    14.     {
    15.         WWW www = new WWW("file:///" + Path.Combine(Application.streamingAssetsPath, "Image.png"));
    16.  
    17.         yield return www;
    18.  
    19.         Debug.Log("Texture: `" + www.texture + "` Error: `" + www.error + "`");
    20.     }
    21. }
    When run in the editor the following debug line is output:
    Texture: `(UnityEngine.Texture2D)` Error: ``

    When run in the Hololens emulator the following debug line is output:
    Texture: `` Error: ``

    Does anyone have any idea what I am doing wrong or is this a bug in Unity?
     

    Attached Files:

  2. CaptainOver

    CaptainOver

    Joined:
    Apr 3, 2014
    Posts:
    4

    I'm seeing the same thing. Odd thing is that you can't even inspect the WWW object when running in the debugger.
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    Code (CSharp):
    1. WWW www = new WWW("file:///" + Path.Combine(Application.streamingAssetsPath, "Image.png"));
    I think this will result in mixed slashes in the URI. Try fixing it to have proper forward slashes. You can pass the path to an System.Uri constructor and then take value of AbsoluteUri property.
     
  4. cellarmation

    cellarmation

    Joined:
    Jan 17, 2015
    Posts:
    30
    Thanks for the suggestion, you are right that it was causing it to have mixed slashes but making the change you suggested has not fixed the issue. The behaviour I am seeing is exactly the same, working in editor null texture in the Hololens&emulator. This is the replacement line that I used:

    Code (CSharp):
    1. WWW www = new WWW(new System.Uri(Path.Combine(Application.streamingAssetsPath, "Image.png")).AbsoluteUri);
     
  5. cellarmation

    cellarmation

    Joined:
    Jan 17, 2015
    Posts:
    30
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    file:// protocol has nothing to do with chunked transfer.
     
  7. cellarmation

    cellarmation

    Joined:
    Jan 17, 2015
    Posts:
    30
    I have re-tested this on 2017.3.1p1 and the results are still the same. The correct number of bytes download, no error is set but the texture is null and www.LoadImageIntoTexture() fails. Everything works in the editor, but not on the Hololens.

    Interestingly this does not seem to be specific to the file protocol, I see the exact same issue for images loaded over http. I tested with png, jpg and bmp image formats.
     
  8. ei2kpi

    ei2kpi

    Joined:
    Apr 26, 2014
    Posts:
    6
    Just hit this as well on 2017.4.1f1... Anyone know of a workaround? Upgrading to 2018 is not an option for me. :(
     
  9. KBVDev

    KBVDev

    Joined:
    Dec 1, 2017
    Posts:
    2
    Here are two options if you want to load from a local file:

    Depending on the folder you can use Resources.Load
    If you have a specific location and want to load textures you can use this, although I think its quite slow for big images..

    Code (CSharp):
    1.     /// <summary>
    2.     /// Loads Texture given a persisten data path.
    3.     /// </summary>
    4.     /// <param name="persistentDataPath">Starts with "C://", for example use Application.persistenDataPath</param>
    5.     public static Texture2D LoadTexture2D(string persistentDataPath)
    6.     {
    7.         Texture2D tex = null;
    8.         byte[] fileData;
    9.  
    10.         if (File.Exists(persistentDataPath))
    11.         {
    12.             tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
    13.             fileData = File.ReadAllBytes(persistentDataPath);
    14.  
    15.             // This will auto-resize the texture dimensions.
    16.             tex.LoadImage(fileData);
    17.  
    18.         }
    19.  
    20.         return tex;
    21.     }
    Then if you want to have a new Sprite lets say you can do this:

    Code (CSharp):
    1. Sprite icon = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0, 0));
    If you want to load from web maybe someone else has an idea?
     
    ei2kpi likes this.
  10. Merichbier

    Merichbier

    Joined:
    Apr 14, 2018
    Posts:
    7
    Hi,
    Was this bug fixed ? (i'm using unity 2018.2.11f)
    Because I encountred a situation where a plugin I'm using, may be affected by this bug
     
  11. cellarmation

    cellarmation

    Joined:
    Jan 17, 2015
    Posts:
    30
    I am currently using 2017.2.0f3 which doesn't have this issue. I still see this issue in my codebase if I build on 2017.3.1f1, but don't see it in 2017.4.15f1. I have not tested anything on 2018. I realise you posted this a while ago, so may no longer be interested in the answer but it might be interesting for someone else to find out.