I’m running into an issue with RenderTexture, on iOS-only builds.
I am doing a 2-pass render to the RenderTexture from 2 different cameras (code below).
The first pass looks like this:
But on iOS, the second pass looks like this:
The second pass completely obliterates the first pass.
I do not have this problem on Android/PC/Mac. Only iOS, newer hardware. It works fine on an old iPod Touch from 2012.
This is what it looks like on Android:
And here is the code that does the two-pass render-to-texture:
IEnumerator RenderScreenshot()
{
yield return new WaitForEndOfFrame();
if (mInGameMenu == null)
yield break;
// The main camera is not showing a texture
{ // Render the main camera
Camera mainCam = Camera.main;
mainCam.targetTexture = Game.Instance.Script.ScreenshotTexture;
if (mainCam.targetTexture != null)
mainCam.Render();
mainCam.targetTexture = null;
}
// The NGUI layer renders fine
{ // Render the NGUI layer, with the screenshot elements
GameObject nguiCam = GameObject.Find("UI Root/Camera");
Camera uiCam = nguiCam.GetComponent<Camera>();
uiCam.targetTexture = Game.Instance.Script.ScreenshotTexture;
if (uiCam.targetTexture != null)
uiCam.Render();
uiCam.targetTexture = null;
uiCam.depth = 0; // Restore the camera depth
}
{ // Create the texture from the RenderTexture
RenderTexture screenshot = Game.Instance.Script.ScreenshotTexture;
RenderTexture.active = screenshot;
int width = screenshot.width;
int height = screenshot.height;
Texture2D tex = new Texture2D(width, height);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
Persistence.Instance.Mobile.Screenshot = tex; // Save the screenshot
}
}
// Screenshot logic is in UI, because we use the UI to render the watermark.
private void TakeScreenshot()
{
// In case this is pertinent...
GameObject nguiCam = GameObject.Find("UI Root/Camera");
Camera cam = nguiCam.GetComponent<Camera>();
cam.depth = -99; // Hide the camera behind the other cameras
...
// Use a coroutine to render a screenshot, because we have to wait
// for the frame to finish rendering first.
StartCoroutine("RenderScreenshot");
}
Any ideas for fixes or workarounds? I’m almost convinced that this is a Unity bug…
Using Unity 5.3.1f1
EDIT:
And this is the related RenderTexture thread:
http://forum.unity3d.com/threads/rendertexture-not-working-on-ios-and-android-unity-4-2-0f4.192561/page-2#post-2498203