Skip to content

Instantly share code, notes, and snippets.

@allfake
Created September 1, 2022 03:39
Show Gist options
  • Save allfake/7d9a51362e3305fadd2bf48ee20bec52 to your computer and use it in GitHub Desktop.
Save allfake/7d9a51362e3305fadd2bf48ee20bec52 to your computer and use it in GitHub Desktop.
UNITY auto size ui set max width
// Not work on Awake
private void Start()
{
var rect = gameObject.GetComponent<RectTransform>();
if (rect.rect.width > ScreenSizeHelper.MaxWidth)
{
var height = rect.rect.height;
rect.anchorMin = new Vector2(0.5f, 0.5f);
rect.anchorMax = new Vector2(0.5f, 0.5f);
rect.sizeDelta = new Vector2(ScreenSizeHelper.MaxWidth, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment