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
1
Question by rmccabe82 · Dec 14, 2014 at 02:50 PM · c#editorpanel

Dynamically resize UI.Panel through code

Hi,

I have a panel that I am dynamically adding objects to (prefabs I've created) at runtime to represent files. The prefabs are added one after the other horizontally. I keep track of the X position when adding a new object and increment. This works fine but the objects within the panel now exceed the width of the panel.

I've set the panel up so its a drag panel. You can scroll through the horizontal list. But the scroll is only as wide as the panel, in otherwords, the rest of the objects are off the screen and cannot be dragged to.

I need to dynamically resize the panel as I add the new object. I have two questions:

1) How do I ascertain the current size of the panel container?

2) How do I then resize the panel at runtime?

Thanks in advance!! Rob

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

3 Replies

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

Answer by DOZO · Dec 14, 2014 at 03:21 PM

I believe you want the "sizeDelta" property of the panel's RectTransform. You can both get and set it.

You could also look at the "Content Fitter" component if you want Unity to handle it automatically.

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 rmccabe82 · Dec 14, 2014 at 06:03 PM 0
Share

The size delta option looks like it could be good but this code doesn't work:

 public Transform panel; // Reference to the panel transform.
 
         void Start()
         {
             RectTransform panelRectTransform = panel.GetComponent<RectTransform>();
     
             for .... // add components 
     
             panelRectTransform.sizeDelta.Set((float)xPos + 10, panelRectTransform.sizeDelta.y); 
             // xPos is the placement of the components.
         }
   

Any ideas where I am going wrong?

Using Debug.Log("panel width = " + panelRectTransform.sizeDelta.x); I can see the correct width of the panel but it never gets updated.

avatar image JapsR · Jul 04, 2015 at 11:11 AM 0
Share

This worked for me: ins$$anonymous$$d of panelRectTransform.sizeDelta.Set((float)xPos + 10, panelRectTransform.sizeDelta.y); use panelRectTransform.sizeDelta = new Vector2((float)xPos + 10, panelRectTransform.sizeDelta.y);

avatar image
3

Answer by JapsR · Jul 04, 2015 at 12:39 PM

Any ideas where I am going wrong?

Using Debug.Log("panel width = " + panelRectTransform.sizeDelta.x); I can see the correct width of the panel but it never gets updated.

This worked for me: instead of panelRectTransform.sizeDelta.Set((float)xPos + 10, panelRectTransform.sizeDelta.y); use panelRectTransform.sizeDelta = new Vector2((float)xPos + 10, panelRectTransform.sizeDelta.y);

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 johandanforth · May 07, 2017 at 08:30 PM 0
Share

Yes, this worked for me too. $$anonymous$$aybe it's a change in later versions of Unity

avatar image JuliuszK · Oct 28, 2017 at 10:01 PM 1
Share

Oh man - thanks for this! I was pulling my hair out of frustration! Why the documentation is so out-dated!

avatar image
1

Answer by demented_hedgehog · Mar 01, 2015 at 07:37 AM

I'm not completely across the new UI layout stuff in Unity 4.6. But I believe that you may be able to handle this automatically (depending on exactly what your requirements are), e.g. add a Layout|Horizontal Layout Group to the panel and add a Layout Element to each of the prefabs to be added to the panel.

Good video tutorial here.

In short, the new unity UI provides the kind of vbox, hbox, gridbox UI builders/resizers you see in other GUI toolkits. They are, however, currently not very well documented, as far as I can see.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Set Gizmo to object in editor by c# script 1 Answer

Editor Script Points 0 Answers

Script Editors for Unity 3 Answers

C#scripts visibility at runtime and in editor in Monodevelop 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