- Home /
GUI Matrix problem
Hi, is there way to scale GUI without aspect ratio deformation? Here's my matrix code:
GUI.matrix = Matrix4x4.TRS(Vector3.zero,Quaternion.identity,new Vector3(Screen.width/960f,Screen.height/640f,1f));
If I use Matrix4x4.Scale
with same width as height (640), that's look fine, but can't be nothing aligned at right side of screen (with Screen.width - xxx
etc.)
Can someone help me? And sorry for my bad English, I am Czech. :)
Answer by aldonaletto · Jun 02, 2013 at 11:29 PM
You always have aspect problems when the X and Y scales are different. A common solution is to calculate only the vertical scale, and assign it to the horizontal scale as well (or vice versa):
float yScale = Screen.height/640f;
GUI.matrix = Matrix4x4.TRS(Vector3.zero,Quaternion.identity,new Vector3(yScale, yScale, 1f));
I know, but if I use this matrix, the star never be on right of screen with Rect(Screen.width - 50, 0, 50, 50)
, look on second screen or here... Here's evident:
The GUI coordinates are also affected by changes in the matrix, thus Screen.width should be divided by yScale in order to compensate this:
float yScale = Screen.height/640f;
GUI.matrix = $$anonymous$$atrix4x4.TRS(Vector3.zero,Quaternion.identity,new Vector3(yScale, yScale, 1f));
...
GUI.DrawTexture(new Rect(Screen.width/yScale - 50, 0, 50, 50), starTexture);