- Home /
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
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.
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.
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);
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);
Yes, this worked for me too. $$anonymous$$aybe it's a change in later versions of Unity
Oh man - thanks for this! I was pulling my hair out of frustration! Why the documentation is so out-dated!
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.
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.
Your answer

Follow this Question
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