/// <summary>
/// Counts the bounding box corners of the given RectTransform that are visible from the given Camera in screen space.
/// </summary>
/// <returns>The amount of bounding box corners that are visible from the Camera.</returns>
/// <param name="rectTransform">Rect transform.</param>
/// <param name="camera">Camera.</param>
private static int CountCornersVisibleFrom(this RectTransform rectTransform, Camera camera)
{
Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height);
Vector3[] objectCorners = new Vector3[4];
rectTransform.GetWorldCorners(objectCorners);
int visibleCorners = 0;
for (var i = 0; i < objectCorners.Length; i++)
{
Vector3 tempScreenSpaceCorner = RectTransformUtility.WorldToScreenPoint(camera, objectCorners[i]);
if (screenBounds.Contains(tempScreenSpaceCorner))
{
visibleCorners++;
}
}
return(visibleCorners);
}