Skip to content

Instantly share code, notes, and snippets.

@Tymski
Last active April 13, 2021 22:09
Show Gist options
  • Save Tymski/162f3d2210270ef8dec61da07f0521b4 to your computer and use it in GitHub Desktop.
Save Tymski/162f3d2210270ef8dec61da07f0521b4 to your computer and use it in GitHub Desktop.
When you put an AspectRatioFitter inside a LayoutGroup, it breaks your layout. This is a fix for this.
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(AspectRatioFitter))]
[RequireComponent(typeof(RectTransform))]
[ExecuteInEditMode]
public class WorkingAspectRatioFitter : MonoBehaviour
{
AspectRatioFitter aspectRatioFitter;
RectTransform rectTransform;
private void Awake()
{
aspectRatioFitter = GetComponent<AspectRatioFitter>();
aspectRatioFitter.enabled = false;
rectTransform = GetComponent<RectTransform>();
}
private void Update()
{
aspectRatioFitter.enabled = true;
aspectRatioFitter.SetLayoutHorizontal();
aspectRatioFitter.SetLayoutVertical();
aspectRatioFitter.SetLayoutHorizontal();
aspectRatioFitter.enabled = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment