UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Ui Module / Raw Image Preserve Aspect Completed

Ui Module — UGTK

Raw Image Preserve Aspect

Summary

Mn_RawImagePreserveAspect is a RawImage subclass that gives it the Preserve Aspect toggle Unity only ships on Image. It letterboxes the texture inside the RectTransform — without touching the RectTransform itself — while still honouring uvRect and supporting SetNativeSize().

It is what you need whenever the texture is not an imported sprite: a RenderTexture, a webcam feed, a downloaded image, a Texture2D built in code. Those all have to go through RawImage, and RawImage stretches by default.


Content


Modules Dependencies

None on the UGTK side. It only relies on Unity UI (UnityEngine.UI.RawImage).


Setup

Drop the RawImage prefab under a Canvas — a RectTransform already carrying Mn_RawImagePreserveAspect with Preserve Aspect on:

Or add the component from UI ▸ Raw Image (Preserve Aspect). On an existing RawImage you cannot just add the component — it is a RawImage — so remove the old one and add this instead, then re-assign the texture.


How To Use

var raw = GetComponent<Mn_RawImagePreserveAspect>();
raw.texture = myRenderTexture;
raw.preserveAspect = true;   // rebuilds the mesh straight away
raw.SetNativeSize();         // RectTransform = texture pixel size

Good to know:


Testing Scene

Open Debug/RawImagePreserveAspectSceneTest.unity: the same texture is shown in rects of different proportions, with and without Preserve Aspect, so the letterboxing is immediately visible.


Technical Info

Path Content
Scripts/Runtime/Mn_RawImagePreserveAspect.cs The component: OnPopulateMesh override that builds the fitted quad, GetDrawingDimensions (min-fit letterbox), SetNativeSize override
Prefabs/RawImage.prefab RawImage with the component already attached
Debug/RawImagePreserveAspectSceneTest.unity Test scene

API: preserveAspect (bool property, rebuilds the mesh on change), SetNativeSize(), plus everything inherited from RawImage (texture, uvRect, color, material, …).

The maths: from the rect ratio and the texture ratio, the wider of the two dimensions is padded by (excess / 2) on both sides — the same min-fit Image uses, which is why the two components look identical side by side.


© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Depends on 0

  • Standalone: no other module required.

Used by 1

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 0 modules in total.