我不明白我是怎么做RectTransformUtility.RectangleContainsScreenPoint()错了,看起来很简单。我制作了一个矩形变换,它是我画布的儿童游戏对象的一个组件,并将其固定到我想要的位置。然后我将这个recttransform,Input.getTouch(0).position和Camera.main作为参数传递给函数。顺便说一句,我的画布是屏幕空间相机,而不是叠加,因此传递null,因为相机参数不是问题。我正在检查玩家点击是否在recttransform中,以便在该位置产生一个球,但无论我在手机上点击屏幕,该功能总是返回true。这是我的代码:
public class PaddleController : MonoBehaviour
{
GameObject childPaddle1, childPaddle2;
Vector3 touchpos;
public float verticalBoundary;
RectTransform tapArea;
Rect tapAreaRect;
private void Start()
{
RectTransform tapArea = GameObject.Find("tapArea").GetComponentInChildren<RectTransform>(); //tapArea is child of canvas
tapAreaRect = tapArea.rect;
childPaddle1 = GameObject.Find("Paddle1");
childPaddle2 = GameObject.Find("Paddle2");
childPaddle1.SetActive(false);
}
private void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
touchpos = Camera.main.ScreenToWorldPoint(touch.position);
if (RectTransformUtility.RectangleContainsScreenPoint(tapArea,touch.position,Camera.main));
{
childPaddle1.transform.position = new Vector3(touchpos.x, touchpos.y, 0); // this is necessary so that the childPaddle1.transform.position.z is not set to the same z value as the camera(this will cause it to be cut off by the camera's near clipping plane
childPaddle1.SetActive(true);
}
break;
case TouchPhase.Ended:
childPaddle1.SetActive(false);
break;
}
}
}
}
我是否以某种方式设置了recttransform错误?感谢任何帮助。谢谢。