using System.Collections; using System.Collections.Generic; using UnityEngine; public static class RectTransformExtension {/// /// Counts the bounding box corners of the given RectTransform that are visible in screen space. /// /// The amount of bounding box corners that are visible. /// Rect transform. /// Camera. Leave it null for Overlay Canvasses. private static int CountCornersVisibleFrom(this RectTransform rectTransform, Camera camera = null) { 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; Vector3 tempScreenSpaceCorner; // Cached for (var i = 0; i < objectCorners.Length; i++) // For each corner in rectTransform { if (camera != null) tempScreenSpaceCorner = camera.WorldToScreenPoint(objectCorners[i]); // Transform world space position of corner to screen space else { Debug.Log(rectTransform.gameObject.name + " :: " + objectCorners[i].ToString("F2")); tempScreenSpaceCorner = objectCorners[i]; // If no camera is provided we assume the canvas is Overlay and world space == screen space } if (screenBounds.Contains(tempScreenSpaceCorner)) // If the corner is inside the screen { visibleCorners++; } } return visibleCorners; } /// /// Determines if this RectTransform is fully visible. /// Works by checking if each bounding box corner of this RectTransform is inside the screen space view frustrum. /// /// true if is fully visible; otherwise, false. /// Rect transform. /// Camera. Leave it null for Overlay Canvasses. public static bool IsFullyVisibleFrom(this RectTransform rectTransform, Camera camera = null) { if (!rectTransform.gameObject.activeInHierarchy) return false; return CountCornersVisibleFrom(rectTransform, camera) == 4; // True if all 4 corners are visible } /// /// Determines if this RectTransform is at least partially visible. /// Works by checking if any bounding box corner of this RectTransform is inside the screen space view frustrum. /// /// true if is at least partially visible; otherwise, false. /// Rect transform. /// Camera. Leave it null for Overlay Canvasses. public static bool IsVisibleFrom(this RectTransform rectTransform, Camera camera = null) { /* if(camera == null) { return false; } if (!rectTransform.gameObject.activeInHierarchy) return false; */ //modified 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; Vector3 tempScreenSpaceCorner; // Cached for (var i = 0; i < objectCorners.Length; i++) // For each corner in rectTransform { tempScreenSpaceCorner = camera.WorldToScreenPoint(objectCorners[i]); // Transform world space position of corner to screen space if (screenBounds.Contains(tempScreenSpaceCorner)) // If the corner is inside the screen { visibleCorners++; } } //modified return CountCornersVisibleFrom(rectTransform, camera) > 0; // True if any corners are visible } }