当前位置: 首页 > article >正文

unity学习27:用Input接口去监测: 单点触摸和多点触摸

目录

1 用Input去监测:触摸 touch

1.1 触摸分类:单点触摸和多点触摸

1.2 用Input.touches[] 获取触摸对象

1.3 获取触摸对象后,再获取触摸对象的属性

1.4 接着获取触摸对象的具体状态

2 触摸的代码今天没有测试,以后再说


1 用Input去监测:触摸 touch

1.1 触摸分类:单点触摸和多点触摸

  • 单点触摸, if(Input.touchCount==1)
  • 多点触摸, if(Input.touchCount==2) 等等
  • 但是需要在 void start() 里要先开启允许多点触摸:Input.multiTouchEnabled=true;

1.2 用Input.touches[] 获取触摸对象

  • 获得触摸对象
  • 需要先 声明对象, Input.touches[0] 的返回值是一个Touch对象
  • Touch touch1=Input.touches[0];
  • 然后才能去使用 touch1对象

  • Input.touches[0]
  • Input.touches[1]
  • 后面的序号是触摸的点

1.3 获取触摸对象后,再获取触摸对象的属性

  • 接着获取触摸对象的属性:触摸位置
  • Debug.Log(touch1.position);

1.4 接着获取触摸对象的具体状态

  • TouchPhase.Began:          //触摸开始时
  • TouchPhase.Moved:          //触摸移动
  • TouchPhase.Stationary:    //触摸静止
  • TouchPhase.Ended:          //触摸结束
  • TouchPhase.Canceled:     //被其他打断了

 

2 触摸的代码今天没有测试,以后再说

  • 主要不好测试触摸
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestTouch : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // 允许多点触摸
        Input.multiTouchEnabled=true;

    }

    // Update is called once per frame
    void Update()
    {
        //判断是否单点触摸
        if(Input.touchCount==1)
        {
            //获得触摸对象
            Touch touch1=Input.touches[0];
            //接着获取触摸对象的属性:触摸位置
            Debug.Log(touch1.position);
            //接着获取触摸对象的具体状态
            switch(touch1.phase)
            {
                case TouchPhase.Began:
                    break;
                case TouchPhase.Moved:
                    break;
                case TouchPhase.Stationary:
                    break;
                case TouchPhase.Ended:
                    break;
                case TouchPhase.Canceled:
                    break;
            }
        }

        //判断是否两点触摸
        if(Input.touchCount==2)
        {
            //获得触摸对象
            Touch touch1=Input.touches[0];
            Touch touch2=Input.touches[1];
            //接着获取触摸对象的属性:触摸位置
            Debug.Log(touch1.position);
            Debug.Log(touch2.position);          
            //接着获取触摸对象的具体状态
            switch(touch1.phase)
            {
                case TouchPhase.Began:
                    break;
                case TouchPhase.Moved:
                    break;
                case TouchPhase.Stationary:
                    break;
                case TouchPhase.Ended:
                    break;
                case TouchPhase.Canceled:
                    break;
            }
        }
    }
}


http://www.kler.cn/a/531141.html

相关文章:

  • 15 刚体变换模块(rigid.rs)
  • 【AI】探索自然语言处理(NLP):从基础到前沿技术及代码实践
  • 使用HttpClient和HttpRequest发送HTTP请求
  • Van-Nav:新年,将自己学习的项目地址统一整理搭建自己的私人导航站,供自己后续查阅使用,做技术的同学应该都有一个自己网站的梦想
  • Upscayl-官方开源免费图像AI增强软件
  • 数据密码解锁之DeepSeek 和其他 AI 大模型对比的神秘面纱
  • 虚幻基础17:动画层接口
  • 【C语言入门】解锁核心关键字的终极奥秘与实战应用(二)
  • js数据结构与算法
  • 房屋中介管理系统的设计与实现
  • 图形学笔记 - 5-光线追踪 - 辐射度量学
  • 高频文件更新数据实时同步实现原理
  • TensorFlow 示例平方米转亩
  • OpenCV:FLANN与暴力特征匹配
  • leetcode——二叉树的最近公共祖先(java)
  • HTML5教程之标签(2)
  • Java_类加载器
  • deep generative model stanford lecture note3 --- latent variable
  • 半导体器件与物理篇7 微波二极管、量子效应和热电子器件
  • SynchronousQueue 与 LinkedBlockingQueue区别及应用场景
  • DeepSeek-R1 低成本训练的根本原因是?
  • CTF-web: php-session临时文件特性
  • Spring MVC学习——发送请求(@RequestMapping注解及请求参数绑定)
  • Android学习19 -- 手搓App
  • The Simulation技术浅析(三):数值方法
  • Hive修复分区