I am building a traffic simulator in Unity. I need to record my game as a video, so that I can use the video for machine learning purposes. I used fraps for the same but didn't find it worked for me since it's not within Unity. How can I make a video recording of the gameplay?
It's going to be difficult.
You'll need to find screen-recording software with an API so it can be activated by a client application.
Bandicam ( http://www.bandicam.com/ ) appears to have a basic screen capture API . The APIs are in C/C++ but in theory you could call the C API from a C#/.NET application.
Your user will need Bandicam installed and you'd be restricted to Windows platforms only.
It would be great to activate ShadowPlay or the other vendor-supported capture systems have APIs, but right now I'm not aware of any. You would have thought that Windows 10's Game Capture feature had an SDK, but I can't see any reference to one.
I have somewhat simplistic solution that I have used to make videos in this question....
To record screen I am using Iceream screen recorder (Not affiliated in any way just a happy user)
it allows you to select and record section on the screen so if you're up for manual this might just do it
You can use Unity API to do this. No need for any 3rd party. Unity renders a frame on its own, why would you need something to render it again or store the rendered texture from memory outside it, when you can do this inside C#? Right, no need to do this. But why is this so hard? Why can't I just take this texture? I don't really have the right answer to this because I'm not an expert on GPU. But as far as I know, even though GPU has memory, it deletes everything from there very often, I guess every frame. (Please if you understand this topic about GPU much more than I do, explain it in comments, I'm very thankful for this).
This Unity forum is dedicated to this question: How to access rendered depth buffer properly?
Unity allows to render the camera manually: Camera Render.
With Some Code:
Also, if this is or not VR, this thread would be definitely helpful and will save a lot of time looking for the performance hits: Real-Time Image Capture In Unity.
// Setup a camera, texture and render texture Camera cam = ...; Texture2D tex = ...; RenderTexture rt = ...; // Render to RenderTexture cam.targetTexture = rt; cam.Render(); // Read pixels to texture RenderTexture.active = rt; tex.ReadPixels(rectReadPicture, 0, 0); // Read texture to array Color framebuffer = tex.GetPixels();
Here is the code from Unity documentation on how to store render to png.
// We should only read the screen buffer after rendering is complete yield return new WaitForEndOfFrame(); // Create a texture the size of the screen, RGB24 format int width = Screen.width; int height = Screen.height; Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); // Read screen contents into the texture tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex.Apply(); // Encode texture into PNG byte bytes = tex.EncodeToPNG(); Object.Destroy(tex); // For testing purposes, also write to a file in the project folder File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);