当你的TipsWindow在屏幕中无法全部展示的时候,它会自动改变弹出的位置。。。
看下面的Demo:
本来想把源码分享一下的,发现不能发附件....
那就贴代码好了,反正也挺简单的,最关键的其实就这么一个函数:
protected void AdjustPosition()
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(m_parentRect, Input.mousePosition, null, out pos))
{
Vector2 offset = new Vector2(-15, -15);
m_thisRect.pivot = new Vector2(1, 1);
Vector2 relPos = pos + offset;
if (relPos.x < (m_parentRect.rect.xMin + m_thisRect.rect.width))
{
offset.x = 15;
m_thisRect.pivot = new Vector2(0, 1);
relPos = pos + offset;
}
if (relPos.y < (m_parentRect.rect.yMin + m_thisRect.rect.height))
{
offset.y = 15;
m_thisRect.pivot = new Vector2(m_thisRect.pivot.x, (pos.y - m_parentRect.rect.yMin) / m_thisRect.rect.height);
relPos = pos + offset;
}
m_thisRect.anchoredPosition = relPos;
}
}