[Unity3D]做个小Demo学习Input.touches

2023-03-14,,

[Unity3D]做个小Demo学习Input.touches

学不如做,下面用一个简单的Demo展示的Input.touches各项字段,有图有真相。

本项目已发布到Github,地址在(https://github.com/bitzhuwei/AndroidTouchDemo)。

制作Demo

很简单,只需拉一个Text,然后添加一个脚本。

脚本如下。

 using UnityEngine;
using System.Collections; public class DisplayTouchInfo : MonoBehaviour { private UnityEngine.UI.Text lblInfo; // Use this for initialization
void Start () {
this.lblInfo = this.GetComponent<UnityEngine.UI.Text>();
} // Update is called once per frame
void Update () {
var builder = new System.Text.StringBuilder();
builder.AppendLine("touch info:");
builder.AppendLine(string.Format("deltaTime:{0}", Time.deltaTime));
builder.AppendLine(string.Format("touchCount:{0}", Input.touchCount));
builder.AppendLine("i, fingerId, position, rawPosition, deltaPosition, deltaTime, phase, tapCount");
for (int i = ; i < Input.touches.Length; i++)
{
var touch = Input.touches[i];
builder.AppendFormat("[{0}]:{1},{2},{3},{4},{5},{6},{7}", i, touch.fingerId, touch.position, touch.rawPosition, touch.deltaPosition, touch.deltaTime, touch.phase, touch.tapCount);
builder.AppendLine();
}
this.lblInfo.text = builder.ToString();
}
}

使用

注意,必须发布到Android手机上才有效,PC貌似不搭理Input.touchs。

[Unity3D]做个小Demo学习Input.touches的相关教程结束。

《[Unity3D]做个小Demo学习Input.touches.doc》

下载本文的Word格式文档,以方便收藏与打印。