
本章讲解下Unity中的单点和多点触控。
Input.touchCount获取当前的触摸点数目,若为1则是单点触控,大于1则是多点触控
点击事件用:Input.GetTouch(num).phase == TouchPhase.Began这样的格式
1.using UnityEngine;
2.
3.using System.Collections;
4.
5.public class click2 : MonoBehaviour {
6.
7.//设置点击时显示的图片
8.
9.public Texture2D img;
10.
11.void Start () {
12.
13.}
14.
15.void Update () {
16.
17.}
18.
19.void OnGUI () {
20.
21.//记录当前触控点数目
22.
23.int count = Input.touchCount;
24.
25.//单点触控,首个触控点的标志是0
26.
27.if (count == 1) {
28.
29.//if(Input.GetTouch(0).phase == TouchPhase.Began){}
30.
31.float x = Input.GetTouch(0).position.x;
32.
33.float y = Input.GetTouch(0).position.y;
34.
35.GUI.DrawTexture(new Rect(x,y,100,100),img);
36.
37.}
38.
39.//多点触控,遍历每个触摸点
40.
41.for (int i = 0 ; i < count ; i++){
42.
43.//if(Input.GetTouch(i).phase == TouchPhase.Began){}
44.
45.float x = Input.GetTouch(i).position.x;
46.
47.float y = Input.GetTouch(i).position.y;
48.
49.GUI.DrawTexture(new Rect(x,y,100,100),img);
50.
51.}
52.
53.}
54.
55.}
注意:
记得把脚本文件拖到Camera里面
然后设置脚本的图片,文章出处【狗刨学习网】
