- Home /
How to render a cinematic or a cutscene in Unity?
A while back i wanted to create a cinematic completely in Unity, Something akin to an epic like Blacksmith. What i created was far from epic Amazons Sparring.
The biggest problem was rendering the movie itself. After spending days googling for any info i gave up, created a build and used Fraps to record it. Lost a bunch of frames, somehow my colors lost saturation (this could be related to my encoding the video for YouTube).
A few days ago i stumbled across WaitForEndOfFrame function. I modified the code a bit and was able to capture images of my frames.
using System.IO;
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
private int count = 1;
// Take a shot immediately
void Awake()
{
StartCoroutine(UploadPNG());
}
void Update()
{
StartCoroutine(UploadPNG());
}
IEnumerator UploadPNG() {
// 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();
Destroy(tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen" + count + ".png", bytes);
count++;
}
}
To get editor PlayMode to display itself in full screen used Full Screen On Play script.
This allowed me to capture frames in full screen resolution directly from the editor. But my PC isn't powerful enough so I lost frames. I thought of slowing down the game itself to allow for every frame to be rendered, so i tweaked TimeManager default values ( Edit>Project Settings>Time) by lowering them as far as they would go. This worked and i was to able capture every frame, until the Editor froze.So i created the build and ran it. It worked up to a point then it froze too.
Every frame i captured at 1920x1080 was a bit over 3MB, and when everything stopped working i had about 6GB of images.
I'm stuck and out of ideas.
Can anyone offer a solution?
If I would want to record a video, I would switch from a time based animation to a frame based.
I'd have a singleton Video$$anonymous$$anager class or something like that which would increase a frame counter and wait for the scene to render, then save the image and increase the counter. All animations and stuff that used Time.time before will use Video$$anonymous$$anager.frame / 60f (for 60 frames per second) afterwards. If you were using the Unity animation system, you would need to set the frame or time there manually as well.
This way your machine has endless time to render and write the images and there is no need for coroutines. The result will still be some GB (depending on the length of the recording) of still images which you will have to drag into the video editor of your choice to render a video from it.
Your answer
Follow this Question
Related Questions
2D Undertale like Cutscenes / Animations,2D Undertale type cutscenes / animations. 0 Answers
Would a Cinema 4d model keep good graphics when imported into Unity? 1 Answer
Is it okay to use the animator with cutscenes ?!,is it okay to make cutscenes with animator ? 1 Answer
Is "Animations" stat in Rendering Statistics Window only an incremental value? 0 Answers
How to create a cutscene on a specific spot in the game environment using cinemachine and timeline? 0 Answers