Scaling GUI to match screen resolution

Hi everyone.

Well, this will be my first post here. I have Unity3D Pro and im developing my new application with this amazing tool :smile:

However ive been having some problems with my GUI.

All my GUI objects on screen are GUITexture objects so i can assign different textures for a button (OnMouseOver, OnMouseUp and OnMouseDown), and its working.

But all my GUI system only works on my ā€œnative resolutionā€ (native resolution of textures) and when i change my resolution in edit mode, all my GUI is going to hell :frowning:

Ive been searching here, in the forums, about this issue and i found something related to GUI.matrix.

Here is my code:

public float nativeScreenHeight = 450f; 
public float nativeScreenWidth = 800f; 

public void OnGUI()	{
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(Screen.width / this.nativeScreenWidth, Screen.height / this.nativeScreenHeight, 1.0f));
}

This script is attached to a GUITexture object.

But doesnt work :face_with_spiral_eyes:

doesnt GUI.matrix work with a GUITexture?? :frowning:

Thanks in advance

No, if you want to use gui textures then you have to either scale it or change it’s pixel inset variable. GUI.matrix is for the GUI class.

Thanks Daniel for your fast answer.

I made a class for this purpose. If you attach it to your GUITexture GameObject your GUI will work with different resolutions.

using UnityEngine;
using System.Collections;

[ExecuteInEditMode()]  
public class AdjustGUIOnScreenScript : MonoBehaviour {
	
	/// <summary>
	/// Height of native screen
	/// </summary>
	public float nativeScreenHeight = 450f; 
	
	/// <summary>
	/// Width of native screen
	/// </summary>
	public float nativeScreenWidth = 800f; 
	
	/// <summary>
	/// Texture height on native screen (original height)
	/// </summary>
	public float texWidth;
	
	/// <summary>
	/// Texture width on native screen (original width)
	/// </summary>
	public float texHeight;
	
	/// <summary>
	/// The scale relation x.
	/// </summary>
	private float _scaleRelationX;
	
	/// <summary>
	/// The scale relation y.
	/// </summary>
	private float _scaleRelationY;

	
	/// <summary>
	/// Start this instance
	/// </summary>
	public void Start() {
		this.guiTexture.texture.wrapMode = TextureWrapMode.Clamp;
		
		if (this.texHeight == 0f || this.texWidth == 0f)	{
			print("Warning: texture width and height should be greater than 0");
		}
	}
	
	/// <summary>
	/// Raises the GUI event
	/// </summary>
	public void OnGUI()	{
		this._scaleRelationX = Screen.width / this.nativeScreenWidth;
		this._scaleRelationY = Screen.height / this.nativeScreenHeight;
		
		this.guiTexture.pixelInset 
			= new Rect(-this.texWidth * this._scaleRelationX / 2, 
				-this.texHeight * this._scaleRelationY / 2, 
				this.texWidth * this._scaleRelationX, 
				this.texHeight * this._scaleRelationY);
	}
}

I hope it helps someone more.

Hey there Alejocadavid

your script looks like the perfect solution for my project and screen resolutions, question is it possible to add the iphone device detection lines of code for it to automatically set it to dimensions to the ipad or iphone depending which device is being used? where would your if statements go?

ive yet to play with your script as its really late right now but , let me know in advance

Thanks !

This is hard…

Hi void

Well i dont know how to get IPhone resolution. In my script i use Screen.width and Screen.height to get the screen resolution and ive not found some method to get iphone / ipad screen resolution (They change their resolution depending their version and model i guess).

Screen orientation can get it with iPhoneSettings.screenOrientation.

If someone could know how to get iphone / ipad resolution may be we can try.