- Home /
Scaling the GUI matrix in mobile causes buttons to be off
Hello, I am making a mobile app using unity's GUI exclusively. I am targeting android. I want the app to work on any size screen, so I am scaling GUI.matrix to make the GUI appear the same on any resolution.
private float originalWidth = 1024.0f;
private float originalHeight = 600.0f;
void Start ()
{
scale.x = Screen.width / originalWidth;
scale.y = Screen.height / originalHeight;
scale.z = 1.0f;
}
void OnGUI ()
{
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
// GUI Code
}
This worked great. Then I noticed that when I tried to touch the buttons that I couldn't. I believe the buttons only work when I push the area that the buttons are before they are scaled into place.
After searching a bit I found a bit of a hack that worked pretty good, but only for mouse clicks.
void OnGUI ()
{
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
// The hack
if ( Event.current.type == EventType.Layout && Event.current.clickCount == 0 )
{
GUI.matrix = Matrix4x4.identity;
}
// GUI Code
}
This didn't work for touch, however. I don't really understand how the "hack" worked, but I tried changing it to this.
if ( Event.current.type == EventType.Layout && Input.touchCount == 0 )
{
GUI.matrix = Matrix4x4.identity;
}
That had no effect, however.
So my question is basically, how can I get the scaling to work properly on android and also be able to properly click/touch the buttons?
I am using GUILayout by the way. Not sure if not using it would solve anything.
Did you ever find a solution to this? I'm actually seeing this same issue in my project using the new UI elements. $$anonymous$$y proect was started in Unity 4.6 where one specific user was seeing the problem. Now I've moved up to Unity 5 and he's still seeing the issue!
He's on a "Oneplus One" phone. I have another tester on the same phone who doesn't see the problem.
Answer by alexanderlarsen · Aug 06, 2020 at 09:37 AM
@Jopan Hello from 2020! Did you find a solution to this?
Your answer
Follow this Question
Related Questions
GUI Button for Android 1 Answer
Alternative to GetButtonUp for GUI.Button 1 Answer
GUI scaling on mobile 1 Answer
Android Button Screen 1 Answer
Why i cant press(use) joystick and gui button at the same time ???? 1 Answer