http://blog.csdn.net/anyuanlzh/article/details/18367941

这篇博文将简单的记录,如何用unity处理在移动设备上的触控操作。
    iOSAndroid设备能够支持多点触控。在unity中你可以通过Input.touches属性集合访问在最近一帧中触摸在屏幕上的每一根手指的状态数据。简单的触控响应实现起很简单,不过一些复杂的触控响应或触控手势什么的,还是使用一些第三方的插件吧,当然你也可以自己封装。不管什么情况,了解决unity原生api还是非常必要的。

相关的api
    1、Toch类:用来记录一个手指触摸在屏幕上的状态与位置的各种相关数据。这其它中只有两个属性是你要注意的,就是Touch.fingerId和Touch.tapCount。
               Touch.fingerId: 一个Touch的标识。Input.touches数组中的同一个索引在两帧之前,指向的可不一定是同一个Touch。用来标识某个具体的touch一定要用fingerId,在分析手势时、或处理多点触控时,fingerId是非常重要的。
               Touch.tapCount: 点击的总人数,这个属性可以用来模拟“双击”的效果。
               这里先普及一个概念吧,一个touch的生命周期是:当一个手指接触到触屏时,产生一个Touch,并分配它一个fingerId,到这个手指离开触屏这个touch就有了。在它整个生命周期里,它的figerId是不会变的。死掉的touch所使用过fingerId当也会被之后的touch多次使用。ps:其实也不是手指一离开,这个touch就立马死掉。还有一种特殊的情况就是:在手指敲开的地方,在一个很短的时间内,一个手指又回到这个地方的话,这个touch没不会死掉,这个也是Touch.tapCount属于的由来吧。这个touch生命周期,是我自己理解的,实际情况是不是这样就不知道了,料想也不会差的太多。
    2、TouchPhase枚举:它列表描述了手指触摸的几种状态。对应Touch类中的phase属性。这是状态分别是:Began、Move、Stationary、Ended、Canceled。
    3、Input.touches:一个Touch数组,代表着当前帧,所有手指在屏幕上的触碰状态与相关数据。(只读)
    4、Input.touchCount: 触摸数量,相当于Input.touches.Length。(只读)
    5、Input.multiTouchEnabled:设置与指示当前系统(注意不是指设备哦!)是否启用多点触控。不过这个属性有点怪,我在电脑上测试给它赋false不会报错但完全是没有用的,它的值一值是true. 不过在我的安卓手机上测试是正常的!Ture表示支持多点触控(一般是5点);False表示单点触控。
    6、Input.GetTouch(int index):安索引值获取一个Touch对象。

下面一个测试Demo
1、简述:
         这个Demo主要是让你的触摸可视化的显示在你的手机(或平板)屏幕上;
         并实时记录和显示了手指在屏幕上的状态和相关数据。
2、相关截图:

3、脚本就一个

  1. /*
  2. * author: AnYuanLzh
  3. * date:   2014/01/18
  4. * */
  5. using UnityEngine;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. public class MyTouch : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 定义的一个手指类
  12. /// </summary>
  13. class MyFinger
  14. {
  15. public int id = -1;
  16. public Touch touch;
  17. static private List<MyFinger> fingers = new List<MyFinger>();
  18. /// <summary>
  19. /// 手指容器
  20. /// </summary>
  21. static public List<MyFinger> Fingers
  22. {
  23. get
  24. {
  25. if(fingers.Count==0)
  26. {
  27. for(int i=0; i<5; i++)
  28. {
  29. MyFinger mf = new MyFinger();
  30. mf.id = -1;
  31. fingers.Add(mf);
  32. }
  33. }
  34. return fingers;
  35. }
  36. }
  37. }
  38. // 小圈圈:用来实时显示手指触摸的位置
  39. GameObject[] marks = new GameObject[5];
  40. public GameObject markPerfab = null;
  41. // 粒子效果:来所显示手指手动的大概路径
  42. ParticleSystem[] particles = new ParticleSystem[5];
  43. public ParticleSystem particlePerfab = null;
  44. // Use this for initialization
  45. void Start ()
  46. {
  47. // init marks and particles
  48. for(int i=0; i<MyFinger.Fingers.Count; i++)
  49. {
  50. GameObject mark = Instantiate(markPerfab, Vector3.zero, Quaternion.identity) as GameObject;
  51. mark.transform.parent = this.transform;
  52. mark.SetActive(false);
  53. marks[i] = mark;
  54. ParticleSystem particle = Instantiate(particlePerfab, Vector3.zero, Quaternion.identity) as ParticleSystem;
  55. particle.transform.parent = this.transform;
  56. particle.Pause();
  57. particles[i] = particle;
  58. }
  59. }
  60. // Update is called once per frame
  61. void Update ()
  62. {
  63. Touch[] touches = Input.touches;
  64. // 遍历所有的已经记录的手指
  65. // --掦除已经不存在的手指
  66. foreach(MyFinger mf in MyFinger.Fingers)
  67. {
  68. if(mf.id == -1)
  69. {
  70. continue;
  71. }
  72. bool stillExit = false;
  73. foreach(Touch t in touches)
  74. {
  75. if(mf.id == t.fingerId)
  76. {
  77. stillExit = true;
  78. break;
  79. }
  80. }
  81. // 掦除
  82. if(stillExit == false)
  83. {
  84. mf.id = -1;
  85. }
  86. }
  87. // 遍历当前的touches
  88. // --并检查它们在是否已经记录在AllFinger中
  89. // --是的话更新对应手指的状态,不是的放放加进去
  90. foreach(Touch t in touches)
  91. {
  92. bool stillExit = false;
  93. // 存在--更新对应的手指
  94. foreach(MyFinger mf in MyFinger.Fingers)
  95. {
  96. if(t.fingerId == mf.id)
  97. {
  98. stillExit = true;
  99. mf.touch = t;
  100. break;
  101. }
  102. }
  103. // 不存在--添加新记录
  104. if(!stillExit)
  105. {
  106. foreach(MyFinger mf in MyFinger.Fingers)
  107. {
  108. if(mf.id == -1)
  109. {
  110. mf.id = t.fingerId;
  111. mf.touch = t;
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. // 记录完手指信息后,就是响应相应和状态记录了
  118. for(int i=0; i< MyFinger.Fingers.Count; i++)
  119. {
  120. MyFinger mf = MyFinger.Fingers[i];
  121. if(mf.id != -1)
  122. {
  123. if(mf.touch.phase == TouchPhase.Began)
  124. {
  125. marks[i].SetActive(true);
  126. marks[i].transform.position = GetWorldPos(mf.touch.position);
  127. particles[i].transform.position = GetWorldPos(mf.touch.position);
  128. }
  129. else if(mf.touch.phase == TouchPhase.Moved)
  130. {
  131. marks[i].transform.position = GetWorldPos(mf.touch.position);
  132. if(!particles[i].isPlaying)
  133. {
  134. particles[i].loop = true;
  135. particles[i].Play();
  136. }
  137. particles[i].transform.position = GetWorldPos(mf.touch.position);
  138. }
  139. else if(mf.touch.phase == TouchPhase.Ended)
  140. {
  141. marks[i].SetActive(false);
  142. marks[i].transform.position = GetWorldPos(mf.touch.position);
  143. particles[i].loop = false;
  144. particles[i].Play();
  145. particles[i].transform.position = GetWorldPos(mf.touch.position);
  146. }
  147. else if(mf.touch.phase == TouchPhase.Stationary)
  148. {
  149. if(particles[i].isPlaying)
  150. {
  151. particles[i].Pause();
  152. }
  153. particles[i].transform.position = GetWorldPos(mf.touch.position);
  154. }
  155. }
  156. else
  157. {
  158. ;
  159. }
  160. }
  161. // exit
  162. if(Input.GetKeyDown(KeyCode.Home) || Input.GetKeyDown(KeyCode.Escape))
  163. {
  164. Application.Quit();
  165. }
  166. //      // test
  167. //      if(Input.GetMouseButtonDown(0))
  168. //      {
  169. //          GameObject mark = Instantiate(markPerfab, Vector3.zero, Quaternion.identity) as GameObject;
  170. //          mark.transform.parent = this.transform;
  171. //          mark.transform.position = GetWorldPos(Input.mousePosition);
  172. //
  173. //          ParticleSystem particle = Instantiate(particlePerfab, Vector3.zero, Quaternion.identity) as ParticleSystem;
  174. //          particle.transform.parent = this.transform;
  175. //          particle.transform.position = GetWorldPos(Input.mousePosition);
  176. //          particle.loop = false;
  177. //          particle.Play();
  178. //      }
  179. }
  180. /// <summary>
  181. /// 显示相关高度数据
  182. /// </summary>
  183. void OnGUI()
  184. {
  185. GUILayout.Label("支持的手指的数量:" + MyFinger.Fingers.Count);
  186. GUILayout.BeginHorizontal(GUILayout.Width(Screen.width));
  187. for(int i=0; i< MyFinger.Fingers.Count; i++)
  188. {
  189. GUILayout.BeginVertical();
  190. MyFinger mf = MyFinger.Fingers[i];
  191. GUILayout.Label("手指" + i.ToString());
  192. if(mf.id != -1)
  193. {
  194. GUILayout.Label("Id: " + mf.id);
  195. GUILayout.Label("状态: " + mf.touch.phase.ToString());
  196. }
  197. else
  198. {
  199. GUILayout.Label("没有发现!");
  200. }
  201. GUILayout.EndVertical();
  202. }
  203. GUILayout.EndHorizontal();
  204. }
  205. public Vector3 GetWorldPos(Vector2 screenPos)
  206. {
  207. return Camera.main.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Camera.main.nearClipPlane + 10));
  208. }
  209. }

4、相关下载(点下面的链接)
           项目文件 和 发布好的apk,下载点这里(不需要下载积分)。
   注:这个apk对Android系统的要求是Android4.0及以上版本。

如有转载请在文首注明出处,AnYuanLzh:http://blog.csdn.net/anyuanlzh/article/details/18367941

【转】Unity 之 移动设备的触控操作的更多相关文章

  1. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件

    原文  MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...

  2. Windows 8.1 应用开发 – 触控操作

    与WPF相同Windows 8.1应用中也具有高级触控操作(Manipulation),其中包含了三种常见的触屏手势:平移.缩放.旋转,通过以下四种事件可为控件实现各种触控操作:Manipulatio ...

  3. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇04:如何实现触控操作》

    4.如何实现触控操作 触控操作概述: 随着APPLE.Samsung.SONY等众多公司,将掌机.电脑和手机等产品在触控领域的不断探索,以触控为操作的机型越来越多的被投放到市场当中.触控游戏.触控软件 ...

  4. 与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触控

    原文:与众不同 windows phone (25) - Input(输入)之捕获 UIElement 之外的触控操作, Silverlight 方式捕获手势操作, XNA 方式捕获手势操作, 多点触 ...

  5. amazeUI tab禁止左右滑动(触控操作)

    参考:http://amazeui.clouddeep.cn/javascript/tabs/ 效果: html: <!DOCTYPE html> <html> <hea ...

  6. 小强的HTML5移动开发之路(52)——jquerymobile中的触控交互

    当使用移动设备进行触控操作时,最常用的就是轻击.按住屏幕或者手势操作,jQuery Mobile可以通过绑定的触控事件来响应使用者的特定触控行为. 一.轻击与按住 直接上代码(一切皆在代码中,细细品吧 ...

  7. Windows phone 8 学习笔记(1) 触控输入(转)

    Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此在输入方式上引入一套全新的触控操作方式,我们需要重新定义相关的事 ...

  8. Windows phone 8 学习笔记(1) 触控输入

    原文:Windows phone 8 学习笔记(1) 触控输入 Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此 ...

  9. 安卓Tv开发(一)移动智能电视之焦点控制(触控事件)

    前言:移动智能设备的发展,推动了安卓另一个领域,包括智能电视和智能家居,以及可穿戴设备的大量使用,但是这些设备上的开发并不是和传统手机开发一样,特别是焦点控制和用户操作体验风格上有很大的区别,本系列博 ...

随机推荐

  1. HDU 4345 Permutation dp

    Permutation Problem Description There is an arrangement of N numbers and a permutation relation that ...

  2. HDU 4974 Dracula and Ethan 优先队列

    Dracula and Ethan Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching competition ...

  3. loj 1026( tarjan + 输出割边 )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1026 思路:Tarjan 算法简单应用.割边的特点:low[v]>dfn[u]( ...

  4. 不能继承OrmLiteBaseActivity时,这样获取getHelper

    private DatabaseHelper databaseHelper = null; public DatabaseHelper getHelper() { if (databaseHelper ...

  5. Hark的数据结构与算法练习之多路归并排序

    算法说明 多路归并排序也叫k路归并排序,实际上是归并排序的扩展版,同样也是归并排序的一种,通常的应用场景的针对大数据量的排序. 实现过程: 1.从字面可以看出,多路归并就是将待排的大数据量分成K路,然 ...

  6. HTML-Canvas03

    颜色合成 globalCompositeOperation 属性: //先绘制一个图形. ctx.fillStyle = "#00ff00"; ctx.fillRect(10,10 ...

  7. Sprint第一个冲刺(第十一天)

    一.Sprint介绍 修改登录信息界面(修改用户名.密码.邮箱.电话.年龄),且同步到云端:修改Item布局:增添设置页. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:

  8. 简单几何(线段相交) POJ 2653 Pick-up sticks

    题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...

  9. ps怎么把白色背景变透明

  10. 分类 kNN

    #coding=utf-8 from numpy import * import operator from os import listdir import matplotlib import ma ...