Unity features 101: Render Textures Part 1 (in-game tv-camera)
Render textures is an awesome feature in Unity that has limitless potential. A render texture is a type of textures that is updated at run time.
You can use it for many cool ideas like an in-game tv-camera or a mini-map. In this article we will see how to create a render texture, its settings, and how to create an in-game tv-camera.
Creating a Render Texture
To create a render texture simply go to Create > Render Texture.
This will create a new render texture in your project files.
The most important setting in the size which is in pixel which also determines the aspect ration of the texture. In our case it is a 256x256 square render texture. You can also add anti-aliasing and play with depth buffer settings, but these can affect your game’s performance. The other settings are like any other textures.
Start off by creating a new camera in your scene and let’s call it tv camera. Select your tv camera and in the Output section of the camera, you can change the Output Texture. Drag your previously created render texture into the output texture of the camera.
And here is a small gif to demonstrate how to…
If done correctly, choose your render texture and you should see a preview of the what the newly created camera sees.
Now adjust your Tv-Camera to see whatever part of your game you want. Here is a quick tip, choose your tv-camera, then adjust your scene view to what the camera should see, then press “CTRL” + “Shift” + F, this will change change the transform of the camera in order to display what you want.
As you can see, this also changed the preview in the render texture.
Now in order to display this during gameplay, we should add this render texture to a gameobject in the scene.
To do so, let’s create a Qaud to act as our in-game TV, go to Create > 3D > Quad and rename it too Tv Camera Display. Next position and scale this quad into any position you like in your game.
Next let’s create a material that will hold the render texture so we can apply it to the quad. In your project files, go to Create > Material and rename it to Tv-Camera Material.
Now the most important step is to drag your render texture into the “base map” of this material. This will allow the material to render the texture during gameplay.
As you see in the material preview, it is now rendering whatever the tv-camera is seeing onto this material.
Finally, drag this material onto your quad.
There you have it, as you can see, now we have an in-game tv-camera that you use for any sorts of things like creating a mini security camera system for your game.
Note: the above capture is from a prototype called Adventures of Lea that you can test out through this link: Adventures of Lea | SIMMER.io.