- I create a new project
- I create a canvas
- I create a panel
- I change the panel rect transform in the inspector to stretch
So, it looks like image 1.
![1]
Then I add this code to the UI panel.
private bool checkFirstTouch = true;
void OnEnable()
{
checkFirstTouch = true;
}
void Update () {
if (checkFirstTouch) {
if (Input.GetMouseButton (0)) {
foreach (Touch touch in Input.touches) {
if (gameObject.GetComponent<RectTransform>().rect.Contains(touch.position)) {
print ("Touched.");
checkFirstTouch = false;
}
}
}
}
}
My problem here is, that when I touch the screen on my mobile device, it’s only working when I touch the left bottom corner. Please look image 2.
Why?
Why the whole screen is not working? Because the panel is stretched?
Must I multiply it with canvas scaler or what?
My canvas looks like this:
[Canvas]: Screen Space Overlay
[CanvasScaler]:
UI Scale Mode: Scale with Screen Size
Reference Resolution: X: 800, Y: 480
Screen Match Mode: Match Width Or Height
Match: 0.5
I’m using Unity 4.7.0.
EDIT:
If I print “gameObject.GetComponent ().rect”, I get this:
(x:-400.00, y:-240.00, width:800.00, height:480.00)