Is there a recommended method of restoring lost rendertextures after a suspend/resume?
This seems to be an issue on Android, too: http://forum.unity3d.com/threads/242550-Android-OnApplicationPause()-Render-Texture-issue
Using OnApplicationPause would seem the obvious way to attempt to deal with this, but so far I’m having no success either - is there a timing issue? When after a resume does it become safe to render?
(It’d be nice if suspend/resume could be simulated in the editor, along with discarding rendertextures in the way that it happens on Android/WP8 )
Doing it during OnApplicationPause is the way to go. What kind of issues are you facing when you restore them when OnApplicationPause gets called?
P. S. You don’t need to “Recreate them”, as Unity does that for you. You only need to render to them again to fill their contents back.
Hmm, after a bit more debugging+experimenting, I seem to have it working, and doesn’t look like the same problem described in that Android thread.
I’d been trying to reallocate the RenderTextures entirely - ‘just in case’ - with no success. I suspect that there was a material instance within NGUI (used to display the RenderTexture - a blurred grab of the game scene, behind some UI) still referencing the old/invalid RenderTexture.
I’m no longer re-allocating them, and I’m re-rendering (with a Camera.Update) from a Monobehaviour Update(), after setting a flag in OnApplicationPause to indicate that they’ve been lost. Haven’t tested on Android yet, but it’s looking good on WP8
This is a known issue on Android, in 4.3.x when resuming, Render textures lose data, it does not happen on 4.2 though, however it has been fixed for 4.5 ( on Android). Anyway whenever you use RenderTextures (on any platform), you need to check with RenderTexture.IsCreated()
to know if you need to render them again.
Note that the documentation warns about this :
“Keep in mind that render texture contents can become “lost” on certain events, like loading a new level, system going to a screensaver mode, in and our of fullscreen and so on. When that happens, your existing render textures will become “not yet created” again, you can check for that with IsCreated function.”