如何从资源文件夹加载图像并将其设置为unity5中UI图像的源图像

时间:2015-07-30 09:21:26

标签: unity3d unity5

如何从资源文件夹加载图像并将该图像设置为UI Image的源图像?

2 个答案:

答案 0 :(得分:6)

将您的精灵放在任何以资源开头的文件夹中,例如我的资源/图像/测试"

[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

答案 1 :(得分:1)

我尝试使用@ananonposter答案,但失败。但是我尝试通过代码下面的另一种方法:

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;

可以正常工作!