有没有办法在Unity3D中限制触摸输入只针对面板?

3

我正在尝试在我的游戏中实现滑动控制,并希望仅在屏幕左侧检测到滑动,同时保留游戏的其他控制机制。

我正在使用这个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;  
        }
    }
}

我想在屏幕左侧创建一个面板,并希望仅在面板上检测滑动手势,在面板外的任何位置单击(即在屏幕右侧)不应被视为滑动手势,而是作为目标(当前设置)。谢谢。

也许可以在https://gamedev.stackexchange.com/上询问。 - Vahid Amiri
1个回答

1
一种解决方案是在鼠标指针在面板外部时跳过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;
    }
}

1
谢谢您的回答。我按照建议进行了操作,编辑了“SwipeManager.cs”脚本以包含矩形变换,并在检查器中将我的面板拖入其中。然后我将“SwipeDetection()”函数包装在您提供的if块中。但它似乎没有遵循它,仍然可以在屏幕上的任何位置检测到滑动。 - StuckInPhDNoMore
1
@StuckInPhD 试试那个编辑。我之前的代码假设面板的父级覆盖整个屏幕。 - Ruzihm
1
那个完美地解决了问题。谢谢。我会接受这个答案,因为我的初始问题已经解决了。我正在使用这些滑动手势来改变攻击的方向,有4条攻击路线,我面临的另一个问题是单次滑动不被视为单次滑动,而是在单次滑动中改变和循环多条攻击路线。有没有办法将单次滑动限制为单个操作?而不是连续一系列的滑动?非常感谢。 - StuckInPhDNoMore
1
@StuckInPhD 我会在你的车道变换代码中添加一个标志,如 bool alreadySwipe = false;。当检测到滑动时,请检查 alreadySwipe。如果 alreadySwipetrue,则不执行任何操作。如果它是 false,则进行车道变换并设置 alreadySwipe = true;。每当 SwipeManager.direction == Swipes.None 时,再次将 alreadySwipe = false;。还请注意,我在我的答案中添加了 direction = Swipes.None; - Ruzihm
1
谢谢你的解释。我明白了,我会把它加入到我的代码中。但是我不理解你最后一行的意思,你说我应该注意你在答案中添加了“direction = Swipes.None;”。但是我在你更新的答案中看不到它。感谢你的所有帮助 :) - StuckInPhDNoMore
1
@StuckInPhD 这很奇怪,我想我没有正确保存我的更改。现在我已经把它放回去了。 - Ruzihm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接