8

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

4

2 回答 2

13

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

[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

于 2016-03-01T08:52:22.427 回答
2

我尝试@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;

它可以工作!

于 2020-08-20T09:15:10.713 回答