I have a matrix to scale the GUI according to the resolution the player selects. I have seen this code in many places, so I assume it works nearly flawlessly. Although, I have not been able to find anyone with a problem similar to mine.
My code:
float originalWidth = 1280;
float originalHeight = 720;
Vector3 scale;
void OnGUI()
{
scale.x = (float)Screen.width / originalWidth; // calculate hor scale
scale.y = (float)Screen.height / originalHeight; // calculate vert scale
scale.z = 1;
Matrix4x4 svMat = GUI.matrix; // save current matrix
// substitute matrix - only scale is altered from standard
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
// draw your GUI controls here:
Vector2 screenPos = Event.current.mousePosition;
Vector2 convertedGUIPos = GUIUtility.ScreenToGUIPoint(screenPos);
Debug.Log("Screen: " + screenPos + " GUI: " + convertedGUIPos);
// restore matrix before returning
GUI.matrix = svMat; // restore matrix
}
Debug.Log("Screen: " + screenPos + " GUI: " + convertedGUIPos);
returns the following.
width: 1153 height: 649
Screen: (757.1, -56.6) GUI: (840.5, -62.8 )
Screen: (840.5, -62.8 ) GUI: (933.1, -69.6)
However, it scales from the top left with both Screen and GUI at 0,0 with the GUI gradually getting out of sync with the Screen.
The only time when both are scaled correctly is when the screen is 1280 720
If anyone has any quick solutions, that would be greatly appreciated. Although, I suppose at this point I would take a longer solution as well.
Edit:
Added another set of the screen print out as well as the current screen size.