C# 如何检测Unity3d上的滑动?只需查看图像
我找到了如何确定顶部或底部的触感,但我需要更多细节。我在照片上展示了我需要的东西。告诉我要找什么C# 如何检测Unity3d上的滑动?只需查看图像,c#,unity3d,unity5,C#,Unity3d,Unity5,我找到了如何确定顶部或底部的触感,但我需要更多细节。我在照片上展示了我需要的东西。告诉我要找什么 请给我一个提示我写了这个未经测试的脚本,希望对你有所帮助 编辑:我在发布这个答案之前没有搜索过,但我认为这样更好 公共类手势识别器:MonoBehavior{ // Min length to detect the Swipe public float MinSwipeLength = 5f; private Vector2 _firstPressPos; private Vector2 _sec
请给我一个提示我写了这个未经测试的脚本,希望对你有所帮助 编辑:我在发布这个答案之前没有搜索过,但我认为这样更好 公共类手势识别器:MonoBehavior{
// Min length to detect the Swipe
public float MinSwipeLength = 5f;
private Vector2 _firstPressPos;
private Vector2 _secondPressPos;
private Vector2 _currentSwipe;
private Vector2 _firstClickPos;
private Vector2 _secondClickPos;
public enum Swipe {
None,
Up,
Down,
Left,
Right,
UpLeft,
UpRight,
DownLeft,
DownRight
};
public static Swipe SwipeDirection;
public float ReturnForce = 10f;
private void Update() {
DetectSwipe();
}
public void DetectSwipe() {
if ( Input.touches.Length > 0 ) {
Touch t = Input.GetTouch( 0 );
if ( t.phase == TouchPhase.Began ) {
_firstPressPos = new Vector2( t.position.x, t.position.y );
}
if ( t.phase == TouchPhase.Ended ) {
_secondPressPos = new Vector2( t.position.x, t.position.y );
_currentSwipe = new Vector3( _secondPressPos.x - _firstPressPos.x, _secondPressPos.y - _firstPressPos.y );
// Make sure it was a legit swipe, not a tap
if ( _currentSwipe.magnitude < MinSwipeLength ) {
SwipeDirection = Swipe.None;
return;
}
_currentSwipe.Normalize();
// Swipe up
if ( _currentSwipe.y > 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f ) {
SwipeDirection = Swipe.Up;
}
// Swipe down
else if ( _currentSwipe.y < 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f ) {
SwipeDirection = Swipe.Down;
}
// Swipe left
else if ( _currentSwipe.x < 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f ) {
SwipeDirection = Swipe.Left;
}
// Swipe right
else if ( _currentSwipe.x > 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f ) {
SwipeDirection = Swipe.Right;
}
// Swipe up left
else if ( _currentSwipe.y > 0 && _currentSwipe.x < 0 ) {
SwipeDirection = Swipe.UpLeft;
}
// Swipe up right
else if ( _currentSwipe.y > 0 && _currentSwipe.x > 0 ) {
SwipeDirection = Swipe.UpRight;
}
// Swipe down left
else if ( _currentSwipe.y < 0 && _currentSwipe.x < 0 ) {
SwipeDirection = Swipe.DownLeft;
// Swipe down right
} else if ( _currentSwipe.y < 0 && _currentSwipe.x > 0 ) {
SwipeDirection = Swipe.DownRight;
}
}
} else {
if ( Input.GetMouseButtonDown( 0 ) ) {
_firstClickPos = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
} else {
SwipeDirection = Swipe.None;
//Debug.Log ("None");
}
if ( Input.GetMouseButtonUp( 0 ) ) {
_secondClickPos = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
_currentSwipe = new Vector3( _secondClickPos.x - _firstClickPos.x, _secondClickPos.y - _firstClickPos.y );
// Make sure it was a legit swipe, not a tap
if ( _currentSwipe.magnitude < MinSwipeLength ) {
SwipeDirection = Swipe.None;
return;
}
_currentSwipe.Normalize();
//Swipe directional check
// Swipe up
if ( _currentSwipe.y > 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f ) {
SwipeDirection = Swipe.Up;
Debug.Log( "Up" );
}
// Swipe down
else if ( _currentSwipe.y < 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f ) {
SwipeDirection = Swipe.Down;
Debug.Log( "Down" );
}
// Swipe left
else if ( _currentSwipe.x < 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f ) {
SwipeDirection = Swipe.Left;
Debug.Log( "Left" );
}
// Swipe right
else if ( _currentSwipe.x > 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f ) {
SwipeDirection = Swipe.Right;
Debug.Log( "right" );
} // Swipe up left
else if ( _currentSwipe.y > 0 && _currentSwipe.x < 0 ) {
SwipeDirection = Swipe.UpLeft;
Debug.Log( "UpLeft" );
}
// Swipe up right
else if ( _currentSwipe.y > 0 && _currentSwipe.x > 0 ) {
SwipeDirection = Swipe.UpRight;
Debug.Log( "UpRight" );
}
// Swipe down left
else if ( _currentSwipe.y < 0 && _currentSwipe.x < 0 ) {
SwipeDirection = Swipe.DownLeft;
Debug.Log( "DownLeft" );
// Swipe down right
} else if ( _currentSwipe.y < 0 && _currentSwipe.x > 0 ) {
SwipeDirection = Swipe.DownRight;
Debug.Log( "DownRight" );
}
}
}
}
//检测刷卡的最小长度
公共浮子长度=5f;
专用矢量2_firstPressPos;
专用矢量2_secondPressPos;
专用矢量2_currentSwipe;
私有向量2_firstClickPos;
私有向量2_secondClickPos;
公共枚举刷{
没有一个
向上的
下来,,
左边
正确的,
乌利夫特,
正直的,
向下,
彻头彻尾的
};
公共静态滑动滑动方向;
公众浮力=10f;
私有void更新(){
检测擦拭();
}
public void DetectSwipe(){
如果(Input.touchs.Length>0){
Touch t=Input.GetTouch(0);
if(t.phase==TouchPhase.start){
_firstPressPos=新矢量2(t.position.x,t.position.y);
}
如果(t.phase==TouchPhase.end){
_第二次按压位置=新矢量2(t位置x,t位置y);
_当前滑动=新矢量3(_secondPressPos.x-_firstPressPos.x,_secondPressPos.y-_firstPressPos.y);
//确保这是合法的一击,而不是水龙头
如果(_currentSwipe.magnitude0&&u currentSwipe.x>-0.5f&&u currentSwipe.x<0.5f){
滑动方向=向上滑动;
}
//向下滑动
否则如果(_currentSwipe.y<0&&u currentSwipe.x>-0.5f&&u currentSwipe.x<0.5f){
滑动方向=向下滑动;
}
//左击
否则如果(_currentSwipe.x<0&&u currentSwipe.y>-0.5f&&u currentSwipe.y<0.5f){
滑动方向=向左滑动;
}
//向右滑动
否则如果(_currentSwipe.x>0&&u currentSwipe.y>-0.5f&&u currentSwipe.y<0.5f){
SwipeDirection=Swipe.Right;
}
//向左滑动
否则如果(_currentSwipe.y>0&&u currentSwipe.x<0){
SwipeDirection=Swipe.UpLeft;
}
//向右滑动
否则如果(_currentSwipe.y>0&&u currentSwipe.x>0){
SwipeDirection=垂直滑动;
}
//向左轻扫
否则如果(_currentSwipe.y<0&&u currentSwipe.x<0){
SwipeDirection=Swipe.downlight;
//向右滑动
}否则如果(_currentSwipe.y<0&&u currentSwipe.x>0){
SwipeDirection=Swipe.DownRight;
}
}
}否则{
if(Input.GetMouseButtonDown(0)){
_firstClickPos=newvector2(Input.mousePosition.x,Input.mousePosition.y);
}否则{
滑动方向=滑动。无;
//Debug.Log(“无”);
}
if(Input.GetMouseButtonUp(0)){
_secondClickPos=newvector2(Input.mousePosition.x,Input.mousePosition.y);
_当前滑动=新矢量3(_secondClickPos.x-_firstClickPos.x,_secondClickPos.y-_firstClickPos.y);
//确保这是合法的一击,而不是水龙头
如果(_currentSwipe.magnitude0&&u currentSwipe.x>-0.5f&&u currentSwipe.x<0.5f){
滑动方向=向上滑动;
Debug.Log(“Up”);
}
//向下滑动
否则如果(_currentSwipe.y<0&&u currentSwipe.x>-0.5f&&u currentSwipe.x<0.5f){
滑动方向=向下滑动;
Debug.Log(“Down”);
}
//左击
否则如果(_currentSwipe.x<0&&u currentSwipe.y>-0.5f&&u currentSwipe.y<0.5f){
滑动方向=向左滑动;
Debug.Log(“左”);
}
//向右滑动
否则如果(_currentSwipe.x>0&&u currentSwipe.y>-0.5f&&u currentSwipe.y<0.5f){
SwipeDirection=Swipe.Right;
Debug.Log(“右”);
}//向上向左滑动
否则如果(_currentSwipe.y>0&&u currentSwipe.x<0){
SwipeDirection=Swipe.UpLeft;
Debug.Log(“UpLeft”);
}
//向右滑动
否则如果(_currentSwipe.y>0&&u currentSwipe.x>0){
SwipeDirection=垂直滑动;
Debug.Log(“直立”);
}
//向左轻扫
否则如果(_currentSwipe.y<0&&u currentSwipe.x<0){
SwipeDirection=Swipe.downlight;
Debug.Log(“左下”);
//向右滑动
}否则如果(_currentSwipe.y<0&&u currentSwipe.x>0){
SwipeDirection=Swipe.DownRight;
Debug.Log(“彻头彻尾”);
}
}
}
}
您昨天也可能重复了这个问题,因为同样的原因关闭了这个问题。尝试检查另一个问题,有很多有用的信息可以帮助您开始。我需要检测LeftUP和Left Down/右上和右下/底部(从左到右)/底部(从右到左)和顶部(从右到左)/顶部(从左到右)也许你会看到这个游戏-基本上答案都贴在那里