- Home /
How to resize UI panel based on visibility of children?
How would I go about scaling the size of a UI panel based on the visibility of it's child-components? For instance, if I have a panel of buttons and I want to size it down when some buttons are not available?
Thanks!
Why not set UI Scale $$anonymous$$ode of Canvas Scaler to Scale with Screen Size?
Because (unless I'm misunderstanding) that wouldn't accomplish the goal... to vertically resize a panel based on the number of buttons it contains (IE - to size to the visible content dynamically).
Answer by SterlingSoftworks · Jan 20, 2016 at 05:58 AM
I'm assuming this is what you are going after correct? (ignore the yellowish/brown that's just the background, the pale white is a panel and the full white are buttons)
This took some fiddling with the GUI system but I've got your solution!
First, create a Panel on your canvas and make it what ever size you need/want.
Then you're going to add two components to this panel. Through the "Add Component" button you're going to make your way to Layout -> Horizontal, Vertical, or Grid Layout Group (whatever suits your needs). You can mess with the cell size, spacing and etc to again, suit your needs.
Then you'll need to add the Content Size Fitter component also found in the Layout section. Set the Horizontal and Vertical fits to "Preferred Size".
NOW, add however many buttons to this panel as your heart desires. :D
The panel will re-size to fit the amount of ACTIVE child buttons.
Fantastic... thank you. I was fiddling with those exactly, but had yet to get it all working.
The grid layout also looks fantastic since you can use the constraints to do horizontal + vertical with a fixed column/row if desired.
Thanks again.
Good, I'm glad you were at least trying to get something to work by exploring the the components ins$$anonymous$$d of co$$anonymous$$g straight here xD
I don't have much experience with GUI and this will definitely be stored in my head for later use as I've never done this before. So thank YOU for asking it hah
It's great isn't it?? $$anonymous$$akes putting together menus SO much easier and looking much cleaner. Wish I knew that when I made my first game!
It's awesome. There was a similar functionality in nGUI and I was really missing it in Unity 4.6. Well done!
Your answer
Follow this Question
Related Questions
Why use different Canvas Render Modes 0 Answers
UI buttons become unselectable on scene change 2 Answers
How do I create an Image of certain size in the middle of the screen with stretching border images 0 Answers
Text object can't be searched for with GameObject.Find 3 Answers
How to make button stay the same size on all screens? 2 Answers