2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 【unity3D】单点和多点触控

【unity3D】单点和多点触控

时间:2022-06-02 08:41:08

相关推荐

【unity3D】单点和多点触控

总结:

Input.touchCount获取当前的触摸点数目,若为1则是单点触控,大于1则是多点触控

点击事件用:Input.GetTouch(num).phase == TouchPhase.Began这样的格式

代码:

using UnityEngine;

using System.Collections;

public class click2 : MonoBehaviour {

//设置点击时显示的图片

public Texture2D img;

void Start () {

}

void Update () {

}

void OnGUI () {

//记录当前触控点数目

int count = Input.touchCount;

//单点触控,首个触控点的标志是0

if (count == 1) {

//if(Input.GetTouch(0).phase == TouchPhase.Began){

}

float x = Input.GetTouch(0).position.x;

float y = Input.GetTouch(0).position.y;

GUI.DrawTexture(new Rect(x,y,100,100),img);

}

//多点触控,遍历每个触摸点

for (int i = 0 ; i < count ; i++){

//if(Input.GetTouch(i).phase == TouchPhase.Began){}

float x = Input.GetTouch(i).position.x;

float y = Input.GetTouch(i).position.y;

GUI.DrawTexture(new Rect(x,y,100,100),img);

}

}

}

注意:

记得把脚本文件拖到Camera里面

然后设置脚本的图片

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。