我是靠谱客的博主 怕孤独皮皮虾,最近开发中收集的这篇文章主要介绍Unity 手指上下左右滑动的判定,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

using UnityEngine;
using System.Collections;
public class jarodInputController : MonoBehaviour {
private float fingerActionSensitivity = Screen.width * 0.05f; //手指动作的敏感度,这里设定为 二十分之一的屏幕宽度.
//
private float fingerBeginX;
private float fingerBeginY;
private float fingerCurrentX;
private float fingerCurrentY;
private float fingerSegmentX;
private float fingerSegmentY;
//
private int fingerTouchState;
//
private int FINGER_STATE_NULL = 0;
private int FINGER_STATE_TOUCH = 1;
private int FINGER_STATE_ADD = 2;
// Use this for initialization
void Start ()
{
fingerActionSensitivity = Screen.width * 0.05f;
fingerBeginX = 0;
fingerBeginY = 0;
fingerCurrentX = 0;
fingerCurrentY = 0;
fingerSegmentX = 0;
fingerSegmentY = 0;
fingerTouchState = FINGER_STATE_NULL;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Mouse0))
{
if(fingerTouchState == FINGER_STATE_NULL)
{
fingerTouchState = FINGER_STATE_TOUCH;
fingerBeginX = Input.mousePosition.x;
fingerBeginY = Input.mousePosition.y;
}
}
if(fingerTouchState == FINGER_STATE_TOUCH)
{
fingerCurrentX = Input.mousePosition.x;
fingerCurrentY = Input.mousePosition.y;
fingerSegmentX = fingerCurrentX - fingerBeginX;
fingerSegmentY = fingerCurrentY - fingerBeginY;
}
if (fingerTouchState == FINGER_STATE_TOUCH)
{
float fingerDistance = fingerSegmentX*fingerSegmentX + fingerSegmentY*fingerSegmentY;
if (fingerDistance > (fingerActionSensitivity*fingerActionSensitivity))
{
toAddFingerAction();
}
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
fingerTouchState = FINGER_STATE_NULL;
}
}
private void toAddFingerAction()
{
fingerTouchState = FINGER_STATE_ADD;
if (Mathf.Abs (fingerSegmentX) > Mathf.Abs (fingerSegmentY))
{
fingerSegmentY = 0;
}
else
{
fingerSegmentX = 0;
}
if (fingerSegmentX == 0)
{
if (fingerSegmentY > 0)
{
Debug.Log ("up");
}
else {
Debug.Log ("down");
}
}
else if(fingerSegmentY == 0)
{
if(fingerSegmentX > 0)
{
Debug.Log ("right");
}
else
{
Debug.Log("left");
}
}
}
}

 

最后

以上就是怕孤独皮皮虾为你收集整理的Unity 手指上下左右滑动的判定的全部内容,希望文章能够帮你解决Unity 手指上下左右滑动的判定所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部