Search Unity

Question Displaying a texture with Graphics.Blit

Discussion in 'General Graphics' started by Stevens-R-Miller, May 5, 2023.

  1. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    This code is copied from the Unity documentation on Graphics.Blit:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Example : MonoBehaviour
    4. { // Copies aTexture to rTex and displays it in all cameras.
    5.  
    6.     Texture aTexture;
    7.     RenderTexture rTex;
    8.  
    9.     void Start()
    10.     {
    11.         if (!aTexture || !rTex)
    12.         {
    13.             Debug.LogError("A texture or a render texture are missing, assign them.");
    14.         }
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         Graphics.Blit(aTexture, rTex);
    20.     }
    21. }
    As written, it produces the error message at Line 13 (so I guess you could say it works).

    However, when I make
    aTexture
    and
    rTex
    public and drop appropriate assets into them, all I get when I run the scene is a view of the skybox, not the texture dropped into
    aTexture
    .

    Adding this component to the GameObject with the main Camera does work:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // Blit a texture to the screen.
    4.  
    5. public class Blit : MonoBehaviour
    6. {
    7.     public Texture aTexture;
    8.  
    9.     void OnRenderImage(RenderTexture src, RenderTexture dst)
    10.     {
    11.         Graphics.Blit(aTexture, null as RenderTexture);
    12.     }
    13. }
    It seems as though the first example is supposed to do the same thing, but I'm obviously missing something.

    Can someone help me understand what the first example is doing?

    Thanks!