- using UnityEngine;
- using System.Collections;
-
-
- public class Test : MonoBehaviour {
- private Vector3 screenPoint;
- private Vector3 offset;
- // Update is called once per frame
- void Update () {
- for (int i = 0; i < Input.touchCount; ++i) {
- if (Input.GetTouch(i).phase == TouchPhase.Began) {
- screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
-
-
- offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, screenPoint.z));
- }
- if(Input.GetTouch(i).phase == TouchPhase.Moved) {
- Vector3 curScreenPoint = new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, screenPoint.z);
-
-
- Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
- transform.position = curPosition;
- }
- }
- }
Unity3D手机中Input类touch详解:
1.Input.touchCount 触摸随之增长,一秒50次增量。
2.Input.GetTouch(0).phase==TouchPhase.Moved 手指滑动中最后一帧滑动的状态是运动的。
3.TouchPhase 触摸的几个状态。
4.Touch.deltaPosition 增量位置(Input.GetTouch(0).deltaPosition)最后一帧滑动的值,只返回xy轴坐标,也可用vector3(z轴为0),所以一般用vector2接收。
- static var aa:int;
- function Update () {
- if(Input.touchCount>0)
- {
- print(Input.touchCount);
- }
- }
- function OnGUI()
- {
- GUI.Label(Rect(34,34,34,34),"sdff");
- }
- touchCount指的是触摸帧的数量。要注意的是:touch事件 只能在模拟器或者真机上运行(已测试通过),大约一秒钟touch不放。touchCount+50次左右。2.Input.touches 触摸列表。
3.让cube随着touch 移动代码: