经过Unity 触摸插件 TouchScript遇到的坑后我又需要重新写一次这个功能,由于之前遇到太多的坑我怀疑我打开的方式不对,遂决定重新写一次。
首先我发现twoFingerTransformHandler和manipulationTransformedHandler是可以合并到一个Screen Transform Gesture 一个方法里面的。这个是我目前用的方法。
private void GestureHandler(object sender, System.EventArgs e)
{
if (m_isFirstRotation)
{
m_isFirstRotation = false;
xDeg = Vector3.Angle(Vector3.right, mainCamera.transform.right);
yDeg = Vector3.Angle(Vector3.up, mainCamera.transform.up);
}
xDeg += (screenGesture.DeltaPosition.x / Screen.width) * 200;
yDeg -= (screenGesture.DeltaPosition.y / Screen.height) * 100;
if (isMainView)
{
//Clamp the vertical axis for the orbit
yDeg = Mathf.Clamp(yDeg, yMinLimit, yMaxLimit);
mainCamera.transform.rotation = Quaternion.Euler(yDeg, xDeg, 0);
}
else
{
topCamera.localPosition = new Vector3(yDeg * -2500, defaultTopCameraPos.y, xDeg * -2500);
}
zoomValue += (screenGesture.DeltaScale * rate - 1f);
FOVSlider.OnGestureZoomValueChanged(zoomValue);
}
这个方法出现了一个问题,zoomValue += (screenGesture.DeltaScale * rate - 1f)
中zoomValue 是一个0~2的值,代表是在放大还是缩小,这个值直接用来放大缩小会导致一个连续性的问题,不能只放大一点点。
而且还会导致在缩放时可以移动镜头的问题。
再后来我把它改成这样就解决了
private void GestureHandler(object sender, System.EventArgs e)
{
zoomValue = 0;
if (screenGesture.DeltaScale != 1)
{
zoomValue += (screenGesture.DeltaScale * rate - 1f);
FOVSlider.OnGestureZoomValueChanged(zoomValue);
return;
}
if (m_isFirstRotation)
{
m_isFirstRotation = false;
xDeg = Vector3.Angle(Vector3.right, mainCamera.transform.right);
yDeg = Vector3.Angle(Vector3.up, mainCamera.transform.up);
}
xDeg += (screenGesture.DeltaPosition.x / Screen.width) * 200;
yDeg -= (screenGesture.DeltaPosition.y / Screen.height) * 100;
if (isMainView)
{
//Clamp the vertical axis for the orbit
yDeg = Mathf.Clamp(yDeg, yMinLimit, yMaxLimit);
mainCamera.transform.rotation = Quaternion.Euler(yDeg, xDeg, 0);
}
else
{
topCamera.localPosition = new Vector3(yDeg * -2500, defaultTopCameraPos.y, xDeg * -2500);
}
}