Search Unity

Render a PNG Texture from URL onto Instantiated Cube

Discussion in 'VR' started by christopher-lanoue, Jan 26, 2017.

  1. christopher-lanoue

    christopher-lanoue

    Joined:
    Jun 22, 2016
    Posts:
    4
    I'm trying to render a `image/png` texture from a url onto a newly instantiated cube. It works great when I am testing in the Unity Editor, but the second I move over to the Hololens the texture is not rendering as I simply see the bright pink default texture.

    I've tried all the advice from the below linked discussion, but still no dice. InternetClient and InternetClientServer are enabled and I've tested the URL in the Hololens browser and it worked fine.
    https://forums.hololens.com/discussion/2702/texture-from-url-not-loading-in-hololens-and-emulator

    Am I missing an option in Package.appxmanifest or is this a known bug in the Hololens?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class ForumSample : MonoBehaviour
    7. {
    8.     void Start ()
    9.     {
    10.         StartCoroutine (retrieveImageAndAttachToGameObject ());
    11.     }
    12.  
    13.     IEnumerator retrieveImageAndAttachToGameObject ()
    14.     {
    15.         UnityWebRequest www = UnityWebRequest.GetTexture ("<url_to_png_image>", false);
    16.         www.SetRequestHeader ("Accept", "image/png");
    17.         Debug.LogError ("Downloading...");
    18.         yield return www.Send ();
    19.  
    20.         while (!www.isDone) {
    21.             Debug.LogError (".");
    22.             yield return null;
    23.         }
    24.  
    25.         if (www.isError) {
    26.             Debug.Log (www.error);
    27.         } else {
    28.             Debug.Log ("Getting the texture content");
    29.  
    30.             // Instantiate a cube
    31.             GameObject go = GameObject.CreatePrimitive (PrimitiveType.Cube);
    32.  
    33.             // Attach the browser image to the instantiated cube
    34.             go.GetComponent<Renderer> ().materials [0].mainTexture = DownloadHandlerTexture.GetContent (www);
    35.         }
    36.     }
    37. }
     
  2. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    Can you add a test to check whether GetContent() is returning null?
     
  3. christopher-lanoue

    christopher-lanoue

    Joined:
    Jun 22, 2016
    Posts:
    4
    Added now, but can't check on Hololens until tomorrow. Will respond then.
     
  4. christopher-lanoue

    christopher-lanoue

    Joined:
    Jun 22, 2016
    Posts:
    4
    The `Texture` object is not null and I'm getting correct width and height values from the downloaded texture `Debug.Log(DownloadHandlerTexture.GetContent(www).height.ToString())`
     
  5. christopher-lanoue

    christopher-lanoue

    Joined:
    Jun 22, 2016
    Posts:
    4
    It appears this line
    Code (CSharp):
    1. GameObject.CreatePrimitive (PrimitiveType.Cube);
    is the issue. I created a simple cube and then saved as a prefab and did
    Code (CSharp):
    1. Instantiate(Resources.Load("Cube") as GameObject, Vector3.zero, Quaternion.identity) as GameObject;
    and the texture rendered correctly. Could there be some issue with PrimitiveType?