Есть ли способ ограничить сенсорный ввод панелью в Unity3D? - PullRequest
0 голосов
/ 28 декабря 2018

Я пытаюсь внедрить элементы управления смахиванием в свою игру и хочу, чтобы смахивания были обнаружены только в левой части экрана, а другой механизм управления игрой оставлен без изменений.

I'mиспользуя это SwipeManager:

public enum Swipes { None, Up, Down, Left, TopLeft, BottomLeft, Right, TopRight,  BottomRight};

public class SwipeManager : MonoBehaviour
{
    public float minSwipeLength = 200f;

    private Vector2 fingerStart;
    private Vector2 fingerEnd;

    public static Swipes direction;
    public static float angle;
    public static Vector2 strength;

    public static bool debugMode = false;

    void Update ()
    {
        SwipeDetection();
    }

    public void SwipeDetection ()
    {
        if (Input.GetMouseButtonDown(0)) {
            fingerStart = Input.mousePosition;
            fingerEnd  = Input.mousePosition;
        }

        if(Input.GetMouseButton(0)) {
            fingerEnd = Input.mousePosition;

            strength = new Vector2 (fingerEnd.x - fingerStart.x, fingerEnd.y - fingerStart.y);

            // Make sure it was a legit swipe, not a tap
            if (strength.magnitude < minSwipeLength) {
                direction = Swipes.None;
                return;
            }

            angle = (Mathf.Atan2(strength.y, strength.x) / (Mathf.PI));
            if (debugMode) Debug.Log(angle);
            // Swipe up
            if (angle>0.375f && angle<0.625f) {
                direction = Swipes.Up;
                if (debugMode) Debug.Log ("Up");
                // Swipe down
            } else if (angle<-0.375f && angle>-0.625f) {
                direction = Swipes.Down;
                if (debugMode) Debug.Log ("Down");
                // Swipe left
            } else if (angle<-0.875f || angle>0.875f) {
                direction = Swipes.Left;
                if (debugMode) Debug.Log ("Left");
                // Swipe right
            } else if (angle>-0.125f && angle<0.125f) {
                direction = Swipes.Right;
                if (debugMode) Debug.Log ("Right");
            }
            else if(angle>0.125f && angle<0.375f){
                direction = Swipes.TopRight;
                if (debugMode) Debug.Log ("top right");
            }
            else if(angle>0.625f && angle<0.875f){
                direction = Swipes.TopLeft;
                if (debugMode) Debug.Log ("top left");
            }
            else if(angle<-0.125f && angle>-0.375f){
                direction = Swipes.BottomRight;
                if (debugMode) Debug.Log ("bottom right");
            }
            else if(angle<-0.625f && angle>-0.875f){
                direction = Swipes.BottomLeft;
                if (debugMode) Debug.Log ("bottom left");
            }
        }

        if(Input.GetMouseButtonUp(0)) {
            direction = Swipes.None;  
        }
    }
}

Я хочу создать панель слева от экрана и хочу, чтобы прокрутки были обнаружены только на панели, где бы ни щелкали за пределами этой панели (то есть на правой сторонеэкрана) не должен считаться ударом, но цель (как это настроено сейчас)

Спасибо

1 Ответ

0 голосов
/ 28 декабря 2018

Одним из решений является пропуск SwipeDetection, когда указатель мыши находится за пределами панели.Итак, если вы можете получить ссылку на RectTransform панели, то вы можете просто проверить, находится ли указатель мыши внутри нее, прежде чем вызывать SwipeDetection.

, чтобы учесть возможностьпользователь, нажимая на панель и затем перемещаясь в нее, вы можете назначить fingerStart = Input.mousePosition;, когда мышь находится за пределами прямоугольника.

В целом это может выглядеть следующим образом:

public RectTransform rectTransform; // panel RectTransform assigned to this variable

...

void Update ()
{
    Vector2 mousePosition = Input.mousePosition;

    if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, mousePosition))
    {
        SwipeDetection();
    }
    else 
    {
        fingerStart = Input.mousePosition;
        direction = Swipes.None;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...