- Home /
Scaled GUI
I have a GUI Texture, when I click on it a GUI Button should appear. It all worked well until I put the "scaled code". I found the scaling part on some other question.
How can I make it work? Here's my code.
private bool displayMusic = false;
public GUITexture myTexture;
private Vector3 scale;
int originalWidth = 640;
int originalHeight = 400;
void OnGUI()
{
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
Matrix4x4 svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
if (Input.GetMouseButton (0) && myTexture.HitTest (Input.mousePosition))
{
displayMusic=true;
}
if (displayMusic)
{
if (AudioListener.volume != 0 && GUI.Button (new Rect(1005,130,135,30), "Sound effects: ON"))
{
AudioListener.volume = 0;
displayMusic=false;
}
else
if (AudioListener.volume == 0 && GUI.Button (new Rect(1005,130,135,30), "Sound effects: OFF"))
{
AudioListener.volume = 100;
displayMusic=false;
}
}
GUI.matrix = svMat;
}
}
When you say it doesn't work, I'm not quite sure what you mean. Does it generate an error? Does it not display at all? Or does it display strangely?
When I click on the GUI Texture it doesn't happen anything, but it should show the GUI Button.
Answer by Graham-Dunnett · Jul 22, 2014 at 02:07 PM
Matrix4x4.TRS()
changes the way that rendering happens. It does not change the way that input touch and mouse position get computed. Don't use this approach to resize your GUI elements.
Well.. Then, how can I make my GUI elements don't change position on different resolutions?