zoukankan      html  css  js  c++  java
  • [Unity移动端]Touch类

    Touch类的信息只能在移动端(触摸屏)上能够获取,在编辑器上是不能获取到的。因此,为了方便测试,可以打包apk后在模拟器上跑:

    unity打包apk:https://www.jianshu.com/p/3c67fbfbb67c

    一.常用api

    1.Input.touches:返回上一帧中所有的触摸信息。每一条触摸信息代表着一只手指在屏幕上的触碰状态。因为是一帧的触摸信息,所以建议在Update中调用。长度为1时,表示1只手指在屏幕上,如此类推。

    2.Input.GetTouch:返回指定的一条触摸信息。一般传0,表示返回第一条触摸信息,即在屏幕上的第一只手指的触摸信息。

    3.TouchPhase:触摸状态。其取值为:Began(手指开始触摸屏幕)、Moved(手指在屏幕上移动)、Stationary(手指触摸屏幕,但并没有移动)、Ended(手指从屏幕上移开。这是一个触摸的最后状态)、Canceled(系统取消跟踪触摸,如用户把屏幕放到他脸上或超过五个接触同时发生(个数根据平台而定)。这是一个触摸的最后状态)

    二.测试

    代码如下:

     1 using System.Collections.Generic;
     2 using UnityEngine;
     3 
     4 public class TestTouch : MonoBehaviour {
     5 
     6     private string touchesStr;//当前记录
     7     private List<string> logList = new List<string>();//历史记录
     8     private Vector2 scrollPosition = Vector2.zero;
     9 
    10     private void Update()
    11     {
    12         Touch[] touches = Input.touches;
    13         touchesStr = string.Format("Input.touches({0}):", touches.Length);
    14 
    15         for (int i = 0; i < touches.Length; i++)
    16         {
    17             Touch touch = touches[i];
    18             string pos = touch.position.ToString();
    19             string phase = touch.phase.ToString();
    20             string content = string.Format("{0},{1}", pos, phase);
    21             touchesStr = touchesStr + content;
    22         }
    23 
    24         if (touches.Length > 0)
    25         {
    26             AddLog(touchesStr);
    27         }
    28     }
    29 
    30     private void OnGUI()
    31     {
    32         GUILayout.Label(touchesStr);
    33 
    34         scrollPosition = GUILayout.BeginScrollView(scrollPosition, true, true, GUILayout.Width(Screen.width), GUILayout.Height(Screen.height / 2));
    35         for (int i = 0; i < logList.Count; i++)
    36         {
    37             GUILayout.Label(logList[i]);
    38         }
    39         GUILayout.EndScrollView();
    40 
    41         if (GUILayout.Button("清除log"))
    42         {
    43             ClearLog();
    44         }
    45     }
    46 
    47     void AddLog(string str)
    48     {
    49         logList.Add(str);
    50     }
    51 
    52     void ClearLog()
    53     {
    54         logList = new List<string>();
    55     }
    56 }

    1.点击

    2.拖拽

  • 相关阅读:
    electron调用c#动态库
    Mybatis使用自定义类型转换Postgresql
    Spring Boot Security And JSON Web Token
    从零开始把项目发布到NPM仓库中心
    从零开始把项目发布到Nuget仓库中心
    从零开始把项目发布到maven仓库中心
    vue项目中如何在外部js中例如utils.js直接调用vue实例及vue上挂在的方法
    vue单页应用在页面刷新时保留状态数据的方法
    Vue watch 监听复杂对象变化,oldvalue 和 newValue 一致的解决办法。
    vue项目的登录跳转代码
  • 原文地址:https://www.cnblogs.com/lyh916/p/8228317.html
Copyright © 2011-2022 走看看