I used multiple camera in my game to render output in render texture in UI (RawImage). It perfectly worked in editor and some android phone. But after testing in different mobile phone I found out that it does not work in all android devices. I create a small project to test it and after build, it does not work; the problem still exists in android devices.
I uploaded test project and sample images in google drive (Test Project and Sample Images). I follow mini map tutorial from Brackeys to make this test project.
I also found this thread but it does not work in this test. Can any body give me solution of this problem.
Sample Test image Sample Test
Solution
Dragging and dropping Render Texture does not work like in this tutorial for mobile build. Solution is to read pixels in render texture which is targeted in camera properties and set that texture into RawImage.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CaptureImage : MonoBehaviour {
[SerializeField] Camera camera;
[SerializeField] RawImage rawImage;
[SerializeField] RenderTexture renderTexture;
private void Update() {
capture();
}
private void capture() {
RenderTexture activeRenderTexture = RenderTexture.active;
RenderTexture.active = camera.targetTexture;
camera.Render();
Texture2D image = new Texture2D(renderTexture.width, renderTexture.height);
image.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
image.Apply();
RenderTexture.active = activeRenderTexture;
rawImage.texture = image;
}
}
Obviously this will decrease the FPS; dragging and dropping render texture will have max 200FPS (following Brackeys tutorial) but after using this script it will go down to max 145 FPS (tested in unity editor stats) good part is it will work in android.
How to use this script is here (2)
Note: I m using google drive to upload images coz I can’t upload image in this platform shows error while uploading image.