C# 单位变换包含点

C# 单位变换包含点,c#,unity3d,C#,Unity3d,有没有办法检查Rect变换是否包含点?提前谢谢。我尝试了Bounds.Contains()和RectTransformUtility.RectangleContainsScreenPoint(),但这对我没有帮助 private bool AreCoordsWithinUiObject(Vector2 coords, GameObject gameObj) { Bounds bounds = gameObj.GetComponent<Renderer>().bounds;

有没有办法检查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));
}
线条是游戏对象的列表


画布渲染没有
边界
成员变量。但是,您的任务只需使用成员变量即可完成,因为我们可以通过这种方式获得矩形的宽度和高度。下面的脚本假设画布元素锚定在画布的中心。当鼠标位于脚本附加到的元素内时,它会打印“TRUE”

void Update()
{
    // convert pixel coords to canvas coords
    Vector2 point = Input.mousePosition - new Vector2(Screen.width / 2, Screen.height / 2); 
    Debug.Log(IsPointInRT(point, this.GetComponent<RectTransform>()));
}

bool IsPointInRT(Vector2 point, RectTransform rt)
{
    // Get the rectangular bounding box of your UI element
    Rect rect = rt.rect;

    // Get the left, right, top, and bottom boundaries of the rect
    float leftSide = rt.anchoredPosition.x - rect.width / 2;
    float rightSide = rt.anchoredPosition.x + rect.width / 2;
    float topSide = rt.anchoredPosition.y + rect.height / 2;
    float bottomSide = rt.anchoredPosition.y - rect.height / 2;

    //Debug.Log(leftSide + ", " + rightSide + ", " + topSide + ", " + bottomSide);

    // Check to see if the point is in the calculated bounds
    if (point.x >= leftSide &&
        point.x <= rightSide &&
        point.y >= bottomSide &&
        point.y <= topSide)
    {
        return true;
    }
    return false;
}
void Update()
{
//将像素坐标转换为画布坐标
Vector2点=Input.mousePosition-新Vector2(Screen.width/2,Screen.height/2);
Log(IsPointInRT(point,this.GetComponent());
}
布尔IsPointInRT(矢量2点,矩形变换rt)
{
//获取UI元素的矩形边界框
Rect Rect=rt.Rect;
//获取矩形的左、右、上、下边界
浮动左侧=rt.anchoredPosition.x-矩形宽度/2;
浮动右侧=右固定位置x+右宽度/2;
浮式上部组块=右锚定位置y+垂直高度/2;
浮动底面=右锚定位置y-垂直高度/2;
//调试日志(左侧+”、“+右侧+”、“+上部组块+”、“+底部);
//检查点是否在计算边界内
如果(点x>=左侧&&
点x=底部&&

point.yCanvasRenders没有
bounds
成员变量。但是,您的任务可以只使用成员变量来完成,因为我们可以以这种方式获得矩形的宽度和高度。下面的脚本假设画布元素锚定在画布的中心。它打印“TRUE”当鼠标位于脚本附加到的元素内时

void Update()
{
    // convert pixel coords to canvas coords
    Vector2 point = Input.mousePosition - new Vector2(Screen.width / 2, Screen.height / 2); 
    Debug.Log(IsPointInRT(point, this.GetComponent<RectTransform>()));
}

bool IsPointInRT(Vector2 point, RectTransform rt)
{
    // Get the rectangular bounding box of your UI element
    Rect rect = rt.rect;

    // Get the left, right, top, and bottom boundaries of the rect
    float leftSide = rt.anchoredPosition.x - rect.width / 2;
    float rightSide = rt.anchoredPosition.x + rect.width / 2;
    float topSide = rt.anchoredPosition.y + rect.height / 2;
    float bottomSide = rt.anchoredPosition.y - rect.height / 2;

    //Debug.Log(leftSide + ", " + rightSide + ", " + topSide + ", " + bottomSide);

    // Check to see if the point is in the calculated bounds
    if (point.x >= leftSide &&
        point.x <= rightSide &&
        point.y >= bottomSide &&
        point.y <= topSide)
    {
        return true;
    }
    return false;
}
void Update()
{
//将像素坐标转换为画布坐标
Vector2点=Input.mousePosition-新Vector2(Screen.width/2,Screen.height/2);
Log(IsPointInRT(point,this.GetComponent());
}
布尔IsPointInRT(矢量2点,矩形变换rt)
{
//获取UI元素的矩形边界框
Rect Rect=rt.Rect;
//获取矩形的左、右、上、下边界
浮动左侧=rt.anchoredPosition.x-矩形宽度/2;
浮动右侧=右固定位置x+右宽度/2;
浮式上部组块=右锚定位置y+垂直高度/2;
浮动底面=右锚定位置y-垂直高度/2;
//调试日志(左侧+”、“+右侧+”、“+上部组块+”、“+底部);
//检查点是否在计算边界内
如果(点x>=左侧&&
点x=底部&&

point.y您需要提供UI摄像头作为RectTransformUtility的第三个参数。RectangleContainesScreenPoint使其正常工作

您需要提供UI摄像头作为RectTransformUtility的第三个参数。RectangleContainesScreenPoint使其正常工作

请包括您尝试的代码。您的问题是什么这只是一个函数。把不起作用的代码放进去。
Bounds.Contains
RectTransformUtility.RectangleContainesScreenpoint
,也许有人能发现你的问题。我用代码更新了帖子,可能是因为你想得到的是“渲染器”而不是“画布渲染器”?我试图获取CanvasRenderer,但它不起作用。我更新了后请包含您尝试过的代码。您的问题只是一个函数。将不起作用的代码放入。包括
边界。包含
RectTransformUtility。RectangleContainsScreenPoint
,可能有人能够发现您的问题。我更新可能是因为你想得到一个“渲染器”而不是“画布渲染器”?我试图得到画布渲染器,但它不起作用我更新了帖子谢谢,这帮了我!谢谢,这帮了我!这是正确的答案。这是正确的答案。
void Update()
{
    // convert pixel coords to canvas coords
    Vector2 point = Input.mousePosition - new Vector2(Screen.width / 2, Screen.height / 2); 
    Debug.Log(IsPointInRT(point, this.GetComponent<RectTransform>()));
}

bool IsPointInRT(Vector2 point, RectTransform rt)
{
    // Get the rectangular bounding box of your UI element
    Rect rect = rt.rect;

    // Get the left, right, top, and bottom boundaries of the rect
    float leftSide = rt.anchoredPosition.x - rect.width / 2;
    float rightSide = rt.anchoredPosition.x + rect.width / 2;
    float topSide = rt.anchoredPosition.y + rect.height / 2;
    float bottomSide = rt.anchoredPosition.y - rect.height / 2;

    //Debug.Log(leftSide + ", " + rightSide + ", " + topSide + ", " + bottomSide);

    // Check to see if the point is in the calculated bounds
    if (point.x >= leftSide &&
        point.x <= rightSide &&
        point.y >= bottomSide &&
        point.y <= topSide)
    {
        return true;
    }
    return false;
}