问题描述
private Touch touch;
private Vector2 beginTouchPosition;
private Vector2 endTouchPosition;
public GameObject timeText;
public GameObject CashText;
void Update()
{
if (Input.touchCount > 0)
{
switch (Input.GetTouch(0).phase)
{
case TouchPhase.Began:
beginTouchPosition = touch.position;
timeText.GetComponent<Text>().text = beginTouchPosition.y.ToString();
break;
case TouchPhase.Ended:
endTouchPosition = touch.position;
CashText.GetComponent<Text>().text = endTouchPosition.y.ToString();
if (beginTouchPosition == endTouchPosition)
{
//Do Stuff
}
break;
}
}
}
上面的代码的问题是timeText和cashText都显示(0,0),这意味着开始位置的位置始终为(0,0),结束位置也是如此位置阶段。即使它们绝对不是。
解决方法
之所以发生,是因为您从未分配任何要触摸的东西。所以是空的
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
switch (touch.phase)
{
.....
}
}