I have a HUD. I want to always set the HUD to the size of the screen. Any ideas? C# please!!!
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
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!