C# 模拟;滑动以解锁";在Unity 3D中

C# 模拟;滑动以解锁";在Unity 3D中,c#,unity3d,C#,Unity3d,我必须在Unity 3D中创建一个“滑动解锁”功能(就像iPhone锁屏中的一样)。我尝试了以下方法: 第1部分-检测刷卡 我能够使用以下代码检测到刷卡: private bool couldBeSwipe = false; private float minSwipeDistance = 14; private float maxSwipeTime = 10; private float comfortZone = 100; private float startTime; private V

我必须在Unity 3D中创建一个“滑动解锁”功能(就像iPhone锁屏中的一样)。我尝试了以下方法:

第1部分-检测刷卡

我能够使用以下代码检测到刷卡:

private bool couldBeSwipe = false;
private float minSwipeDistance = 14;
private float maxSwipeTime = 10;
private float comfortZone = 100;
private float startTime;
private Vector2 startPos;
const int SWIPE_NONE = 0;
const int SWIPE_UP = 1;
const int SWIPE_RIGHT = 2;
const int SWIPE_DOWN = 3;
const int SWIPE_LEFT = 4;
private int swipeDirection = SelectStageScene.SWIPE_NONE;
void Update()中

//输入
swipeDirection=选择stagescene.SWIPE\u NONE;
如果(Input.touchCount>0){
触摸=输入。触摸[0];
开关(接触相){
案例接触阶段。开始:
couldBeSwipe=true;
startPos=touch.position;
startTime=Time.Time;
打破
案例接触阶段。移动:
if((Mathf.Abs(touch.position.y-startPos.y)>舒适区)和&(Mathf.Abs(touch.position.x-startPos.x)>舒适区)){
couldBeSwipe=false;
}
打破
案件审理阶段结束:
float swipeTime=Time.Time-startTime;
浮动开关站姿=(touch.position-startPos).magnity;
float deltaX=touch.position.x-startPos.x;
float deltaY=touch.position.y-startPos.y;
如果(可以擦除&(swipeTimeMinsweepDistance)){
如果(Mathf.Abs(deltaX)>舒适区){
浮动开关方向Y=数学符号(deltaX);
如果(SwipDirectiony==1){
swipeDirection=选择stagescene.SWIPE\u RIGHT;
}否则{
swipeDirection=选择stagescene.SWIPE\u LEFT;
}
}
如果(Mathf.Abs(deltaY)>舒适区){
浮动开关方向Y=数学符号(deltaY);
如果(SwipDirectiony==1){
swipeDirection=选择StageScene.SWIPE\u UP;
}否则{
swipeDirection=选择StageScene.SWIPE_DOWN;
}
}
}else if(swipeTime>maxSwipeTime){
Log(“太慢”);
swipeDirection=选择stagescene.SWIPE\u NONE;
}否则如果(SwipedInstance
第2部分-移动纹理(带轴约束)


有几种方法可以移动纹理,包括
GUI.DragWindow()
GUI.Box()
,等等。但是没有一种方法可以直接移动
纹理。这是沿轴移动纹理的最简单方法。例如,仅在X轴上限制其移动?

关于使用Unity GUI,没有“移动”,每次调用OnGUI()中的GUI元素时,您都会不断地重新绘制或发送重新绘制的消息,例如GUI.Button

如果您想使用OnGUI,我将按照以下方式构造运动:

我已经塑造了你的函数并编写了这个类

using UnityEngine;
using System.Collections;

public class SelectStageScene : MonoBehaviour {

    // You'll want to set this;
    public Texture2D        screenTexture;

    private float           minSwipeDistance        = 14;
    private float           maxSwipeTime            = 10;
    private float           startTime;

    private Vector2         startPos;
    private Vector2         currentPos;

    const int               SWIPE_NONE              = 0;
    const int               SWIPE_UP                = 1;
    const int               SWIPE_RIGHT             = 2;
    const int               SWIPE_DOWN              = 3;
    const int               SWIPE_LEFT              = 4;
    private int             swipeDirection          = SelectStageScene.SWIPE_NONE;

    private Vector2         screenTextureOffset     = Vector2.zero;
    private float           fadeAlpha               = 0f;
    private float           fadeSpeed               = 1f; // How fast the texture fades after swipe.

    public void Update() {
        // If no swipe direction is set.
        if ( swipeDirection == SelectStageScene.SWIPE_NONE ) {
            // To fade back in after swipe (just to complete the loop)
            if ( fadeAlpha > 0 ) {
                fadeAlpha -= Time.deltaTime * fadeSpeed;
            }
            // Getting input
            if ( Input.touchCount > 0 ) {
                Touch touch = Input.touches [0];
                switch ( touch.phase ) {
                case TouchPhase.Began:
                    startPos = touch.position;
                    startTime = Time.time;
                    break;
                case TouchPhase.Moved:
                    currentPos = touch.position;
                    break;
                case TouchPhase.Ended:
                    screenTextureOffset = currentPos - startPos;

                    // By using swipe distance as a magnitude here, regardless of x or y axis, we'll be choosing a swipe direction.
                    // If we were only interested in X axis we would use screenTextureOffset.x instead of swipeDistance
                    if ( Time.time - startTime < maxSwipeTime && ( currentPos - startPos ).magnitude > minSwipeDistance ) {
                        // Find if we've moved more on the x-axis or y-axis.
                        if ( Mathf.Abs( screenTextureOffset.x ) > Mathf.Abs( screenTextureOffset.y ) ) {
                            // x-axis
                            if ( screenTextureOffset.x > 0 ) {
                                swipeDirection = SelectStageScene.SWIPE_RIGHT;
                            } else {
                                swipeDirection = SelectStageScene.SWIPE_LEFT;
                            }
                        } else {
                            // y-axis
                            if ( screenTextureOffset.y > 0 ) {
                                swipeDirection = SelectStageScene.SWIPE_UP;
                            } else {
                                swipeDirection = SelectStageScene.SWIPE_DOWN;
                            }
                        }
                    } else {
                        swipeDirection = SelectStageScene.SWIPE_NONE;
                    }
                    break;
                }
            } else {
                screenTextureOffset *= 1 - Time.deltaTime * fadeSpeed;
            }
        } else {
            // This fades the texture and moves it further in direction of swipe.
            screenTextureOffset *= 1 + Time.deltaTime * fadeSpeed;
            fadeAlpha += Time.deltaTime * fadeSpeed;
            if ( fadeAlpha > 1 ) {
                swipeDirection = SelectStageScene.SWIPE_NONE;
                Debug.Log( "Finished swipe movement : " + swipeDirection );
            }
        }
    }

    public void OnGUI() {
        GUI.color = Color.white - Color.black * fadeAlpha;
        GUI.DrawTexture( new Rect( screenTextureOffset.x, screenTextureOffset.y, Screen.width, Screen.height ), screenTexture );
        /*// Specific axis. Constraining visually leaves easy potential access later on.
        if ( Mathf.Abs( screenTextureOffset.x ) > Mathf.Abs( screenTextureOffset.y ) ) {
            // x-axis
            GUI.DrawTexture( new Rect( screenTextureOffset.x, 0, Screen.width, Screen.height ), screenTexture );
        } else {
            // y-axis
            GUI.DrawTexture( new Rect( 0, screenTextureOffset.y, Screen.width, Screen.height ), screenTexture );
        }
        */
    }

}
使用UnityEngine;
使用系统集合;
公共类选择阶段:单一行为{
//您将要设置此选项;
公共纹理2D屏幕纹理;
专用浮动距离=14;
专用浮点数maxSwipeTime=10;
私人浮动开始时间;
私人矢量2 startPos;
私有向量2 currentPos;
const int SWIPE_NONE=0;
const int SWIPE_UP=1;
const int SWIPE_RIGHT=2;
常量整数向下滑动=3;
const int SWIPE_LEFT=4;
private int swipeDirection=选择stagescene.SWIPE\u NONE;
private Vector2 screenTextureOffset=Vector2.0;
专用浮点数fadeAlpha=0f;
private float fadeSpeed=1f;//刷卡后纹理淡入淡出的速度。
公共无效更新(){
//如果未设置滑动方向。
如果(swipeDirection==选择StageScene.SWIPE\u NONE){
//轻扫后淡入(仅完成循环)
如果(fadeAlpha>0){
fadeAlpha-=Time.deltaTime*fadeSpeed;
}
//获取输入
如果(Input.touchCount>0){
触摸=输入。触摸[0];
开关(触控相){
案例接触阶段。开始:
startPos=touch.position;
startTime=Time.Time;
打破
案例接触阶段。移动:
currentPos=触摸位置;
打破
案件审理阶段结束:
screenTextureOffset=currentPos-startPos;
//通过在这里使用滑动距离作为幅值,无论x轴或y轴如何,我们都将选择滑动方向。
//如果我们只对X轴感兴趣,我们会使用screenTextureOffset.X而不是SwipedStance
if(Time.Time-startTimeminsweep距离){
//找出我们在x轴或y轴上移动的更多。
如果(Mathf.Abs(screenTextureOffset.x)>Mathf.Abs(screenTextureOffset.y)){
//x轴
如果(screenTextureOffset.x>0){
swipeDirection=选择stagescene.SWIPE\u RIGHT;
}否则{
swipeDirection=选择stagescene.SWIPE\u L
using UnityEngine;
using System.Collections;

public class SelectStageScene : MonoBehaviour {

    // You'll want to set this;
    public Texture2D        screenTexture;

    private float           minSwipeDistance        = 14;
    private float           maxSwipeTime            = 10;
    private float           startTime;

    private Vector2         startPos;
    private Vector2         currentPos;

    const int               SWIPE_NONE              = 0;
    const int               SWIPE_UP                = 1;
    const int               SWIPE_RIGHT             = 2;
    const int               SWIPE_DOWN              = 3;
    const int               SWIPE_LEFT              = 4;
    private int             swipeDirection          = SelectStageScene.SWIPE_NONE;

    private Vector2         screenTextureOffset     = Vector2.zero;
    private float           fadeAlpha               = 0f;
    private float           fadeSpeed               = 1f; // How fast the texture fades after swipe.

    public void Update() {
        // If no swipe direction is set.
        if ( swipeDirection == SelectStageScene.SWIPE_NONE ) {
            // To fade back in after swipe (just to complete the loop)
            if ( fadeAlpha > 0 ) {
                fadeAlpha -= Time.deltaTime * fadeSpeed;
            }
            // Getting input
            if ( Input.touchCount > 0 ) {
                Touch touch = Input.touches [0];
                switch ( touch.phase ) {
                case TouchPhase.Began:
                    startPos = touch.position;
                    startTime = Time.time;
                    break;
                case TouchPhase.Moved:
                    currentPos = touch.position;
                    break;
                case TouchPhase.Ended:
                    screenTextureOffset = currentPos - startPos;

                    // By using swipe distance as a magnitude here, regardless of x or y axis, we'll be choosing a swipe direction.
                    // If we were only interested in X axis we would use screenTextureOffset.x instead of swipeDistance
                    if ( Time.time - startTime < maxSwipeTime && ( currentPos - startPos ).magnitude > minSwipeDistance ) {
                        // Find if we've moved more on the x-axis or y-axis.
                        if ( Mathf.Abs( screenTextureOffset.x ) > Mathf.Abs( screenTextureOffset.y ) ) {
                            // x-axis
                            if ( screenTextureOffset.x > 0 ) {
                                swipeDirection = SelectStageScene.SWIPE_RIGHT;
                            } else {
                                swipeDirection = SelectStageScene.SWIPE_LEFT;
                            }
                        } else {
                            // y-axis
                            if ( screenTextureOffset.y > 0 ) {
                                swipeDirection = SelectStageScene.SWIPE_UP;
                            } else {
                                swipeDirection = SelectStageScene.SWIPE_DOWN;
                            }
                        }
                    } else {
                        swipeDirection = SelectStageScene.SWIPE_NONE;
                    }
                    break;
                }
            } else {
                screenTextureOffset *= 1 - Time.deltaTime * fadeSpeed;
            }
        } else {
            // This fades the texture and moves it further in direction of swipe.
            screenTextureOffset *= 1 + Time.deltaTime * fadeSpeed;
            fadeAlpha += Time.deltaTime * fadeSpeed;
            if ( fadeAlpha > 1 ) {
                swipeDirection = SelectStageScene.SWIPE_NONE;
                Debug.Log( "Finished swipe movement : " + swipeDirection );
            }
        }
    }

    public void OnGUI() {
        GUI.color = Color.white - Color.black * fadeAlpha;
        GUI.DrawTexture( new Rect( screenTextureOffset.x, screenTextureOffset.y, Screen.width, Screen.height ), screenTexture );
        /*// Specific axis. Constraining visually leaves easy potential access later on.
        if ( Mathf.Abs( screenTextureOffset.x ) > Mathf.Abs( screenTextureOffset.y ) ) {
            // x-axis
            GUI.DrawTexture( new Rect( screenTextureOffset.x, 0, Screen.width, Screen.height ), screenTexture );
        } else {
            // y-axis
            GUI.DrawTexture( new Rect( 0, screenTextureOffset.y, Screen.width, Screen.height ), screenTexture );
        }
        */
    }

}