Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Created December 11, 2019 15:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tklee1975/fa106950d5ad47cd60b814992e609337 to your computer and use it in GitHub Desktop.
Save tklee1975/fa106950d5ad47cd60b814992e609337 to your computer and use it in GitHub Desktop.
Automatically Aspect Fit the RawImage (One of the Unity UI Component)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RawImageAspectFitter : MonoBehaviour
{
[SerializeField] bool m_adjustOnStart = true;
protected RawImage m_image;
protected float m_aspectRatio = 1.0f;
protected float m_rectAspectRatio = 1.0f;
private void Start()
{
AdjustAspect();
}
void SetupImage()
{
m_image = GetComponent<RawImage>();
CalculateImageAspectRatio();
CalculateTextureAspectRatio();
}
void CalculateImageAspectRatio()
{
RectTransform rt = transform as RectTransform;
m_rectAspectRatio = rt.sizeDelta.x / rt.sizeDelta.y;
}
void CalculateTextureAspectRatio()
{
if(m_image == null)
{
Debug.Log("CalculateAspectRatio: m_image is null");
return;
}
Texture2D texture = (Texture2D) m_image.texture;
if(texture == null)
{
Debug.Log("CalculateAspectRatio: texture is null");
return;
}
m_aspectRatio = (float)texture.width / texture.height;
//Debug.Log("textW=" + texture.width + " h=" + texture.height + " ratio=" + m_aspectRatio);
}
public void AdjustAspect()
{
SetupImage();
bool fitY = m_aspectRatio < m_rectAspectRatio;
SetAspectFitToImage(m_image, fitY, m_aspectRatio);
}
protected virtual void SetAspectFitToImage(RawImage _image,
bool yOverflow, float displayRatio)
{
if (_image == null)
{
return;
}
Rect rect = new Rect(0, 0, 1, 1); // default
if (yOverflow)
{
rect.height = m_aspectRatio / m_rectAspectRatio;
rect.y = (1 - rect.height) * 0.5f;
}
else
{
rect.width = m_rectAspectRatio / m_aspectRatio;
rect.x = (1 - rect.width) * 0.5f;
}
_image.uvRect = rect;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment