I have an UI Image in my Canvas parent, and a 2D game object (its a 2D game), I want be able to touch the UI image and move or rotate my game object. All i know about touching is this
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
2Dgameobject.transform.Rotate (Vector3.forward * Time.deltaTime * rotationSpeed);
}
But this only works for touching the whole screen, how do do this for the UI image?
So each UI element comes with predefined functions that you can manipulate through your own script such as,
OnPointerEnter() = When your mouse or finger is over the button
OnPointerExit() = When your mouse or finger has exited the button
OnPointerDown() = When the button has been clicked
OnPointerUp() = When the button has been released.
Go to your UI button and add the component “Event Trigger” (I’m pretty sure that is the name of it.) And also add your script defining the above functions that you want to use. Link the events to your script’s proper functions to get the results you want and this will work with a mouse and touch input.
But be sure that whatever functions you are going to link to the events are made public in your script. Otherwise the event trigger will not detect them.