【发布时间】:2021-02-19 01:43:07
【问题描述】:
有没有办法检查 Rect 变换是否包含点?提前致谢。我尝试了 Bounds.Contains() 和 RectTransformUtility.RectangleContainsScreenPoint() 但这对我没有帮助
private bool AreCoordsWithinUiObject(Vector2 coords, GameObject gameObj)
{
Bounds bounds = gameObj.GetComponent<Renderer>().bounds;
return bounds.Contains(new Vector3(coords.x, coords.y, 0));
}
这样我有一个错误“对象没有附加渲染器”,但我已将 CanvasRenderer 附加到它。
RectTransformUtility.RectangleContainsScreenPoint(gameObj.GetComponent<RectTransform>(), coords);
这个方法总是返回false
if (AreCoordsWithinUiObject(point, lines[i]))
{
print("contains");
}
lines 是 GameObjects 的列表
【问题讨论】:
-
请包含您尝试过的代码。你的问题只是一个函数。放置不起作用的代码。
Bounds.Contains和RectTransformUtility.RectangleContainsScreenPoint可能会有人发现您的问题。 -
我用代码更新了帖子
-
可能是因为您试图获取“渲染器”而不是“CanvasRenderer”?
-
我尝试获取 CanvasRenderer 但它不起作用
-
我更新了帖子