Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Troubles with rawImage (trying to show some Texture2D)

Discussion in 'UGUI & TextMesh Pro' started by Thibault_potier, Aug 26, 2021.

  1. Thibault_potier

    Thibault_potier

    Joined:
    May 4, 2021
    Posts:
    46
    Hi

    I'm using rawImages to show some texture2D in my UI.

    The purpose is to browse some textures on the computer and apply it to my mesh. meanwhile i'm drawing the Texture2D on a rawImage to visualize it.

    My new texture apply correctly on my mesh, but doesn't show on my rawImage.


    If I make a texture2D with a .png image per example, this texture2D will apply correctly on the mesh, and it will applay correctly on the rawImage.

    If I make a textur2D with a .tga file, it will still works on the mesh, but my rawImage will be set transparent :(

    see:

    On the left I have a list of all the materials used on the mesh. I can select a new image on the computer to replace the current texture:

    Here I will replace the texture for the "hull" material. I will select a png texture :


    It works perfectly.
    Now same thing with a .tga texture :


    As you can see it works fine on the model but the rawImage did not liked it. Here is my code :

    upload_2021-8-26_10-17-18.png

    Code (CSharp):
    1. public Texture2D LoadTextureFromPath(string path)
    2.     {
    3.         Debug.Log("Loading " + path);
    4.  
    5.         if (path != null)
    6.         {
    7.             Texture2D newTexture = new Texture2D(2,2);
    8.  
    9.             if (path.Contains(".tga"))
    10.             {
    11.                 Debug.Log("this is a tga texture");
    12.                 newTexture = TGALoader.LoadTGA(path);
    13.             }
    14.             else
    15.             {
    16.                 Debug.Log("this is not a tga texture");
    17.                 byte[] byteArray = File.ReadAllBytes(path);
    18.                 newTexture.LoadImage(byteArray, false);
    19.             }
    20.  
    21.             newTexture.Apply();
    22.  
    23.             return (newTexture);
    24.         }
    25.         else
    26.         {
    27.             Debug.Log("Failed to load image at path: " + path);
    28.             return null;
    29.         }
    30.     }
    Code (CSharp):
    1. // Invoked when clicking on item button
    2.     public void setTexture()
    3.     {
    4.         string path = ViewerManager.instance.selectTexturePath();
    5.  
    6.         Texture2D newTexture = ViewerManager.instance.LoadTextureFromPath(path);
    7.  
    8.         //apply new texture on the model material (this works -> model change appearance in game view)
    9.         ViewerManager.instance.materialsDict[materialName.text].SetTexture("_MainTex", newTexture);
    10.  
    11.         m_material = ViewerManager.instance.materialsDict[materialName.text];
    12.  
    13.         m_RawImage.texture = newTexture; // this doesn't works : rawImage become transparent
    14.     }
    Any ideas on this ? Or alternative ways to proceed ?

    Any help appreciated, thanks
     
  2. Thibault_potier

    Thibault_potier

    Joined:
    May 4, 2021
    Posts:
    46
    JG-Denver likes this.
  3. JG-Denver

    JG-Denver

    Joined:
    Jan 4, 2013
    Posts:
    77
    It doesn't matter now, but I was just writing to you. You are posted in the wrong forum, you want to post this in UGUI & TextureMesh Pro forum. RawImage is part of the original UGUI, not the UI Toolkit which is new. Probably get better answers there for any future issues like this.
     
    Thibault_potier likes this.
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    733
    I will move the thread.