So, I have an in-game camera the player can use, and I would like to be able to save the images instantly without noticing a lag. So far, I create render a camera into a RenderTexture and I feed it in this extension method:
public static void SaveToFile(this RenderTexture renderTexture, string name)
{
RenderTexture currentActiveRT = RenderTexture.active;
RenderTexture.active = renderTexture;
Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
var bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(name, bytes);
UnityEngine.Object.Destroy(tex);
RenderTexture.active = currentActiveRT;
}
It does work, but ReadPixels is slow as hell and it creates a noticeable hiccup. I tried several ways to smooth out the performance spike created by it. First, I tried to call this method from another thread, but it has to be called in the main thread. Then, I tried to use renderTexture.GetNativeTexturePtr() with Marshal.Copy(), but I can’t find a way to do it. I guess the only other way is to make a Native plugin, but my knowledge in C/C++ is pretty limited. Anybody has any idea on this?