Search Unity

Correct way to update/change RawImage texture?

Discussion in 'UGUI & TextMesh Pro' started by BlueOnE, Mar 13, 2015.

  1. BlueOnE

    BlueOnE

    Joined:
    Jan 24, 2015
    Posts:
    16
    Hi,
    First off, I want sure if this is applicable for the UI or Scripting sections in the forum, but here goes:

    This might be a silly question, but I am trying to change the texture in my UI/HUD's RawImage element.
    After a lot of googling and trying, the only thing I got working was the following code:
    Code (CSharp):
    1.  
    2.  UIicon = GameObject.Find ("UI-holditem").GetComponent<RawImage>();
    3.  Texture2D othericon = (Texture2D) AssetDatabase.LoadAssetAtPath("Assets/Textures/UI/item_icon_2.png", typeof(Texture2D));
    4.  UIicon.texture = othericon;
    5.  
    Although it seems to work fine, I got mixed feelings when I needed to import UnityEditor for the AssetDatabase to work.
    All I'm wondering about is if this is a valid way to load an image and use that as a RawImage texture?
    For some reason it doesn't feel entirely right to me.

    Would love to hear some feedback on this, thanks so far.
     
  2. iivo_k

    iivo_k

    Joined:
    Jan 28, 2013
    Posts:
    314
    That's only gonna work in the editor. Put the texture in a resources folder, if you want to load it by name using Resources.Load.
     
  3. BlueOnE

    BlueOnE

    Joined:
    Jan 24, 2015
    Posts:
    16
    awesome, you confirmed my suspicions. thanks for the info.
    Now of to rearange my folder structure to properly use this resources folder :s
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Alternately you can get the byte stream of the image and create a new Texture2D from it. This is also how you use it for images downloaded from the web with www
     
  5. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    You dont need a RawTexture...

    Code (CSharp):
    1. public Sprite otherSprite;
    2.     public Texture2D otherTexture;
    3.  
    4.     void Start () {
    5.         Invoke ("ChangeTexture", 1f);
    6.  
    7.     }
    8.  
    9.     void ChangeTexture()
    10.     {
    11.      
    12.         //use this if its texture2D
    13.         Vector2 pivot = new Vector2(0.5f, 0.5f);
    14.         Rect tRect = new Rect(0,0, otherTexture.width, otherTexture.height);
    15.         GetComponent<Image>().overrideSprite = Sprite.Create( otherTexture, tRect, pivot);
    16.      
    17.         //use this if its sprite  
    18.         GetComponent<Image>().overrideSprite = otherSprite;
    19.     }
     
    SimonDarksideJ likes this.
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    That is an interesting way of creating a sprite from a texture @psyydack
    Although I'd have to wonder why you would (apart from avoiding issues with the RawTeture UI Component :D)
     
  7. mellovely

    mellovely

    Joined:
    Jan 31, 2017
    Posts:
    21
    I know this is an old post, but Im having a similar issue.
    In my case I am trying to change the movieTexture on my Raw Image when I press a button. I have a few buttons and a few videos, Im trying to make it so that if I press button A then video A plays, button B video B plays and so on.
    I have the following code which I have added to every button, on the inspector I have given each button a different video to play. But the only video that will play is the one that is originally a movie texture of the raw image (video A) so I click button A the video plays fine, but if I press button B nothing happens, the movie texture doesn't change and nothing plays

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class VideoController : MonoBehaviour
    6. {
    7.    
    8.      public MovieTexture video;
    9.    
    10.      public void OnMouseDown()
    11.      {
    12.          GetComponent<Renderer>().material.mainTexture = video as MovieTexture;
    13.          if (!video.isPlaying)
    14.          {
    15.              video.Stop();
    16.          }
    17.        
    18.              video.Play();
    19.        
    20.        
    21.      }
    22.    
    23.      void Update()
    24.      {
    25.        
    26.      }
    27. }