代码之家  ›  专栏  ›  技术社区  ›  StuckInPhDNoMore

有没有办法将触摸输入限制到Unity3D中的面板?

  •  3
  • StuckInPhDNoMore  · 技术社区  · 7 年前

    我正在尝试在我的游戏中实现刷卡控制,希望只在屏幕左侧检测到刷卡,并保持游戏的其他控制机制不变。

    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 回复  |  直到 7 年前
        1
  •  1
  •   Ruzihm aks786    7 年前

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