| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public static class RectTransformExtension
- {/// <summary>
- /// Counts the bounding box corners of the given RectTransform that are visible in screen space.
- /// </summary>
- /// <returns>The amount of bounding box corners that are visible.</returns>
- /// <param name="rectTransform">Rect transform.</param>
- /// <param name="camera">Camera. Leave it null for Overlay Canvasses.</param>
- 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;
- }
- /// <summary>
- /// 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.
- /// </summary>
- /// <returns><c>true</c> if is fully visible; otherwise, <c>false</c>.</returns>
- /// <param name="rectTransform">Rect transform.</param>
- /// <param name="camera">Camera. Leave it null for Overlay Canvasses.</param>
- 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
- }
- /// <summary>
- /// 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.
- /// </summary>
- /// <returns><c>true</c> if is at least partially visible; otherwise, <c>false</c>.</returns>
- /// <param name="rectTransform">Rect transform.</param>
- /// <param name="camera">Camera. Leave it null for Overlay Canvasses.</param>
- 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
- }
- }
|