Unity UI 识别画圆手势
最近做了一个项目,需要打开一个编辑UI,平时不显示,如果在UI界面上做一个按钮,感觉很丑,就想到了Reporter_Logs的画圆触发。
识别到你在界面上画了一个圆 (可用于 Windows 和 Android) 代码如下:
bool isGestureDone() { if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { if (Input.touches.Length != 1) { gestureDetector.Clear(); gestureCount = 0; } else { if (Input.touches[0].phase == TouchPhase.Canceled || Input.touches[0].phase == TouchPhase.Ended) gestureDetector.Clear(); else if (Input.touches[0].phase == TouchPhase.Moved) { Vector2 p = Input.touches[0].position; if (gestureDetector.Count == 0 || (p - gestureDetector[gestureDetector.Count - 1]).magnitude > 10) gestureDetector.Add(p); } } } else { if (Input.GetMouseButtonUp(0)) { gestureDetector.Clear(); gestureCount = 0; } else { if (Input.GetMouseButton(0)) { Vector2 p = new Vector2(Input.mousePosition.x, Input.mousePosition.y); if (gestureDetector.Count == 0 || (p - gestureDetector[gestureDetector.Count - 1]).magnitude > 10) gestureDetector.Add(p); } } } if (gestureDetector.Count < 10) return false; gestureSum = Vector2.zero; gestureLength = 0; Vector2 prevDelta = Vector2.zero; for (int i = 0; i < gestureDetector.Count - 2; i++) { Vector2 delta = gestureDetector[i + 1] - gestureDetector[i]; float deltaLength = delta.magnitude; gestureSum += delta; gestureLength += deltaLength; float dot = Vector2.Dot(delta, prevDelta); if (dot < 0f) { gestureDetector.Clear(); gestureCount = 0; return false; } prevDelta = delta; } int gestureBase = (Screen.width + Screen.height) / 4; if (gestureLength > gestureBase && gestureSum.magnitude < gestureBase / 2) { gestureDetector.Clear(); gestureCount++; if (gestureCount >= numOfCircleToShow) return true; } return false; }