I add UI object Button and add the C# script with public function. To button I add component Event Trigger, do events(Pointer Click and Pointer Down) and redirect to my function public void onClick()
On PC code works, but when I upload game to android and touch the object, code not works.
How to do onTouch event?
3 Answers
I think OnMouseDown will check every frame if there is a mouse input it's like update so you have to cheek touch in Update & with touch you will have more control like Touch Phase to detect if the touch begins or lifted or moved etc... you need to check
if(input.touchCount > 0)
void Update() { if (Input.touchCount > 0){ print("exist a touch"); if(Input.GetTouch(0).phase == TouchPhase.Began){ print("Touch begans"); } if(Input.GetTouch(0).phase == TouchPhase.Ended){ print("Touch Ended"); } } }
chCount > 0)& inside this you can cheek for touch Phase Touches aren't Clicks
In order to handle touch input you need to check Input.touchCount and then query each touch with Input.GetTouch. Note that each Touch has an an ID that will be unique per finger and consistent across frames.
There are no easy OnClick like methods for touches, as touches can be a lot more complex (tap, long tap, drag, etc), so you will have to check inside Update() and handle the conversion from touch data to mouse analogs yourself.
You can use the following steps to handle touch events in your Unity3D application.
Call
Input.GetTouchto obtain aTouchstruct.Input.GetTouchreturns Touch for a selected screen touch (for example, from a finger or stylus). Touch describes the screen touch. The index argument selects the screen touch.Input.touchCountprovides the current number of screen touches. IfInput.touchCountis greater than zero, the GetTouch index sets which screen touch to check. Touch returns a struct with the screen touch details. Each extra screen touch uses an increasingInput.touchCount.GetTouchreturns a Touch struct. Use zero to obtain the first screen touch. As an example,Touchincludes a position in pixels.
For Example:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{ public GameObject projectile; public GameObject clone; void Update() { for (int i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject; } } }
}In this example, we are trying to instantiate an object on touch.
You can also use the following template to handle the touch interactions.
void Update () { // Handle native touch events foreach (Touch touch in Input.touches) { HandleTouch(touch.fingerId, Camera.main.ScreenToWorldPoint(touch.position), touch.phase); } // Simulate touch events from mouse events if (Input.touchCount == 0) { if (Input.GetMouseButtonDown(0) ) { HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Began); } if (Input.GetMouseButton(0) ) { HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Moved); } if (Input.GetMouseButtonUp(0) ) { HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Ended); } }
}
private void HandleTouch(int touchFingerId, Vector3 touchPosition, TouchPhase touchPhase) { switch (touchPhase) { case TouchPhase.Began: // TODO break; case TouchPhase.Moved: // TODO break; case TouchPhase.Ended: // TODO break; }
}For more: