Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 Nanako · May 20, 2015 at 01:54 AM · uiwidth

How do i set the width/height of a UI element?

this seems simple, but i cant figure it out. the rectTransform has no width or height parameter. Its rect parameter has a width parameter which is readonly, and the rect itself is also readonly so i can't write another rect over it

how do i actually change the width of a ui element from code?

Comment
Add comment · Show 2
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 HTwist · May 20, 2015 at 03:06 AM 0
Share

Check out http://forum.unity3d.com/threads/resolved-changing-height-of-recttransform-sizedelta-via-c-script-doesnt-update-box.318477/

You can't set width and heigh directly. You must play with sizeDelta.

avatar image Lylek · May 20, 2015 at 03:17 AM 1
Share

can also change scale

 GetComponent.<RectTransform>().localScale = Vector3(1, 1, 1);

5 Replies

· Add your reply
  • Sort: 
avatar image
25

Answer by Gnorpelzwerg · Jan 15, 2017 at 12:06 PM

GetComponent< RectTransform >( ).SetSizeWithCurrentAnchors( RectTranform.Axis.Vertical, myHeight); worked for me. For width use: RectTransform.Axis.Horizontal.

Comment
Add comment · Show 2 · 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 Simplexws · Oct 02, 2017 at 12:05 PM 0
Share

Thanks a lot mate :))))

avatar image Fattie · Nov 10, 2020 at 07:41 PM 0
Share

An answer so good I sent 200 reward :)

avatar image
17

Answer by robbagus · May 20, 2015 at 06:26 AM

yourUIElement.GetComponent(RectTransform).sizeDelta = new Vector2 (width, height);

Do bear in mind that the size is relative to your UI anchor.

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
avatar image
1

Answer by paddywwoof · Mar 08, 2019 at 06:54 PM

or I used this method for a tiled image as a health bar.

 health_bar.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, HEALTH_LEFT, health * HEALTH_UNIT);
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
avatar image
1

Answer by badadam · Mar 08, 2019 at 08:01 PM

Try this code

 yourUI.rectTransform.sizeDelta = new Vector2(newWidthUI,newHeightUI);

Comment
Add comment · Show 1 · 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 paddywwoof · Mar 08, 2019 at 08:45 PM 0
Share

@badadam Hi, with a health bar type thing the rectangle of tiled images needs to have its width changed then the left side moved by half the change in width. Possibly also adjusted for any scaling used in the UI element. So for that kind of thing the SetInsetAndSizeFromParentEdge takes care of scaling and moving to the correct location.

Paddy

avatar image
0

Answer by Shadow2359 · May 20, 2015 at 06:27 AM

If your UI element is a child of the RectTransform component you can use RectTransform.sizeDelta:Vector2 which will scale the UI element in the vertical and horizonal axis up to the anchor points, kinda like a scale function. If it doesn't need to be done in code you should be able to set the width and height in the inspector once you add the Rect Transform component to the game object that is going to house the UI element (I'm assuming a text element). If it does have to be done in script try looking below to see if to see any better alternatives to sizeDelta which I'm sure there are...

http://docs.unity3d.com/Manual/class-RectTransform.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

26 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 avatar image avatar image avatar image avatar image

Related Questions

Get height and width of gameobject set with anchors 0 Answers

UI 4.6: custom anchors breaking sizeDelta 0 Answers

Incorrect RectTransform width value when trying to read child RectTransform width. 0 Answers

How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 Answers

UI Vertical Layout Group with Content Size Fitter, how to set max width? 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