Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Image as a Texture in HoloLens -Issue

Good evening,

Recently I was trying to create an app in Unity where with a button I could open an image or a pdf while the app remains open and running. I tried to do it with Unity, and I didn't see any problem. However, when I used the HoloLens emulator, every time I did it the app opens the file but then closes.

I thought that maybe if I defined several 2D Empty Objects (as Images, Raw Images...) the app would still run while doing this. To do this, I used the WWW class and the example of the Unity Scripting API.
https://docs.unity3d.com/ScriptReference/WWW.html
However, I don't know why when I run the app the Image doesn't change... I also tried the SetTexture() method it still doesn't work. So I was wondering if you have encountered the same problem, and if you could help me to solve it out.

Thanks a lot for your help!

Best Regards

Best Answer

  • mark_grossnicklemark_grossnickle ✭✭✭
    edited July 2018 Accepted Answer

    It may be the WWW class. Try switching to UnityWebRequestTexture.

    Here is what we use. Note, I am being lazy and not removing a bunch of extra stuff. You can remove the ImageFitter code, any scaling/alignment code, and replace our HasError check:

    '
    UnityWebRequest TextureRequest = null;
    protected IEnumerator LoadImageHelper(string url, float scale = 1)
    {
    var localScale = DefaultScale * scale;
    TextureRequest = UnityWebRequestTexture.GetTexture(url);
    TextureRequest.disposeDownloadHandlerOnDispose = true;
    TextureRequest.SetRequestHeader("Accept", "/");
    TextureRequest.SetRequestHeader("Accept-Encoding", "gzip, deflate");
    TextureRequest.SetRequestHeader("User-Agent", "runscope/0.1");
    yield return TextureRequest.SendWebRequest();
    while (!TextureRequest.isDone)
    {
    yield return 1;
    }
    if (!TQ3DUtils.HasError(TextureRequest))
    {
    var downloadHandler = ((DownloadHandlerTexture)TextureRequest.downloadHandler);
    LoadedTexture = downloadHandler.texture;

                var aspect = AspectMode;
                if(AspectMode == ImageFitter.UseLargest)
                {
                    if(LoadedTexture.width < LoadedTexture.height)
                    {
                        aspect = ImageFitter.HeightControlsWidth;
                    } else
                    {
                        aspect = ImageFitter.WidthControlsHeight;
                    }
                }
    
    
                float ratio = 1f;
                switch (aspect)
                {
                    case ImageFitter.HeightControlsWidth:
                        ratio = (float)LoadedTexture.width / (float)LoadedTexture.height;
                        localScale = Populate(localScale, ratio, WidthAxis);
                        localScale = Populate(localScale, 1, HeightAxis);
                        break;
                    case ImageFitter.WidthControlsHeight:
                        ratio = (float)LoadedTexture.height / (float)LoadedTexture.width;
                        localScale = Populate(localScale, 1, WidthAxis);
                        localScale = Populate(localScale, ratio, HeightAxis);
                        break;
                    case ImageFitter.None:
                        break;
                }
                ImageRenderer.transform.localScale = localScale;
                LoadedTexture.wrapMode = TextureWrapMode.Clamp;
                if (ImageRenderer is SpriteRenderer)
                {
                    var spriteRenderer = ImageRenderer as SpriteRenderer;
                    spriteRenderer.sprite = Sprite.Create(LoadedTexture, new Rect(0.0f, 0.0f, LoadedTexture.width, LoadedTexture.height), new Vector2(0.5f, 0.5f));
                }
                else
                {
                    ImageRenderer.material.mainTexture = LoadedTexture;
                }
                Debug.LogError("Loaded: " + LoadedTexture.wrapMode + " " + LoadedTexture.mipMapBias + " count: " + LoadedTexture.mipmapCount + " anso " + LoadedTexture.anisoLevel);
                ImageRenderer.material.color = Color.white;
                ImageRenderer.gameObject.SetActive(true);
    
                if (ratio != 1f && AlignmentControl != null)
                {
                    AlignmentControl.AlignmentUpdated();
                }
    
                if (OnImageLoaded != null)
                {
                    OnImageLoaded();
                }
            }
            else
            {
                ErrorManager.Instance.ShowError("Load Image Error: " + TextureRequest.error + ". Code: " + TextureRequest.responseCode);
            }
    
            // Don't dispose... it will remove the texture
            TextureRequest.Dispose();
            TextureRequest = null;
        }'
    

    Taqtile

Answers

  • mark_grossnicklemark_grossnickle ✭✭✭
    edited July 2018 Accepted Answer

    It may be the WWW class. Try switching to UnityWebRequestTexture.

    Here is what we use. Note, I am being lazy and not removing a bunch of extra stuff. You can remove the ImageFitter code, any scaling/alignment code, and replace our HasError check:

    '
    UnityWebRequest TextureRequest = null;
    protected IEnumerator LoadImageHelper(string url, float scale = 1)
    {
    var localScale = DefaultScale * scale;
    TextureRequest = UnityWebRequestTexture.GetTexture(url);
    TextureRequest.disposeDownloadHandlerOnDispose = true;
    TextureRequest.SetRequestHeader("Accept", "/");
    TextureRequest.SetRequestHeader("Accept-Encoding", "gzip, deflate");
    TextureRequest.SetRequestHeader("User-Agent", "runscope/0.1");
    yield return TextureRequest.SendWebRequest();
    while (!TextureRequest.isDone)
    {
    yield return 1;
    }
    if (!TQ3DUtils.HasError(TextureRequest))
    {
    var downloadHandler = ((DownloadHandlerTexture)TextureRequest.downloadHandler);
    LoadedTexture = downloadHandler.texture;

                var aspect = AspectMode;
                if(AspectMode == ImageFitter.UseLargest)
                {
                    if(LoadedTexture.width < LoadedTexture.height)
                    {
                        aspect = ImageFitter.HeightControlsWidth;
                    } else
                    {
                        aspect = ImageFitter.WidthControlsHeight;
                    }
                }
    
    
                float ratio = 1f;
                switch (aspect)
                {
                    case ImageFitter.HeightControlsWidth:
                        ratio = (float)LoadedTexture.width / (float)LoadedTexture.height;
                        localScale = Populate(localScale, ratio, WidthAxis);
                        localScale = Populate(localScale, 1, HeightAxis);
                        break;
                    case ImageFitter.WidthControlsHeight:
                        ratio = (float)LoadedTexture.height / (float)LoadedTexture.width;
                        localScale = Populate(localScale, 1, WidthAxis);
                        localScale = Populate(localScale, ratio, HeightAxis);
                        break;
                    case ImageFitter.None:
                        break;
                }
                ImageRenderer.transform.localScale = localScale;
                LoadedTexture.wrapMode = TextureWrapMode.Clamp;
                if (ImageRenderer is SpriteRenderer)
                {
                    var spriteRenderer = ImageRenderer as SpriteRenderer;
                    spriteRenderer.sprite = Sprite.Create(LoadedTexture, new Rect(0.0f, 0.0f, LoadedTexture.width, LoadedTexture.height), new Vector2(0.5f, 0.5f));
                }
                else
                {
                    ImageRenderer.material.mainTexture = LoadedTexture;
                }
                Debug.LogError("Loaded: " + LoadedTexture.wrapMode + " " + LoadedTexture.mipMapBias + " count: " + LoadedTexture.mipmapCount + " anso " + LoadedTexture.anisoLevel);
                ImageRenderer.material.color = Color.white;
                ImageRenderer.gameObject.SetActive(true);
    
                if (ratio != 1f && AlignmentControl != null)
                {
                    AlignmentControl.AlignmentUpdated();
                }
    
                if (OnImageLoaded != null)
                {
                    OnImageLoaded();
                }
            }
            else
            {
                ErrorManager.Instance.ShowError("Load Image Error: " + TextureRequest.error + ". Code: " + TextureRequest.responseCode);
            }
    
            // Don't dispose... it will remove the texture
            TextureRequest.Dispose();
            TextureRequest = null;
        }'
    

    Taqtile

Sign In or Register to comment.