private void Move() { if (Input.GetMouseButtonDown(0)) { DownPosition = Input.mousePosition; print("鼠标左键按下时的位置:" + DownPosition); } if (Input.GetMouseButtonUp(0) == false)//只要没有鼠标抬起动作就退出 { return; } SlideDir dir = CheckDir();//调用CheckDir()函数获取方向 }
//检测鼠标滑动的方向 private SlideDir CheckDir() { if (Input.GetMouseButtonUp(0)) { //print ("鼠标左键按下时的位置:" + DownPosition); print("鼠标左键抬起时的位置:" + Input.mousePosition); Vector3 offest = Input.mousePosition - DownPosition;//局部变量 print("鼠标左键按下到抬起时的偏差:" + offest); //检测水平 if (Mathf.Abs(offest.x) > 100 && Mathf.Abs(offest.x) >= Mathf.Abs(offest.y)) { if (offest.x < 0) { dir = SlideDir.left; } else { dir = SlideDir.right; } } //检测竖直 if (Mathf.Abs(offest.y) > 100 && Mathf.Abs(offest.x) < Mathf.Abs(offest.y)) { if (offest.y > 0) { dir = SlideDir.top; } else { dir = SlideDir.bottom; } } //无方向 if (Mathf.Abs(offest.x) <= 100 && Mathf.Abs(offest.y) <= 100) { dir = SlideDir.nullpoint; } } return(dir); }
Vector2 Target(RectTransform content, SlideDir dir, float slideRate) { if (dir == SlideDir.left) { return(content.anchoredPosition + (Vector2.left * canvasWidth * slideRate)); } else if (dir == SlideDir.right) { return(content.anchoredPosition + (Vector2.right * canvasWidth * slideRate)); } else if (dir == SlideDir.up) { return(content.anchoredPosition + (Vector2.up * canvasHeight * slideRate)); } else if (dir == SlideDir.down) { return(content.anchoredPosition + (Vector2.down * canvasHeight * slideRate)); } else { throw new System.Exception("Invalid direction"); } }
void Slide(RectTransform content, SlideDir dir, AnimationCurve curve, UnityEvent OnSlideEnd, float slideRate = 1) { Vector2 target = Target(content, dir, slideRate); StartCoroutine(AnimateWithEvent(content, target, curve, OnSlideEnd)); }
void Slide(RectTransform content, SlideDir dir, AnimationCurve curve, float slideRate = 1) { Vector2 target = Target(content, dir, slideRate); StartCoroutine(Animate(content, target, curve)); }