有没有更好的方法在 Unity 中创建这种滑动?

Is there a better way to create this kind of swipe inside Unity?

我正在为我的 2D 游戏制作滑动,下面的代码处理游戏的水平滑动。可以把它想象成在一排门之间的水平滑动(Android-like 滑动):

using UnityEngine;
using System.Collections;

public class DoorController : MonoBehaviour {
    public Camera mainCamera;
    private float speed;
    private float iniCam;
    float delta = 0;
    private Vector2 iniPos = new Vector2 ();
    private Vector2 midPos = new Vector2 ();
    private Vector2 endPos = new Vector2 ();
    private GameObject[] numberOfDoors;
    private bool auth2160;
    private bool auth2880;



    // Use this for initialization
    void Start () {

    }



    IEnumerator swipe0() {
        while (mainCamera.transform.position.x != 0) {
            mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (0, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
            yield return null;
        }
    }
    IEnumerator swipe720() {
        while (mainCamera.transform.position.x != 720) {
            mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (720, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
            yield return null;
        }
    }
    IEnumerator swipe1440() {
        while (mainCamera.transform.position.x != 1440) {
            mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (1440, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
            yield return null;
        }
    }
    IEnumerator swipe2160() {
        while (mainCamera.transform.position.x != 2160) {
            mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (2160, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
            yield return null;
        }
    }
    IEnumerator swipe2880() {
        while (mainCamera.transform.position.x != 2880) {
            mainCamera.transform.position = Vector2.MoveTowards(mainCamera.transform.position, new Vector2 (2880, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
            yield return null;
        }
    }

    // Update is called once per frame
    void Update () {
        numberOfDoors = GameObject.FindGameObjectsWithTag("Porta");
        if (numberOfDoors.Length == 3) {
            auth2160 = false;
            auth2880 = false;
        } else if (numberOfDoors.Length == 4) {
            auth2160 = true;
            auth2880 = false;
        } else if (numberOfDoors.Length == 5) {
            auth2160 = true;
            auth2880 = true;
        }



        if (Input.touchCount > 0) {
            Touch touch = Input.touches[0];
            if(touch.phase == TouchPhase.Began){
                StopAllCoroutines();
                iniCam = mainCamera.transform.position.x;
                iniPos = new Vector2(touch.position.x,0);
                Debug.Log("Posiçao inicial: " + iniPos);

            }
            else if(touch.phase == TouchPhase.Moved){
                midPos = new Vector2(touch.position.x,0);
                Debug.Log("Posiçao durante: " + midPos);
                    //StartCoroutine("swipeLeft");
                    mainCamera.transform.position = new Vector2(iniCam-(midPos.x-iniPos.x)*1.5F,0);


            }
            else if(touch.phase == TouchPhase.Ended){
                endPos = new Vector2(touch.position.x,0);
                delta = iniPos.x-endPos.x; //> 0 tendencia direita;
                Debug.Log("Posiçao final: " + endPos);
                if(mainCamera.transform.position.x < 0 ){
                StartCoroutine("swipe0");
                }else if(mainCamera.transform.position.x >= 0 && mainCamera.transform.position.x < 360 && delta < 0){
                    StartCoroutine("swipe0");
                }else if(mainCamera.transform.position.x >= 0 && mainCamera.transform.position.x < 360 && delta > 0){
                    StartCoroutine("swipe0");
                }else if(mainCamera.transform.position.x >= 360 && mainCamera.transform.position.x < 720 && delta < 0){
                    StartCoroutine("swipe0");
                }else if(mainCamera.transform.position.x >= 360 && mainCamera.transform.position.x < 720 && delta > 0){
                    StartCoroutine("swipe720");
                }else if(mainCamera.transform.position.x >= 720 && mainCamera.transform.position.x < 1080 && delta < 0){
                    StartCoroutine("swipe720");
                }else if(mainCamera.transform.position.x >= 720 && mainCamera.transform.position.x < 1080 && delta > 0){
                    StartCoroutine("swipe720");
                }else if(mainCamera.transform.position.x >= 1080 && mainCamera.transform.position.x < 1440 && delta < 0){
                    StartCoroutine("swipe720");
                }else if(mainCamera.transform.position.x >= 1080 && mainCamera.transform.position.x < 1440 && delta > 0){
                    StartCoroutine("swipe1440");
                }else if(mainCamera.transform.position.x >= 1440 && mainCamera.transform.position.x < 1800 && delta < 0){
                    StartCoroutine("swipe1440");
                }else if(mainCamera.transform.position.x >= 1440 && mainCamera.transform.position.x < 1800 && delta > 0){
                    StartCoroutine("swipe1440");
                }else if(mainCamera.transform.position.x >= 1800 && mainCamera.transform.position.x < 2160 && delta < 0){
                    StartCoroutine("swipe1440");
                }else if(mainCamera.transform.position.x >= 1800 && mainCamera.transform.position.x < 2160 && delta > 0){
                    StartCoroutine("swipe2160");
                }else if(mainCamera.transform.position.x >= 2160 && mainCamera.transform.position.x < 2520 && delta < 0){
                    StartCoroutine("swipe2160");
                }else if(mainCamera.transform.position.x >= 2160 && mainCamera.transform.position.x < 2520 && delta > 0){
                    StartCoroutine("swipe2160");
                }else if(mainCamera.transform.position.x >= 2520 && mainCamera.transform.position.x < 2880 && delta < 0){
                    StartCoroutine("swipe2160");
                }else if(mainCamera.transform.position.x >= 2520 && mainCamera.transform.position.x < 2880 && delta > 0){
                    StartCoroutine("swipe2880");
                }else if(mainCamera.transform.position.x > 2880){
                    StartCoroutine("swipe2880");
                }if(mainCamera.transform.position.x >= 1440 && !auth2160){
                    StopAllCoroutines();
                    StartCoroutine("swipe1440");
                }if(mainCamera.transform.position.x >= 2160 && !auth2880){
                    StopAllCoroutines();
                    StartCoroutine("swipe2160");
                }
            }
        }
    }
}

作为 unity 的初学者,这是我唯一想到的方法。你知道更好更清洁的方法吗?如果滑动不是完整的滑动,代码将推或拉门。

下面的代码只是对您所写内容的快速清理。您基本上需要做的是参数化您的协程。
另请注意,我只是将您的条件组合在一起。

最后,关于 "div" 变量的快速说明。本质上,我没有使用更大(更笨重)的数字,如 1440、2160 等,而是将值除以 360,然后使用它。除了让它更容易阅读之外,它并没有真正做任何事情。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BetterSwipeExample : MonoBehaviour {

    public Camera mainCamera;
    private float speed;
    private float iniCam;
    float delta = 0;
    private Vector2 iniPos = new Vector2 ();
    private Vector2 midPos = new Vector2 ();
    private Vector2 endPos = new Vector2 ();
    private GameObject[] numberOfDoors;
    private bool auth2160;
    private bool auth2880;



    // Use this for initialization
    void Start () {

    }


    private IEnumerator SwipeCoroutine (float finalPos) {
        while (mainCamera.transform.position.x != finalPos) {
            mainCamera.transform.position = 
                Vector2.MoveTowards(mainCamera.transform.position, 
                new Vector2 (finalPos, 0), (float)(Time.deltaTime * (1000 + Mathf.Abs(delta))));
            yield return null;
        }
    }


    // Update is called once per frame
    void Update () {
        numberOfDoors = GameObject.FindGameObjectsWithTag("Porta");
        if (numberOfDoors.Length == 3) {
            auth2160 = false;
            auth2880 = false;
        } 
        else if (numberOfDoors.Length == 4) {
            auth2160 = true;
            auth2880 = false;
        } 
        else if (numberOfDoors.Length == 5) {
            auth2160 = true;
            auth2880 = true;
        }



        if (Input.touchCount > 0) {
            Touch touch = Input.touches[0];
            if(touch.phase == TouchPhase.Began){
                StopAllCoroutines();
                iniCam = mainCamera.transform.position.x;
                iniPos = new Vector2(touch.position.x,0);
                Debug.Log("Posiçao inicial: " + iniPos);

            }
            else if(touch.phase == TouchPhase.Moved){
                midPos = new Vector2(touch.position.x,0);
                Debug.Log("Posiçao durante: " + midPos);
                //StartCoroutine("swipeLeft");
                mainCamera.transform.position = new Vector2(iniCam-(midPos.x-iniPos.x)*1.5F,0);


            }
            else if(touch.phase == TouchPhase.Ended){
                endPos = new Vector2(touch.position.x,0);
                delta = iniPos.x-endPos.x; //> 0 tendencia direita;
                Debug.Log("Posiçao final: " + endPos);

                int div = (int)mainCamera.transform.position.x / 360;

                if(div >= 6 && !auth2880){
                    StopAllCoroutines();
                    StartCoroutine(SwipeCoroutine(2160));
                    return;
                }

                if(div >= 4 && !auth2160){
                    StopAllCoroutines();
                    StartCoroutine(SwipeCoroutine(1440));
                    return;
                }


                if( div < 1 || 
                   (div == 1 && delta < 0) ) {
                    StartCoroutine(SwipeCoroutine(0));
                }

                else if ( (div == 1 && delta > 0) ||
                         (div == 2) ||
                         (div == 3 && delta < 0) ){
                    StartCoroutine(SwipeCoroutine(720));
                }

                else if ( (div == 3 && delta > 0) ||
                         (div == 4) ||
                         (div == 5 && delta < 0) ){
                    StartCoroutine(SwipeCoroutine(1440));
                }

                else if ( (div == 5 && delta > 0) ||
                         (div == 6) ||
                         (div == 7 && delta < 0) ){
                    StartCoroutine(SwipeCoroutine(2160));
                }

                else {
                    StartCoroutine(SwipeCoroutine(2880));
                }
            }
        }
    }
}