向右滑动关闭界面

向右滑动关闭界面

在一些app或者特殊功能的时候会用到手指向右滑动关闭界面的情况,找了半天没合适的,自己动手,丰衣足食

注释:本来是代码是在界面上拖拽用来执行,后来发现会阻挡下面按钮的触发事件,后改成判断最上层界面

上代码 IsBlocksPaycasts = CanvasGroup的blocksRaycasts 用来在关闭的时候关闭界面的射线检测

下面展示一些 内联代码片

	private Vector3 offect;
    private BasePanel currentPanel;
    private bool isDrag = false;
    
    public void Update()
    {
    	//按下的时候
        if (Input.GetMouseButtonDown(0))
        {
            offect = Input.mousePosition;
            //判断当前的界面,可自行根据实际情况改变
            currentPanel = UIManager.Instance.GetCurrentPanel();
        }
        if (Input.GetMouseButton(0))
        {
            if (currentPanel == null)
            {
                return;
            }
            if (Input.mousePosition.x - offect.x > 100)
            {
                isDrag = true;
                currentPanel.IsBlocksPaycasts = false;
            }
            if (isDrag)
            {
                if (Input.mousePosition.x - offect.x < 0)
                {
                    currentPanel.transform.localPosition = Vector3.zero;
                    return;
                }
                currentPanel.transform.localPosition = Vector3.right * (Input.mousePosition.x - offect.x - 100);
                //Debug.Log(Input.mousePosition.x);
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            if (currentPanel == null)
            {
                return;
            }
            if (!isDrag)
            {
                return;
            }
            if (currentPanel.transform.localPosition.x < Screen.width / 2)
            {
                currentPanel.GetComponent<RectTransform>().DOLocalMove(Vector3.zero, currentPanel.TweenTime);
            }
            else
            {
                currentPanel.CloseTween();
            }
            currentPanel.IsBlocksPaycasts = true;
            isDrag = false;
            offect = Vector3.zero;
            //Debug.Log("弹起");
        }
    }

大功告成~!加鸡腿!!!!

猜你喜欢

转载自blog.csdn.net/zhangjiyuan666/article/details/117697066