将缩放转换为新的InputSystem



我正在尝试使用EnhancedTouch.Touch从旧输入转换为缩放。不幸的是,我不知道如何从这里向前走?我得到的误差是Operator '==' cannot be applied to operands of type 'TouchPhase' and 'TouchPhase'。我该如何解决这个问题?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
public class ZoomTouch : MonoBehaviour {
public Camera camera;
float touchDist = 0;
float lastDist = 0;
public void Update () {
if (Input.touchCount == 2) {
Touch touch1 = Touch.activeFingers[0].currentTouch;
Touch touch2 = Touch.activeFingers[0].currentTouch;
if (touch1.phase == TouchPhase.Began && touch2.phase == TouchPhase.Began) {
lastDist = Vector2.Distance (touch1.position, touch2.position);
}
if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved) {
float newDist = Vector2.Distance (touch1.position, touch2.position);
touchDist = lastDist - newDist;
lastDist = newDist;
camera.fieldOfView += touchDist * 0.1f;
}
}
}
}

我使用:

if (_activeTouch.phase == UnityEngine.InputSystem.TouchPhase.Began)
{
_startPos = _activeTouch.screenPosition;
}

问题是,如果你简单地写TouchPhase,它是使用enum UnityEngine.TouchPhase,这是不一样的enum UnityEngine.InputSystem.TouchPhase

你还可以看到,如果你添加using UnityEngine.InputSystem;,错误改变为TouchPhase' is an ambiguous reference between 'UnityEngine.InputSystem.TouchPhase' and 'UnityEngine.TouchPhase

我希望这个代码工作。然而,我没有触控设备来测试它。这段简单的代码可以工作,取现在和过去之间的距离,并放大它们的增量。

public float lastDistance;
public float distance;
void Update()
{
if (Input.touchCount < 2) return;
var touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);

distance = Vector2.Distance(touch1.position, touch1.position);
if (Mathf.Abs(lastDistance-distance) >= 10) lastDistance = distance; // for avoiding sharp zooms on first touch

Camera.main.fieldOfView += distance-lastDistance * 0.1f;
lastDistance = distance;
}

相关内容

  • 没有找到相关文章

最新更新