Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by ruffyD · Jan 21, 2015 at 10:19 AM · uicanvaseventevent-handling

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sniper43 · Jan 21, 2015 at 10:31 AM 0
Share

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

avatar image Mmmpies · Jan 21, 2015 at 10:53 AM 0
Share

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?

avatar image ruffyD · Jan 22, 2015 at 10:24 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges