- Home /
UI: Canvas size changed Event?
Hey guys,
so I have a Canvas with a Screen Space - Overlay setup and need to detect changes to the canvas (hence window) in order to rescale certain objects. So, I figured that there has to be some event I can listen to. But until now, I did not find a way to find one.
Any help here would be appreciated.
Are you sure you don't want to set the Canvas Scaler component to "Scale with Screen size"? This auto handles your rescales, but takes some adjusments. It forces you to deisgn your UI slightly differently ($$anonymous$$OSTLY how careful you have to be with anchor positions), but it let's you not have to worry about differing resolutions.
http://docs.unity3d.com/$$anonymous$$anual/script-CanvasScaler.html
Do you mean you want to detect if different Aspect ratios are being used. Like if you develop for Android and the screen could be any size or aspect ration?
Hey guys, thanks for your answers. So, to make this a little more clear:
I have a rather complex menu layout (e.g. with colliding objects that can push each other away) and have to be perfectly aligned for that. @sniper43 Yeah the CanvasScalar looks pretty good at first sight, but I would still prefer a event driven rescaling.
That is because I save a fraction of the current canvas size, which I use as a standard size and from which I calculate everything else. For example: 1/10 each of the canvas' width and hight. I would much rather just update that scalar (for x,y) when the canvas' size changes.
@$$anonymous$$mmpies Yeah co$$anonymous$$g from mobile I didn't think of that either b/c in the mobile case, there is no change in window size except for orientation changes which can probably be caught anyway. BUT for desktop applications, the window should properly resize and with it, all of it's layout elements proportionally.
Answer by mischinab · May 28, 2018 at 12:17 PM
You can utilise the OnRectTransformDimensionsChange.html
callback by attaching a script to the canvas (or any GameObject that has a RectTransform
), and add the following code:
private void OnRectTransformDimensionsChange()
{
// The RectTransform has changed!
}
You could either add this to the root Canvas, or to any GameObjects whose RectTransform will automatically scale with the screen, but need some extra tweaking such as repositioning children or adjusting text sizes.
Here’s the official documentation: https://docs.unity3d.com/ScriptReference/EventSystems.UIBehaviour.OnRectTransformDimensionsChange.html
Your answer
Follow this Question
Related Questions
Antialiasing As Post Effect Blurring UI Text 0 Answers
How do Pointer events work? 4.6 UI 1 Answer
UI button dosent show function 1 Answer
Why canvas set to Screen Space - Overlay doesn't cover entire screen 0 Answers
Unity 4.6: Position jumps when switching Canvas render mode from Overlay to camera in code 0 Answers