- Home /
Button Focus Prevents Touch Raycast (Android C#)
I am working on Android and have the following touch-driven interactions in Update:
void Update()
{
if (Input.touchCount == 1)
{
thisTouch = Input.GetTouch(0);
if (thisTouch.phase == TouchPhase.Moved)
{
if (Mathf.Abs(thisTouch.deltaPosition.y) > 16.0f || Mathf.Abs(thisTouch.deltaPosition.x) > 16.0f)
{
hasMovedThisTouch = true;
}
if (hasMovedThisTouch)
{
DoRotation(thisTouch.deltaPosition);
}
}
else if (thisTouch.phase == TouchPhase.Ended)
{
if (!hasMovedThisTouch)
{
DoClick();
}
hasMovedThisTouch = false;
}
}
else if (Input.touchCount == 2)
{
DoZoom(Input.GetTouch(0), Input.GetTouch(1));
hasMovedThisTouch = true;
if (Input.GetTouch(0).phase == TouchPhase.Ended && Input.GetTouch(1).phase == TouchPhase.Ended)
{
hasMovedThisTouch = false;
}
}
transform.LookAt(target);
}
Which is basically: If one finger is touching and not moving, DoClick. If one finger is touching and moving, DoRotate. If two fingers are touching, DoZoom.
I also have a few buttons on my overlay canvas that affect objects in the scene. This causes a problem.
When I click a button, the next raycast is blocked. I believe that it is because an item in the GUI still has focus. I attempt to solve this by putting GUI.FocusControl(null); at the end of my Update, but this has no effect.
How do I keep my touch interactions prepared to raycast after a button has been pressed?
I suppose I should clarify about Raycast:
Raycast happens in DoClick. DoClick performs an action on the object you are clicking, which is deter$$anonymous$$ed by raycast.
These are the things I have tried that don't work:
Placing any of these in Update() does nothing:
GUI.FocusControl(null);
GUI.FocusControl("");
GUI.UnfocusWindow();
I also made a function I put at the end of every button interaction. It does nothing:
void RefreshGUI()
{
GUI.SetActive(false);
GUI.SetActive(true);
}
In all cases, the next non-button touch will not DoClick().
Answer by Ereptor · Jun 23, 2016 at 02:32 PM
This problem was solved by pulling EventSystem.current.IsPointerOverGameObject(); out of DoClick().
DoClick() must be called from TouchPhase.Ended, but IsPointerOverGameObject() must be called from TouchPhase.Began.
I just had to store an "isClickingButton" bool in my active touch so it could be accessed in both phases.
Also, IsPointerOverGameObject required (thisTouch.fingerID); to work properly.
Your answer
Follow this Question
Related Questions
How to touch select 3D objects 2 Answers
Android 3D Touch for Menu 0 Answers
I am making android fps and my raycast shooting button don't work. What I should do? 1 Answer
How to make this buttons for Android? 1 Answer
Unity UI: neglect transparent area of button and trigger user input underneath it. 2 Answers