Search Unity

Render Texture on responsive UI

Discussion in 'UI Toolkit' started by Noblauch, Apr 30, 2020.

  1. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    275
    Hi everybody!

    I have a problem that I'm not sure how to tackle.
    I have an UI-based game and one mask features a Render Texture in form of a Raw Image (UI Element).
    The content that is rendered gets stretched as I scale the UI around. As I googled, I can't change the size of a Render Texture at runtime (that would fix the issue). But I can create a new one every frame, which sounds not like a good Idea.
    I tested the camera and rendered content in a separate scene, just rendering to screen. The issue only occurs when rendering to texture. In the end I just want the same behavior as rendering to screen normally, but taken my however stretched UI-Canvas as the virtual screen.

    Maybe someone has an Idea? ;)

    Bildschirmfoto 2020-04-30 um 14.06.17.png
    Bildschirmfoto 2020-04-30 um 14.06.29.png
     
    Mondeos1400 likes this.
  2. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    275
    I ended up writing a dirty script for now that checks the UI Elements RawImages Rect Transform for its resolution. If it changes, it creates a new RenderTexture with the corresponding resolution respecting a configurable base resolution and applies it to the RawImage component and a specified camera.

    Here is the script if anyone needs it:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. // Attach this script to your UI Element which has the RawImage component attached.
    6. // Select a camera that should receive the updated texture, done.
    7.  
    8. [RequireComponent(typeof(RawImage))]
    9. public class RenderTextureRescaler : MonoBehaviour
    10. {
    11.     public Camera cameraToUpdate;
    12.     public int targetResolution = 512;
    13.  
    14.     private RenderTexture currentTexture;
    15.     private RawImage rawImage;
    16.     private RectTransform trans;
    17.     private float aspect;
    18.  
    19.     private void Awake()
    20.     {
    21.         trans = (RectTransform)transform;
    22.         rawImage = GetComponent<RawImage>();
    23.  
    24.         if (rawImage.texture)
    25.             currentTexture = (RenderTexture)rawImage.texture;
    26.         else
    27.             currentTexture = CreateTexture();
    28.  
    29.         if (!cameraToUpdate)
    30.         {
    31.             Debug.LogError("You need to define a camera that receives the Render Texture!");
    32.             Destroy(this);
    33.         }
    34.     }
    35.  
    36.     private void Update()
    37.     {
    38.         float currentAspect = trans.rect.width / trans.rect.height;
    39.         if (currentAspect != aspect)
    40.         {
    41.             aspect = currentAspect;
    42.             UpdateTexture();
    43.         }
    44.     }
    45.  
    46.     private void UpdateTexture()
    47.     {
    48.         currentTexture.Release();
    49.         currentTexture = CreateTexture();
    50.         rawImage.texture = currentTexture;
    51.         cameraToUpdate.targetTexture = currentTexture;
    52.     }
    53.  
    54.     private RenderTexture CreateTexture()
    55.     {
    56.         RenderTexture tex = new RenderTexture((int)(targetResolution * aspect), targetResolution, 24);
    57.         tex.antiAliasing = 2;
    58.         return tex;
    59.     }
    60. }
    61.  
    Usage:
    Bildschirmfoto 2020-04-30 um 15.31.20.png
     
    Last edited: May 1, 2020
    Mondeos1400 and Creiz like this.