1. 程式人生 > >unity中對安卓觸控函式的測試

unity中對安卓觸控函式的測試

最近對Touch類研究了一下,所有測試都是在安卓上測試,iOS未知,上程式碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using UnityEngine.UI;
/// <summary>
/// 對Touch類的研究
/// </summary>
public class TouchScreen : MonoBehaviour {

    private MeshRenderer cube;
    private Text _text;
Touch[] touch1; // private bool isShow = false; // private PlayVideo playV; // Use this for initialization void Start () { cube = GameObject.Find("Cube").GetComponent<MeshRenderer>(); _text = GameObject.Find("Text").GetComponent<Text>(); // playV = GameObject.Find
("ARCamera").GetComponent<PlayVideo>(); } // Update is called once per frame void Update () { // int touchNum = Input.touchCount; //只讀屬性,就是有幾個點觸控在螢幕上,兩個手指一起按在螢幕上就是2,類推; #region //if (touchNum == 1) //{ // cube.material.color = Color.red; //} //if (touchNum == 2
) //{ // cube.material.color = Color.yellow; //} //if (touchNum == 3) //{ // cube.material.color = Color.green; //} //if (touchNum == 4) //{ // cube.material.color = Color.black; //} // _text.text = touchNum.ToString(); //將數值轉換成等效的字串形式 #endregion // Touch touch1 = Input.GetTouch(0); //官方說明:返回一個存放觸控資訊的物件;真機測試的結果就是 在已經觸控的幾個點中,索引為1則代表存放第二個觸控物件的資訊,可以對第二個觸控物件進行操作 #region //if (touch1.tapCount == 1)//這個就是連擊的次數,雙擊為2,三擊為3,類推 //{ // cube.material.color = Color.red; //} //if (touch1.tapCount == 2) //{ // cube.material.color = Color.yellow; //} //if (touch1.tapCount == 3) //{ // cube.material.color = Color.black; //} //if (touch1.phase == TouchPhase.Moved) //觸控時並移動 //{ // cube.material.color = Color.red; //} //if (touch1.phase == TouchPhase.Began) //觸控開始,開始狀態 //{ // cube.material.color = Color.yellow; //} //if (touch1.phase == TouchPhase.Ended) //觸控結束,這是一個觸控的最後狀態 //{ // cube.material.color = Color.black; //} //if (touch1.phase == TouchPhase.Stationary) //長按不動 TouchPhase觸控的幾種狀態 列舉 //{ // cube.material.color = Color.blue; //} //if (touch1.phase == TouchPhase.Canceled) //系統取消跟蹤觸控 //{ // cube.material.color = Color.red; //} //_text.text = touch1.tapCount.ToString(); #endregion // cube.transform.position = touch1.deltaPosition; //deltaposition是一個二維向量,表示最近更新中記錄的觸控位置和上一個更新中記錄的位置之間的差異; // _text.text = touch1.deltaPosition.ToString(); //int fingerout = 0; //touch1 = Input.touches; //返回最後一幀中所有觸控物件的返回列表(只讀) //foreach (Touch item in Input.touches) //{ // if (item.phase != TouchPhase.Canceled && item.phase != TouchPhase.Ended) // { // fingerout++; // } //} // cube.transform.position = touch1[0].position; //接觸點的畫素座標,豎屏時的左上角(橫屏時的左下角)為原點,最大座標就是手機解析度了 // cube.transform.position = touch1[0].rawPosition; //就是觸控點剛接觸的位置,不會隨著手指的移動而其值改變,也是豎屏的左上角為原點,最大座標是解析度; // cube.transform.position = Input.mousePosition; //接觸點的畫素座標 too // cube.transform.position = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); // cube.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 pos = new Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,10); cube.transform.position = Camera.main.ScreenToWorldPoint(pos); ; //Camera.main.ScreenToWorldPoint(Vector3 p) ,如果p.z=0,則該函式返回的總是攝像機的世界座標位置,所以必須帶深度 // _text.text = touch1.deltaTime.ToString(); //自上一次記錄的觸控值更改以來,已經過的時間值;若觸控值沒有改變則時間為0; // _text.text = cube.transform.position.ToString(); _text.text = cube.transform.position.ToString(); } public void ExitGame() { Application.Quit(); } }