【发布时间】:2017-02-04 16:58:32
【问题描述】:
我想知道如何检查 Unity Canvas 上的两个 UI 面板是否相互重叠。
目前我通过比较画布元素 Rects 来做到这一点
画布设置
- 渲染模式:屏幕空间 - 相机
- 像素完美:[是]
- 渲染相机:主相机
- 平面距离:100
- 排序层:默认
- 层内顺序:0
画布缩放器设置
- UI 缩放模式:恒定像素大小
- 比例因子:1
- 每单位参考像素数:100
我用来检查的代码
[Header("Check For Overlap")]
public RectTransform PlayerBar;
public RectTransform LeftBar;
public Rect RectOne;
public Rect RectTwo;
public bool overlapping;
//Check if the two canvas element Rects overlap each other
public void CheckForOverlap()
{
overlapping = false;
// Convert Canvas RectTransforms to World Rects
RectOne = GetWorldRect(LeftBar);
RectTwo = GetWorldRect(PlayerBar);
if (RectOne.Overlaps(RectTwo))
{
overlapping = true;
}
}
public Rect GetWorldRect(RectTransform rt)
{
// Get World corners, take top left
Vector3[] corners = new Vector3[4];
rt.GetWorldCorners(corners);
Vector3 topLeft = corners[0];
// Rect Size ... I'm not sure if this is working correctly?
Vector2 size = new Vector2(rt.rect.size.x, rt.rect.size.y);
return new Rect(topLeft, size);
}
会发生什么
'Overlapping' bool 立即变为 true。
矩形返回为(示例)
X -7.5,Y 2.5 W 98.5,H 164.1667
【问题讨论】:
标签: c# user-interface unity3d rect