- Home /
Resizing UI Image but with minimum width
I have a dialog box I want to fit the bottom of the screen such that there's a certain amount of space on either side, this I can do easily using a stretchy rect transform.
However, I want this box to always be at least a certain width and for this minimum width to "override" the rect transform's settings as the screen's aspect ratio changes. So for example, if the screen is >1000 pixels wide, there is always 200 pixels of space on either side of the UI image. However once it is <1000, it remains fixed at 600 pixels wide.
I think it has something to do with using a Layout Group and a Content Size Fitter with the "min size" setting but I'm not sure exactly how to arrange the hierarchy.
Thanks.
Ok, if I add a layout element with $$anonymous$$ width on the box, then put that as a child of a horizontal layout group, then I'm nearly there - except the box doesn't stay centered when it hits the $$anonymous$$ width - the space to the left remains at 200 pixels while the right side shrinks.
Answer by EmmetOT · Jan 09, 2018 at 12:02 PM
Answering my own question:
Horizontal layout group with child control size and child force expand both set for width, three children. Two empty gameobjects to the left and right of the box, and the box itself in the middle with a layout element and the min width set to the desired width. Works nicely.
Answer by kami1339 · Apr 05, 2019 at 08:27 AM
solved I examined it .
YourImageOrRaw image .GetComponent<RectTransform>().sizeDelta = new Vector2( minWidth, minHeight);
Don't forget it >using UnityEngine.UI;
Your answer
Follow this Question
Related Questions
How to find the bottom position of a Image based on it's height? 2 Answers
How do I layout dynamically instantiated UGUI components? 1 Answer
UI Elements Move Animation Using iTween 2 Answers
Anchoring a UI image to the top of another UI image. 1 Answer
How to Save Property Values of a Component on a Scriptable Object? 1 Answer