如何从资源文件夹中加载图像,并将该图像设置为 UI 图像的源图像?
如何从资源文件夹中加载图像,并将该图像设置为 UI 图像的源图像?
将您的精灵放置在以“资源”开头的任何文件夹中,例如我的是在“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
我尝试了@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;
It can work!