Use render textures to display the contents of another scene as textures

Page update date :
Page creation date :

Verification environment

  • Windows 11
Unity Editor
  • 2021.3.3f1
Input System Package
  • 1.3.0

Prerequisites for this tip

The following settings have been made in advance as a premise for the description of this tip.

At first

Render textures allow you to write your drawings to a texture instead of the screen. This allows you to freely generate textures during game execution without preparing texture images in advance.

You can also write to a render texture that changes dynamically every frame, for example, to project a different image from the main camera on a display that exists in the game.

Preparing Images

When using a render texture, you do not need to prepare an image, but you can prepare it as a sample to make the drawing result easier to understand.

Creating a Render Texture

Create an asset from the project. From Create, select Render Texture.

The name can be arbitrary. Since the name is not specified directly from the NewRenderTexture outside at this time, leave as .

Select the render texture and specify its size from the Inspector. Since the render texture is a drawing area, you must specify a size. This time, I want to write a scene created separately, so I set the aspect ratio to 16:9.

Setting the Render Texture Destination

Select "UI" > "Raw Image" from the hierarchy of the scene that is initially created. Display the contents of the render texture in this object. It is an object of the UI, but what you need is a "Raw Image" component, which can be set to another object.

The RawImage has been placed. Because it's a UI object, it's placed in a Canvas. I haven't set anything yet, so I just get a white rectangle.

We want the scale to be equal to each other, so we set the size of the RawImage object to be the same as the render texture. If you keep the aspect ratio the same, the drawing result will only be scaled even if you change the size, but if you change the aspect ratio, the drawing result will be crushed vertically or horizontally.

The Raw Image component of the RawImage object has a "texture", so set the render texture you just created there. The contents of the render texture are still empty, so they are transparent in the view.

Creating a scene of render texture drawing content

Next, create a new scene to create the content to be drawn in the render texture. Choose File from the menu and select New Scene.

Since this sample is 2D, you'll choose a 2D scene as well.

Create it in the Scene folder. SampleSceneTexture Leave the name as .

After creating the scene, place the objects appropriately. All you have to do is make sure you know the contents of the render texture. In order to confirm that anything that protrudes outside the camera is not drawn, a sprite is also placed near the camera frame.

If the objects overlap, the context of the objects will be indeterminate, so if you have a background, lower the order of the background layers.

After selecting "MainCamera", set the render texture in "Target texture". This causes the drawing to be drawn in the render texture.

Remove the Audio Listener component set to MainCamera. All you need for a render texture is drawing, so you don't need Audio Listener. A warning will be displayed if there are multiple "Audio Listeners" at runtime.

By the way, if you run the scene for the render texture in this state, nothing will be displayed on the screen. This is because you are drawing on a render texture. If you want to check it on the screen, delete the "target texture" and then execute it.

Layer settings

In this sample, you will have to move two scenes at the same time, but if you move them as they are, the contents to be drawn in the render texture will also be displayed on the main screen, so the layers will not be drawn separately.

First, select "Edit Layer" from the layer in the upper right corner of the editor.

The name and location are arbitrary, but here we will add a new RenderTarget layer named .

After adding the layer, open the scene for the render texture, select all the objects you want to draw, and change the layer value to the one you just created RenderTarget .

SampleSceneTexture This will end the editing of the scene, so please save it.

Displaying Render Textures

SampleScene Once you have opened a scene, add it to the hierarchy SampleSceneTexture by dragging and dropping the scene. Then, there should be two scenes in the hierarchy, and the contents of the scene should be displayed on the SampleSceneTexture screen.

SampleSceneTexture The content of the scene is disturbing, so click the eye icon of the scene from the SampleSceneTexture hierarchy. SampleSceneTexture This will hide the objects in the scene and only the contents of the scene will SampleScene be visible. By the way, since all the render texture settings have been completed,SampleScene the contents of the render texture should be displayed in the raw image placed in the scene.

However, the hiding by the eye icon is only a setting to make it invisible on the editor, so if you run SampleSceneTexture the game as it is, the contents of the scene will also be displayed on the screen. SampleScene Then select Scene Main Camera and uncheck From RenderTarget Culling Mask in the Camera Inspector. This prevents the objects in the layer from being displayed when RenderTarget the game runs.

Try running the game. It should appear as intended.

Try moving the render texture

However, in this state, it is difficult to know whether it is really working as a render texture or simply displaying the image as a crop, so let's move it.

The content can be anything, but here we will set the movement to automatically rotate the sprite. Create a SelfRotate script and create it as .

Here, we will include a simple process of rotating itself little by little.

using UnityEngine;

public class SelfRotate : MonoBehaviour
  // 更新はフレームごとに 1 回呼び出されます
  void Update()
    transform.Rotate(0, 0, 0.1f);

After creating the script, attach more and more to the object you want to rotate.

Then run the game and see if it spins. I think you can see that the object is moving in the render texture.

How to move multiple scenes at the same time

Here, we created a simple example so that we have two scenes running from the beginning, but in a real game, there are many cases where you have to start with a single scene. In that case, I think that you will have to dynamically add scenes at the beginning of the scene to make it multiple scenes.

Please refer to "Adding a scene" for the explanation of how to add a scene. Basically, SceneManager.LoadScene("<シーン名>", LoadSceneMode.Additive); calling should solve most of the problems.

How to prevent the contents of a render texture from appearing on the main screen without using layers

The content of the render texture is displayed on the main screen because the main scene and the render texture scene are in the same place. Therefore, if you move the contents of the render texture to a place that is never shown in the main scene, it will not be displayed on the main screen. For example, you can move a render texture object or camera to the (-10000, -10000) location. This way, layer settings are unnecessary.

When you want to complete the contents of a render texture in one scene without dividing it into different scenes

This is done using the same technique as the previous method without layers. You can move the content displayed by the render texture to a place where it will never be displayed on the main camera. The difference from the method of separating scenes is that you need to place two cameras, but other than that, you will basically have the same settings as the method of separating scenes.

If you want to capture content captured from a different perspective in a 3D game as a render texture

This tip didn't talk much about 3D because it focused on 2D, but the approach is basically the same. Since you are projecting a single 3D space from multiple perspectives, you don't need to have more than two scenes. All you have to do is place as many cameras as you need, set the drawing destination as the render texture, and set where to display the render texture.