Align and scale the GUI according to the screen resolution.

Hi,

I have use this below code for align my texture and scale in any kind of resolution. But its not working.
I have took this from some forums, but this java version work fine but it doesn’t.

Help me to do this…

public Texture2D texture;

	// Use this for initialization
	void Start () {
		

	}
	
	// Update is called once per frame
	void Update () {
	}
	
	void OnGUI()
	{
	  GUI.DrawTexture(ResizeGUI(Rect(100, 100, 100, 100)), texture);
	}
 
	Rect ResizeGUI(Rect _rect)
	{
	    float FilScreenWidth = _rect.width / 800;
	    float rectWidth = FilScreenWidth * Screen.width;
	    float FilScreenHeight = _rect.height / 600;
	    float rectHeight = FilScreenHeight * Screen.height;
	    float rectX = (_rect.x / 800) * Screen.width;
	    float rectY = (_rect.y / 600) * Screen.height;
	 
	    return new Rect(rectX,rectY,rectWidth,rectHeight);
	}

I would just get a percentage on game load. For instance, let’s say we want our default resolution for the GUI to be 800x600, but the user selected 640x480. We’d do some basic math on start to get the percentage of the screen we want to take up.

public Texture2D texture;
public Vector2 defaultSize = new Vector2(800,600);
private Vector2 newSize;

void Start()
{
	newSize.x = Screen.width / defaultSize.x; //In our case will be close 0.75 (600/800=.75, so 640 would be a little more... .77-.78?
	newSize.y = Screen.height / defaultSize.y;
}

void OnGUI()
{
	//Now we draw the size that we would if it were 800x600 and multiply it by the difference.
	GUI.DrawTexture(ResizeGUI(100,100,100,100), texture);
}

Rect ResizeGUI(float x,float y,float width,float height)
{
	x *= newSize.x;
	y *= newSize.y;
	width *= newSize.x;
	height *= newSize.y;

	return new Rect(x,y,width,height); //edit: added new in so it works now.
	//This will return the new size based on percentage, the rect will now be close to 75x75x75x75. 
}

Note that this was just wrote up in notepad, so I can’t say it will work 100% because I didn’t test it. Should work though. If not, should be close enough that you can figure it out.

no it’s not working its showing this error:

O-o sorry, forgot the word new before the Rect.

return new Rect(x,y,width,height);

ya its working…