- Home /
Scaling GUI correctly according to game window resolution?
Hello, I know this question has been asked before, but after 2 hours of searching, I desided to write here :-)
My problem is that when I script the GUI on my main monitor (1920*1200) The gui looks normal (because that's the resolution i made the gui "fit" in) but when I try the game on a lower resolution monitor, the GUI is the same size as on the 1920*1200, and the gui is far to big. What is the easiest way to scale my gui so that it has a suitable size for different screen sizes?
Answer by paulaceccon · Apr 07, 2013 at 12:39 PM
using UnityEngine;
using System.Collections;
public class BackgroundController : MonoBehaviour
{
GUITexture _backgroundGUITexture;
void Update()
{
Rect r = new Rect( 0, 0, Screen.width, Screen.height );
if (_backgroundGUITexture)
_backgroundGUITexture.pixelInset = r;
}
public void setBackgroundTexture (Texture bck)
{
_backgroundGUITexture.texture = bck;
_backgroundGUITexture.enabled = true;
}
}
I always use this script to rescale... You just have to attach it to the game object that contains you GUITexture.
Okay, it seems like that when i press play, the BackgroundGUITexture get's unasigned althrough i attached it in the Scene/editor. But if I (at runtime) attach the object with the guitexture, it work's although the size is 1007, 1030 and that is not even the size of the texture.. Strange..
EDIT: If I delete this line: _backgroundGUITexture = this.GetComponentInChildren();
it kinda work's althrough the size is still 1007, 1030.
Sorry, I forgot about this... It was cause my texture was in a game object inside another game object, so I use GetComponentInChildren.
What is the size of the screen? It's weird, cause here Rect r = new Rect( 0, 0, Screen.width, Screen.height );
you should get a Rect in x,y = 0 and with the screen size. Here it works perfectly.
What do you mean about the size of the GUI? The size of the texture in the screen? If so, you can create a method to change the Rect, passing the size... Something like:
float myWidth = Screen.width;
float myHeight = Screen.height;
void Update()
{
Rect r = new Rect( 0, 0, myWidth, myHeight );
if (_backgroundGUITexture)
_backgroundGUITexture.pixelInset = r;
}
public void setSize (float width, float height)
{
myWidth = width;
myHeight = height;
}
Your answer
Follow this Question
Related Questions
GUI adapting to screen resolution? 3 Answers
How to scale a GUI (Lagacy) grid? 1 Answer
GUI and Screen Resolution 1 Answer
Dealing with GUI size on iPhone 1 Answer