By using render texture and the code(Code1) below, I’m trying to render what main camera renders. The texture and the code work on Android and Unity Editor. However, they does not work on IOS meaning the render texture renderes nothing or is output as complete transparent. Is there anyone who have already addressed this issue before, would you give me some advice??
What I have tried
1, I used the codes shared below the first set of codes (code1). But the result was the same - they work on Android and Editor but on IOS.
2, have changed TextureFormat from ARGB32 to RGB24. The result was the same.
Environment
Unity2020.3.22f
Project type: URP
Target: Android & IOS
A test IOS device: Iphone SE (2nd gen)
IOS version: 15.4.1
Code1
private IEnumerator RenderTake(Image img)
{
RenderTexture renderTexure = new RenderTexture(Screen.width, Screen.height, 0);
Camera.main.targetTexture = renderTexure;
Camera.main.Render();
renderTexure.Create();
RenderTexture.active = renderTexure;
var tex = new Texture2D(renderTexure.width, renderTexure.height, TextureFormat.ARGB32, false);
tex.ReadPixels(new Rect(0, 0, renderTexure.width, renderTexure.height), 0, 0);
tex.Apply();
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
img.sprite = sprite;
img.preserveAspect = true;
yield return null;
Camera.main.targetTexture = null;
}
public IEnumerator Take(Image img)
{
yield return new WaitForEndOfFrame();
img.gameObject.SetActive(true);
var tex = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
//tex.alphaIsTransparency = true;
tex.Apply();
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
img.sprite = sprite;
img.preserveAspect = true;
}