How does unity convert RawImage's RenderTure image into Sprite
First, the purpose
Through the dynamic image capture under RawImage, a lot of images are generated and arranged in the UI.
2. RenderTure cannot be converted directly
1. First convert RenderTure to Texture2d
The code is as follows (example):
[SerializeField] RawImage Photo = null;
Texture2D texture2D = new Texture2D(Photo.texture.width, Photo.texture.height, TextureFormat.ARGB32, false);
RenderTexture.active = renderT;
texture2D.ReadPixels(new Rect(0, 0, Photo.texture.width, Photo.texture.height), 0, 0);
texture2D.Apply();
2. Then convert the obtained Texture2d into Sprite
The code is as follows (example):
Sprite sprite = Sprite.Create(texture2D,
new Rect(0, 0, Photo.texture.width, Photo.texture.height),
new Vector2(0.5f, 0.5f));
var targetTex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
var pixels = sprite.texture.GetPixels(
(int)sprite.textureRect.x,
(int)sprite.textureRect.y,
(int)sprite.textureRect.width,
(int)sprite.textureRect.height);
targetTex.SetPixels(pixels);
targetTex.Apply();
The data requested by the url network used here.