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 :slight_smile:

My problem is that when I script the GUI on my main monitor (19201200) 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 19201200, 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?

1 Answer

1

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;
	}
}

Hmm still doesn't scale to different screen sizes..

–

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.

–

Here is a screenshot, it does rescale when i rescale the window, but the size, is pretty strange, there most be something else that is effecting it. ![alt text][1] [1]: http://img267.imageshack.us/img267/6723/errorscreenshot.png

–