- Home /
4.6 UI Check if a button and a in-game object are overlapping
Hey there!
So, before 4.6, in the old UI system, to check if a GameObject was inside a button Rect, I would just return it's Screen position in a Vector2, and then use the Rect.Contains function (with the proper adjustments.)
However, I'm now trying to do it with the new UI, but can't figure it out how. So, can you guys give me a tips on this?
Thanks...
EDIT: Code with what I'm trying to do:
public GameObject FindGemInPool(int poolIndex, RectTransform rect)
{
List<GameObject> pool = base.pool[poolIndex];
Vector3 position;
Vector2 pos;
foreach (GameObject gem in pool)
{
if (!gem.activeInHierarchy)
continue;
position = gem.transform.position;
if (!IsInView(position))
continue;
pos = Camera.main.WorldToScreenPoint(position);
if (RectTransformUtility.RectangleContainsScreenPoint(rect, pos , Camera.main))
{
return gem;
}
}
return null;
}
Hey there!
I've not used it myself but I did notice this utility method that would appear to be just what you're after, RectTransformUtility.RectangleContainsScreenPoint(...)
Hope this helps. Phill
Unfortunatelly it isn't... Probably, I should post a code snippet with what I'm trying to do...
Actually @Phles never$$anonymous$$d, it was that, I was just doing it wrong! The camera parameter should go null! Thanks!