using UnityEngine; //https://gist.github.com/AkhmadMax, modified public static class RendererExtensions { /// /// Counts the bounding box corners of the given RectTransform that are visible from the given Camera in screen space. /// /// The amount of bounding box corners that are visible from the Camera. /// Rect transform. /// Camera. private static int CountCornersVisibleFrom(this RectTransform rectTransform) { Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height); // Screen space bounds (assumes camera renders across the entire screen) Vector3[] objectCorners = new Vector3[4]; rectTransform.GetWorldCorners(objectCorners); int visibleCorners = 0; for (var i = 0; i < objectCorners.Length; i++) // For each corner in rectTransform { if (screenBounds.Contains(objectCorners[i])) // If the corner is inside the screen { visibleCorners++; } } return visibleCorners; } /// /// Determines if this RectTransform is fully visible from the specified camera. /// Works by checking if each bounding box corner of this RectTransform is inside the cameras screen space view frustrum. /// /// true if is fully visible from the specified camera; otherwise, false. /// Rect transform. /// Camera. public static bool IsFullyVisibleFrom(this RectTransform rectTransform) { return CountCornersVisibleFrom(rectTransform) == 4; // True if all 4 corners are visible } /// /// Determines if this RectTransform is at least partially visible from the specified camera. /// Works by checking if any bounding box corner of this RectTransform is inside the cameras screen space view frustrum. /// /// true if is at least partially visible from the specified camera; otherwise, false. /// Rect transform. /// Camera. public static bool IsVisible(this RectTransform rectTransform) { return CountCornersVisibleFrom(rectTransform) > 0; // True if any corners are visible } }