c# - 有什么方法可以限制 Unity3D 中面板的触摸输入吗?

标签 c# unity3d

我正在尝试在我的游戏中实现滑动控件,并希望仅在屏幕左侧检测到滑动,而让游戏的其他控制机制保持原样。

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

我想在屏幕左侧创建一个面板,并希望仅在面板上检测到滑动,在该面板外(即屏幕右侧)点击的任何地方都不应被视为滑动,而是目标(现在已设置)

谢谢

最佳答案

一种解决方案是跳过 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;
    }
}

关于c# - 有什么方法可以限制 Unity3D 中面板的触摸输入吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53961979/

相关文章:

c# - BeginInvoke/EndInvoke 是在主线程上调用的好习惯吗?

ios - Unity3d 4.6.3交叉编译失败

unity3d - Android 设备上的 UI 粉红色

c# - 当我的变量在 "awake"方法中实例化时,为什么我会收到 NullReferenceException?

c# - 找不到类型 Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView 的 Blazor 默认构造函数

c# - 如何在 C# 中编码 wstring *?

c# - 将 Unity 项目导入 Visual Studio 时出现 "Error: DEP 0700: Registration of the app failed"

c# - 统一的 Firebase 身份验证登录总是返回一个新用户

c# - 为什么 DateTime.Now.Date 格式会因系统而异?

c# - 是否有兼容 MonoTouch 的 BDD 风格的测试框架?