Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by ina · Jun 05, 2015 at 07:40 PM · uiruntimerecttransform

uGUI - resize RectTransform at runtime to fill entire screen

How do you resize a rect transform at runtime to fill the entire screen

I tried .sizeDelta = new Vector2 (Screen.width, Screen.height); - however, this does not seem to go to the actual full size of the screen?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by fafase · Jun 05, 2015 at 07:48 PM

Consider using the anchor position instead.

 RectTransform rt = GetComponent<RectTransform>();
 rt.anchorMin = Vector2.zero;
 rt.anchorMax = Vector2.one;
 rt.sizeDelta = Vector2.zero;

Note that it actually works if the parent occupies the whole screen.

Comment
Add comment · Show 5 · 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
avatar image ina · Jun 08, 2015 at 08:11 AM 0
Share

good trick - any advice on how to understand how the anchors work?

avatar image fafase · Jun 08, 2015 at 12:03 PM 1
Share

Quite tricky to explain...

You have 4 anchors and 4 blue points for each UI item. Each blue point follows its corresponding anchor, so the bottom left is relative to bottom left anchor and so on.

The anchor is normalized to the screen size so you place with percentage of the full distance. This is use to move the anchor based on resizing the screen (if the canvas is meant to, default though).

So, if your anchor is at 0.5 then it is always in the middle, if at 0.2 then it will always be at 20% of the full dimension, so 200 if dimension is 1000 and 200 if the full is 600.

The blue point is following that anchor by the distance you gave it. So if your blue point is on the anchor, the blue point has the same behaviour and rescale based on screen size. If teh blue point was 200 to the right of the anchor then anytime you rescale, this 200 is kept which represents a small portion on large screen but a large portion on small screen.

On the other hand, if you want to keep the dimensions of a UI regardless the screen, you need the anchor not to move with rescaling. You do so by placing them joint together in the middle of the UI item. Then the distance between the four corners and the anchor points which is now a single point in the middle, will remain the same. So a picture of 200x200 will remain that dimension on any screen size.

Just a quick introduction though.

avatar image Major0 · Aug 29, 2015 at 02:08 PM 0
Share

@fafase thanks for the answer and the explanation!

One typo: rt.sizeDelta = Vector2.zero; you missed the 2. :P

Also, do you have any idea what to do when the parent's RectTransform isn't set to stretch( does not occupy the whole screen) or even worse if we have nested RectTransforms.

How would we go about this? maybe by recursively drilling upwards and setting the anchors as you did in your answer till we get to the Canvas?

avatar image rsodre · Mar 16, 2017 at 04:20 PM 1
Share

Worked for me by adding...

 rt.anchoredPosition = Vector2.zero;
avatar image Astiolo rsodre · Mar 22, 2017 at 12:25 AM 0
Share

Yes this answer just sets the transform to the correct size but it can still be off center. I made it work by setting offset$$anonymous$$ax and offset$$anonymous$$in to zero ins$$anonymous$$d of setting sizeDelta. But setting the position would also work.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI RectTransform Position && Screen Resolution 2 Answers

Check if UI element is fully inside another UI element 1 Answer

Reset an GO to its original state (start app state) 2 Answers

[SOLUTION] set UI RectTransform Anchor Presets from code c# 1 Answer

Corners of RectTransform as Vecter[] 1 Answer


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