I’m trying to load a local image and apply it to a UI.Image at runtime. The Question is that I can apply both texture2d and sprite to a Image in a similar way.
For texture2d:
var bytes = File.ReadAllBytes(imgPath);
var texture = new Texture2D(2, 2);
if (texture.LoadImage(buffer)){
image.material.mainTexture = texture;
}
For sprite:
var bytes = File.ReadAllBytes(imgPath);
var texture = new Texture2D(2, 2);
if (texture.LoadImage(buffer)){
var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite;
}
Both can achieve loading the task of loading local image, I wonder what is the difference and which way is better.