Unity 3D Game Engine – Long Touch – Counter Increase Decrease
The counter will increase if you tap or long touch the device
The counter will decrease if you do not touch the device
1. Inside the Hierarchy create the structure:
– Main Camera
– GUI Text
– GameController (Empty Object), attach the ‘TouchController.js’
TouchController.js:
#pragma strict // Hierarchy DRAG E DROP over var GUI Text in Inspector var scoreText : GUIText; // touch counter, private because the users is not be able to change this value private var score : int; function Start () { // The counter initial value is 0 score = 0; scoreText.text = "No Touch:"; } function Update() { // upper and lower limit for score LimitScore (); if (Input.touchCount == 1) { // var score will increase if you tap or long touch the device UpdateScore (); } // every frame score will decrease DecreaseScore (); } function UpdateScore () { // score var increment of +10 // NOTICE!!!! UpdateScore() DEVE essere più veloce di DecreaseScore() altrimenti non si incrementa, // infatti si basano sulla stessa velocità di esecuzione // per rallentare DecreaseScore() si potrebbe creare un ritardo di esecuzione - yield WaitForSeconds (5); score += 10; // scoreText in assigned inside Inspector on GUI Text // change .text property and write it on the display scoreText.text = "Touched: " + score; } function DecreaseScore () { // per rallentare DecreaseScore() si potrebbe creare un ritardo di esecuzione - yield WaitForSeconds (5); // score var decrement of -1 score -= 1; // scoreText in assigned inside Inspector on GUI Text // change .text property and write it on the display scoreText.text = "Touched: " + score; } function LimitScore () { if(score < 1) // minimum score value score = 1; if(score > 1000) // maximum score value, at the end it can reach 999+10=1009 score = 1000; scoreText.text = "Touched: " + score; }
Hierarchy> GameController> TouchController.js assign over var Score Text the GUI Text Object