Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created December 3, 2025 21:35
Show Gist options
  • Select an option

  • Save unitycoder/175fec58e90dc4b64cb9271fb2623075 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/175fec58e90dc4b64cb9271fb2623075 to your computer and use it in GitHub Desktop.
ugui: getting canvas element size fails in build at Start() in build, but works in Editor?!?!
// this works, it waits 1 frame then forces update, BUT it doesnt work if you do those calculations directly at Start()! (works in Editor but not in build)
void Start()
{
rectTransform = GetComponent<RectTransform>();
StartCoroutine(InitializeAfterLayout());
}
IEnumerator InitializeAfterLayout()
{
// Wait for end of frame to ensure layout is calculated
yield return new WaitForEndOfFrame();
// Force canvas update
Canvas.ForceUpdateCanvases();
float screenWidth = Screen.width;
MinX = -rectTransform.rect.width - 32;
var parentRect = rectTransform.parent.GetComponent<RectTransform>();
MaxX = parentRect.rect.width + 32;
Debug.Log("Screen width: " + screenWidth + ", MinX: " + MinX + ", MaxX: " + MaxX + " rectTransform.rect.width: " + rectTransform.rect.width + " parentRect.rect.width:" + parentRect.rect.width);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment