安卓的单点触控

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值