using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Tougchf : MonoBehaviour
{
public Text UiText;
public GameObject PlayerGameObject;
private GameObject go;
private string Info;
private Vector2 orignPoisiton;
// Use this for initialization
void Start ()
{
//将返回键交给安卓使用。
Input.backButtonLeavesApp=true;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Info += "Escape";
}//测试返回键是给unity用的
if (Input.touchCount > 0)
{
Info = string.Empty;
//下面三行同样对移动端有效
//所有touch类在PC端都不生效。
Info += "GetMouseButton(0)" + Input.GetMouseButton(0)+"\n";
Info += "GetAxis(\"Mouse X\")" + Input.GetAxis("Mouse X") + "\n";
Info += "GetAxis(\"Mouse Y\")" + Input.GetAxis("Mouse Y") + "\n";
//手指触摸的个数
Info += "touchCount" + Input.touchCount + "\n";
//Input.touches【i】里面存放着触摸对象的数组
Touch myTouch = Input.touches[0];
//识别当前手指的唯一标志。
Info += "fingerId" + myTouch.fingerId + "\n";
//这次触摸的位置和上次触摸的位置差
Info += "deltaPosition" + myTouch.deltaPosition + "\n";
//记录这次触摸与上次触摸之间的时间差恒为0.0033333334
Info += "deltaTime" + myTouch.deltaTime + "\n";
//触摸屏的次数
Info += "tapCount" + myTouch.tapCount + "\n";
//触摸的状态
Info += "phase" + myTouch.phase + "\n";
//当前touch对象所对应的屏幕坐标
Info += "position" + myTouch.position + "\n";
//当前touch对象所对应的屏幕开始时的坐标,不变的。
Info += "rawPosition" + myTouch.rawPosition + "\n";
switch (myTouch.phase)
{
case TouchPhase.Began:
//Camera.main.ScreenToWorldPoint(myTouch.position) + new Vector3(0, 0, 10)将屏幕坐标转换成世界坐标(以摄像机为例)。摄像机的坐标为(0,0,-10),所以要加一个(0,0,10)
go = Instantiate(PlayerGameObject,Camera.main.ScreenToWorldPoint(myTouch.position) + new Vector3(0, 0, 10), Quaternion.identity);
orignPoisiton = myTouch.position;
break;
case TouchPhase.Moved:
case TouchPhase.Stationary:
go.transform.position = Camera.main.ScreenToWorldPoint(myTouch.position) + new Vector3(0, 0, 10);
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:Destroy(go);
if (orignPoisiton.x > 0 && orignPoisiton.y > 0)
{
Vector2 NowPosition = myTouch.position;
float x = NowPosition.x - orignPoisiton.x;
float y = NowPosition.y - orignPoisiton.y;
if (Mathf.Abs(x) > Mathf.Abs(y)&&Mathf.Abs(y)>0.25)
{
Info += x > 0 ? "dir Right" : "dir Left";
}
else if (Mathf.Abs(x) <= Mathf.Abs(y) && Mathf.Abs(x) > 0.25)
{
Info += y > 0 ? "dir Up" : "dir Down";
}
else
{
Info +="dir UnKnow";
}
}
break;
default:
break;
//throw new ArgumentOutOfRangeException();
}
}
UiText.text = Info;
}
}
安卓的单点触控
最新推荐文章于 2021-05-26 02:23:51 发布