霸气的昵称 发表于 2012-6-13 22:53

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 触摸列表// Prints number of fingers touching the screen
//输出触摸在屏幕上的手指数量

function Update () {
    var fingerCount = 0;
    for (var touch : Touch in Input.touches) {
      if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
            fingerCount++;
    }
    if (fingerCount > 0)
      print ("User has " + fingerCount + " finger(s) touching the screen");
}3.让cube 随着touch 移动代码:static var count:int; //定义touchCount数

var particle_:GameObject;//定义存放cube对象
var touchposition:Vector3; //存储移动三维坐标值
function Update () {
    if(Input.touchCount>0)
    {
            count+=Input.touchCount;
            
    }
    if((Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)) //如果点击手指touch了并且手指touch的状态为移动的
    {
      touchposition=Input.GetTouch(0).deltaPosition;//获取手指touch最后一帧移动的xy轴距离
      particle_.transform.Translate(touchposition.x*0.01,touchposition.y*0.01,0);//移动这个距离
    }
      
   
}
function OnGUI()
{
    GUI.Label(Rect(10,10,100,30),"cishu:"+count.ToString());
    GUI.Label(Rect(10,50,100,30),touchposition.ToString());

}真机或模拟器测试即可


4.获取touch   指定texture的代码:static var aa:int;
var text_ture:GUITexture;
function Update () {
var count=Input.touchCount;
    for(var i:int=0;i<count;i++)
    {
            var touch:Touch=Input.GetTouch(i);
            if(text_ture.HitTest(touch.position)&&touch.phase==TouchPhase.Began)
            {
                aa++;
            }
            
    }
}
function OnGUI()
{
      GUI.Label(Rect(34,34,34,34),aa.ToString());
      
}

oscarlew 发表于 2013-4-4 07:23

这个正需要,学习!!{:soso_e163:}

ringring99 发表于 2017-2-22 20:01

楼主是超人

fengnv314322 发表于 2017-2-22 19:56

好帖就是要顶

liuww 发表于 2017-2-22 20:24

顶顶多好

cbm010 发表于 2017-2-22 20:03

难得一见的好帖

羽の翼 发表于 2017-2-22 19:58

不错不错

Falcon 发表于 2017-3-6 14:57

楼主是超人

阿豆 发表于 2017-3-6 15:16

真心顶

false 发表于 2017-3-6 15:26

难得一见的好帖
页: [1]
查看完整版本: Unity3D Input类中touch详解