Created
December 3, 2025 21:35
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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