- Home /
How to set Unity 4.6 object to screen size all the time???
I have a HUD. I want to always set the HUD to the size of the screen. Any ideas? C# please!!!
Answer by Kiwasi · Nov 20, 2014 at 08:21 PM
C# is way too much for this, it can (and should) be done entirely in the editor.
Create a canvas in screen or camera space
Add a panel as a child of the canvas. Remove the image component. Set it to stretch in both directions.
Put all other UI elements as children of the panel
Answer by c0d3n4m3 · Nov 20, 2014 at 08:31 PM
You could always use RectTransform and Screen.Width. Then drag the HUD UI object into the HUD field in the editor, on whatever GameObject you've attached this script to.
Then, whenever the game starts, the HUD will take on the full width of whatever device/screen it is being viewed/played on.
Although, I too must say that you shouldn't really need to set the width of your UI via a script. Not to say that it's frowned upon, but with the new 4.6 UI tools, scripting this functionality is kind of pointless unless you need to script it in for whatever reason.
public RectTransform HUD;
void Start () {
HUD.sizeDelta = new Vector2(Screen.width, whateverYouWantTheHeightToBe);
}
Hope it helps! :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Make HUD to show object direction 0 Answers
HUD reticle question 0 Answers
Flip over an object (smooth transition) 3 Answers