Unity 3D Game Engine – Android – TouchPhase – Screen – Began-Moved-Stationary-Ended-Canceled

This Script check the Touch Phase on entire Screen.
– TouchPhase.Began – A finger touched the screen
– TouchPhase.Moved – A finger moved on the screen
– TouchPhase.Stationary – A finger is touching the screen but hasn’t moved
– TouchPhase.Ended – A finger was lifted from the screen. This is the final phase of a touch
– TouchPhase.Canceled – The system cancelled tracking for the touch

1. Inside Hierarchy create the structure:

– GUI Text (GameObject)
– Main Camera, attach the script ‘TouchPhaseCheck.js’

TouchPhaseCheck.js


#pragma strict

    // Only ANDROID NOT PC NOT IOS
    // Attach this script to the Main Camera
    
// Hierarchy DRAG E DROP over var GUI Text in Inspector
var scoreText : GUIText;

function Start () {
        // initial value
        scoreText.text = "No Touch";
}

function Update () {

	// TouchPhase.Began - A finger touched the screen
	// se c'è un tocco Input.touchCount AND la fase del tocco è quella iniziale TouchPhase.Began
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
	scoreText.text = "TouchPhase.Began";
	}
	
	// TouchPhase.Moved - A finger moved on the screen
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
	scoreText.text = "TouchPhase.Moved";
	}
	
	// TouchPhase.Stationary - A finger is touching the screen but hasn't moved
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {
	scoreText.text = "TouchPhase.Stationary";
	}
	
	// TouchPhase.Ended - A finger was lifted from the screen. This is the final phase of a touch
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
	scoreText.text = "TouchPhase.Ended";
	}
	
	// TouchPhase.Canceled - The system cancelled tracking for the touch
	// This might happen if, for example, the user puts the device to her face 
        // or simultaneously applies more touches than the system can track (the exact number varies with different platforms) 
	// This is the final phase of a touch.
	if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled) {
	scoreText.text = "TouchPhase.Canceled";
	}
}

2. Hierarchy> Main Camera> Inspector> ‘TouchPhaseCheck.js’ DRAG AND DROP GUI Text (GameObject) over var scoreText