Unity中对安卓触摸函数的测试
发表于2018-08-29
最近对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(); } }
来自:https://blog.csdn.net/Fenglele_Fans/article/details/78770591