找回密码
 立即注册
查看: 3981|回复: 86

[脚本] Unity3D Input类中touch详解

  [复制链接]
发表于 2012-6-13 22:53 | 显示全部楼层 |阅读模式
--------------------------------
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接收

--------------------------------
  1. static var aa:int;
  2. function Update () {
  3.     if(Input.touchCount>0)
  4.     {
  5.         print(Input.touchCount);
  6.     }
  7. }
  8. function OnGUI()
  9. {
  10.     GUI.Label(Rect(34,34,34,34),"sdff");
  11. }
复制代码
touchCount指的是触摸帧的数量。
要注意的是:touch事件 只能在模拟器或者真机上运行(已测试通过) 大约一秒钟touch不放    touchCount+50次左右


2.Input.touches 触摸列表
  1. // Prints number of fingers touching the screen
  2. //输出触摸在屏幕上的手指数量

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

  2. var particle_:GameObject;//定义存放cube对象
  3. var touchposition:Vector3; //存储移动三维坐标值
  4. function Update () {
  5.     if(Input.touchCount>0)
  6.     {
  7.             count+=Input.touchCount;
  8.             
  9.     }
  10.     if((Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)) //[color=Red]如果点击手指touch了  并且手指touch的状态为移动的[/color]
  11.     {
  12.         touchposition=Input.GetTouch(0).deltaPosition;  //[color=Red]获取手指touch最后一帧移动的xy轴距离[/color]
  13.         particle_.transform.Translate(touchposition.x*0.01,touchposition.y*0.01,0);//[color=Red]移动这个距离[/color]
  14.     }
  15.         
  16.    
  17. }
  18. function OnGUI()
  19. {
  20.     GUI.Label(Rect(10,10,100,30),"cishu:"+count.ToString());
  21.     GUI.Label(Rect(10,50,100,30),touchposition.ToString());

  22. }
复制代码
真机或模拟器测试即可


4.获取touch   指定texture的代码:
  1. static var aa:int;
  2. var text_ture:GUITexture;
  3. function Update () {
  4. var count=Input.touchCount;
  5.     for(var i:int=0;i<count;i++)
  6.     {
  7.             var touch:Touch=Input.GetTouch(i);
  8.             if(text_ture.HitTest(touch.position)&&touch.phase==TouchPhase.Began)
  9.             {
  10.                 aa++;
  11.             }
  12.             
  13.     }
  14. }
  15. function OnGUI()
  16. {
  17.         GUI.Label(Rect(34,34,34,34),aa.ToString());
  18.         
  19. }
复制代码
发表于 2013-4-4 07:23 | 显示全部楼层
这个正需要,学习!!{:soso_e163:}
发表于 2017-2-22 20:01 | 显示全部楼层
楼主是超人
发表于 2017-2-22 19:56 | 显示全部楼层
好帖就是要顶
发表于 2017-2-22 20:24 | 显示全部楼层
顶顶多好
发表于 2017-2-22 20:03 | 显示全部楼层
难得一见的好帖
发表于 2017-2-22 19:58 | 显示全部楼层
不错不错
发表于 2017-3-6 14:57 | 显示全部楼层
楼主是超人
发表于 2017-3-6 15:16 | 显示全部楼层
真心顶
发表于 2017-3-6 15:26 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-6 03:32 , Processed in 0.105590 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表