Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load image from resource folder and set it to source image of UI Image in unity5

Tags:

unity3d

unity5

How to load image from resource folder and set that image to source image of UI Image?

like image 465
RaviG Avatar asked Jul 30 '15 09:07

RaviG


2 Answers

Place your sprite in any folder that begins with resources, e.g. mine is in "Resources/Images/test"

[SerializeField] private UnityEngine.UI.Image image = null;

private void Awake()
{
    if( image != null )
    {
        image.sprite = Resources.Load<Sprite>( "Images/test" );
    }
}

http://docs.unity3d.com/462/Documentation/ScriptReference/UI.RawImage.html http://docs.unity3d.com/ScriptReference/Resources.Load.html

like image 182
ananonposter Avatar answered Oct 06 '22 10:10

ananonposter


I try @ananonposter answer but fail.But I try another way by below code:

var image = new Image();
var tex = Resources.Load<Texture2D>("Sprites/transparent");
var sprite = Sprite.Create(tex, new Rect(0.0f,0.0f,tex.width,tex.height), new Vector2(0.5f,0.5f), 100.0f);
image.sprite = sprite;

It can work!

like image 32
Linker Avatar answered Oct 06 '22 10:10

Linker