- Home /
GUITexture and Touch Input
I have a bit of a problem with reading touch input on a GUITexture on an android device. I'm a bit confused by the Unity documentation:
The manual states, under Getting started with android > Mobile Input: "On top of native touch support Unity iOS/Android provides a mouse simulation. You can use mouse functionality from the standard Input class."
Also in the manual in features not supported by Unity Android is states: "OnMouseEnter, OnMouseOver, OnMouseExit, OnMouseDown, OnMouseUp, and OnMouseDrag events are not supported on Android." How do you handle touch input on GUITextures that are intended to be used as buttons (like in a menu)?
I have tried to implement the answer which is shown here, but it didn't work.
What is the best way to implement this if making touchable GuiTexture objects for Android.
Thank you.
I'd like to know the answer to this question too. With GUIText and GUItexture.
Answer by hirenkacha · Jun 18, 2012 at 07:36 AM
You just need to check hitTest of GUITexture with Touch input. If HitTest is true then check if TouchPhase is Began (OnMouseDown) or stationary(OnMouseEnter) or Ended (OnMouseUp).
public class TouchExample : MonoBehaviour
{
public GUITexture myImg;
// Update is called once per frame
void Update ()
{
if (myImg.HitTest(Input.GetTouch(0).position))
{
if(Input.GetTouch(0).phase==TouchPhase.Began)
{
print("Touch has began on image");
}
if(Input.GetTouch(0).phase==TouchPhase.Stationary)
{
print("Touch is on image");
}
if(Input.GetTouch(0).phase==TouchPhase.Moved)
{
print("Touch is moving on image");
}
if(Input.GetTouch(0).phase==TouchPhase.Ended)
{
print("Touch has been ended on image");
}
}
}
would it be necesary to check the amount of touches (to avoid a null reference)?
To avoid null reference. check the condition if(Input.touchCount>0)
.