【转】Unity 之 移动设备的触控操作

时间:2023-03-08 21:09:50

http://blog.****.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、相关截图:
【转】Unity 之 移动设备的触控操作
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.****.net/anyuanlzh/article/details/18367941