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?

  • 1
    \$\begingroup\$ What do you mean by not finding it "convincing"? \$\endgroup\$ Commented Jun 6, 2017 at 13:22
  • 1
    \$\begingroup\$ I would like something that takes the game video within the unity. \$\endgroup\$
    – NAnn
    Commented Jun 6, 2017 at 13:35
  • \$\begingroup\$ I don't think there is a way. I use GIPHY for this, but FRAPS should be fine, too. \$\endgroup\$ Commented Jun 6, 2017 at 16:17
  • \$\begingroup\$ You can store screenshots from Unity, but these are only stills. It has a major impact on your frame-rate, so it's not really applicable for movie-making, unless you're willing to do a 'render' (ie. run the simulation extremely slowly, generate PNGs and then throw them back together into a video via Premiere or a similar video editor) \$\endgroup\$ Commented Jun 7, 2017 at 2:12
  • \$\begingroup\$ I'm no expert in machine learning, but I'm just curious why do you need to process image, if you have objects full of data to process? \$\endgroup\$ Commented Jun 16, 2017 at 15:17

3 Answers 3


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) enter image description here

it allows you to select and record section on the screen so if you're up for manual this might just do it

  • \$\begingroup\$ How do you trigger it from the Unity game? My customers are interested in the same sort of arrangement - being able to start screen-recording from inside the game itself - and it would be nice to have a Mac option. But you didn't mention how it's done. \$\endgroup\$ Commented Jun 7, 2017 at 22:22
  • \$\begingroup\$ @SteveHarding Hi Steve. You do it manually, select the part of screen and then just record... If you have bits where you recorded starting the game, you can just cut it off later... \$\endgroup\$ Commented Jun 8, 2017 at 3:09
  • 2
    \$\begingroup\$ I took "it's not within Unity" to mean "It can't be controlled from", since FRAPS and its many friends - including the tools built into Windows and which ship with your video card - work fine for Unity games. It may be that I misinterpreted the question though. \$\endgroup\$ Commented Jun 8, 2017 at 23:08

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;

// 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);

// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();

// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
  • \$\begingroup\$ You state that you can do this in the Unity API then you say that you don't know how to do it. You then provide links as an answer which makes this (practically) a link-only answer. And even if the links explain how to access the buffer texture you don't explain the process to make it into a video, or even how to save it. \$\endgroup\$
    – Charanor
    Commented Jun 16, 2017 at 18:15
  • \$\begingroup\$ @Charanor I wrote that I don't fully understand how GPU works. I didn't say I don't know how to do it. The last link has an obvious code on how to do it, I just didn't want to copy it from the source and provide full information. Also, if you don't know how to make a video from a bunch of images I guess you don't need this - because it is too broad topic to talk on here. Just go and google it, you can't format video inside a Unity. If you ask questions like that - you have to do some research first before people write you a complete solution. \$\endgroup\$ Commented Jun 16, 2017 at 18:55
  • \$\begingroup\$ @Charanor And the last, saving an image inside Unity, docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html , here you go. It took me 5 sec to open a new tab and google it. OP is doing machine learning, I don't think he is going to process a video format, but frames of that video - which makes it less difficult as he won't need to divide a video into images. \$\endgroup\$ Commented Jun 16, 2017 at 18:57
  • \$\begingroup\$ I would still suggest adding the relevant content of your links into your answer since now if the links die your answer doesn't answer the question (hence why it's a link-only answer as it stands). \$\endgroup\$
    – Charanor
    Commented Jun 16, 2017 at 19:50
  • 2
    \$\begingroup\$ @Charanor Here you go, added the code :) You made a good point, thank you for replying. \$\endgroup\$ Commented Jun 17, 2017 at 0:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .