Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Test if UI element is visible on screen

Discussion in 'UGUI & TextMesh Pro' started by mimminito, Oct 28, 2014.

  1. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Hi,

    I am trying to test if a UI element is visible on the screen. Im currently using a Image control, and would like to know if its visible by the camera, or if its outside of its bounds. I cannot seem to find a way to determine this, and as we do not have a renderer component I cannot do a GeometryUtility.TestPlanesAABB test.
    Any help would be appreciated.

    Adam
     
  2. KGC

    KGC

    Joined:
    Oct 2, 2014
    Posts:
    11
    Sorry for necroing an old post, but I had a similar issue today, and could not find a complete solution via Goggle, so I wrote my own based on the fragmented solutions I could find on Unity Answers. Tested on a Canvas set to Screen Space Camera mode.

    Code (CSharp):
    1. bool isFullyVisible = myRectTransform.IsFullyVisibleFrom(myCamera);

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public static class RendererExtensions
    4. {
    5.     /// <summary>
    6.     /// Counts the bounding box corners of the given RectTransform that are visible from the given Camera in screen space.
    7.     /// </summary>
    8.     /// <returns>The amount of bounding box corners that are visible from the Camera.</returns>
    9.     /// <param name="rectTransform">Rect transform.</param>
    10.     /// <param name="camera">Camera.</param>
    11.     private static int CountCornersVisibleFrom(this RectTransform rectTransform, Camera camera)
    12.     {
    13.         Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height); // Screen space bounds (assumes camera renders across the entire screen)
    14.         Vector3[] objectCorners = new Vector3[4];
    15.         rectTransform.GetWorldCorners(objectCorners);
    16.  
    17.         int visibleCorners = 0;
    18.         Vector3 tempScreenSpaceCorner; // Cached
    19.         for (var i = 0; i < objectCorners.Length; i++) // For each corner in rectTransform
    20.         {
    21.             tempScreenSpaceCorner = camera.WorldToScreenPoint(objectCorners[i]); // Transform world space position of corner to screen space
    22.             if (screenBounds.Contains(tempScreenSpaceCorner)) // If the corner is inside the screen
    23.             {
    24.                 visibleCorners++;
    25.             }
    26.         }
    27.         return visibleCorners;
    28.     }
    29.  
    30.     /// <summary>
    31.     /// Determines if this RectTransform is fully visible from the specified camera.
    32.     /// Works by checking if each bounding box corner of this RectTransform is inside the cameras screen space view frustrum.
    33.     /// </summary>
    34.     /// <returns><c>true</c> if is fully visible from the specified camera; otherwise, <c>false</c>.</returns>
    35.     /// <param name="rectTransform">Rect transform.</param>
    36.     /// <param name="camera">Camera.</param>
    37.     public static bool IsFullyVisibleFrom(this RectTransform rectTransform, Camera camera)
    38.     {
    39.         return CountCornersVisibleFrom(rectTransform, camera) == 4; // True if all 4 corners are visible
    40.     }
    41.  
    42.     /// <summary>
    43.     /// Determines if this RectTransform is at least partially visible from the specified camera.
    44.     /// Works by checking if any bounding box corner of this RectTransform is inside the cameras screen space view frustrum.
    45.     /// </summary>
    46.     /// <returns><c>true</c> if is at least partially visible from the specified camera; otherwise, <c>false</c>.</returns>
    47.     /// <param name="rectTransform">Rect transform.</param>
    48.     /// <param name="camera">Camera.</param>
    49.     public static bool IsVisibleFrom(this RectTransform rectTransform, Camera camera)
    50.     {
    51.         return CountCornersVisibleFrom(rectTransform, camera) > 0; // True if any corners are visible
    52.     }
    53. }
     
    Last edited: Mar 2, 2017
  3. Anisoropos

    Anisoropos

    Joined:
    Jul 30, 2012
    Posts:
    93
    Thanks @KGC , your code works like a charm, is well documented and abides to best practices (static helper functions with private functionality and public accessors). :)
     
    drhmiri likes this.
  4. nngafook

    nngafook

    Joined:
    Sep 7, 2013
    Posts:
    5
    @KGC, you're a god!
    Thanks! <3
     
  5. Sir-Gatlin

    Sir-Gatlin

    Joined:
    Jan 18, 2013
    Posts:
    28
    Thank you! this is awesome. Noob question, where would be the best place to implement this? does it have to go into the update method? or is there a better way to listen for it to change?
     
  6. diegoadrada

    diegoadrada

    Joined:
    Nov 27, 2014
    Posts:
    54
    I have the same doubt, because call this from update method doesn't looks like an optimal way.
     
    Sir-Gatlin likes this.
  7. KGC

    KGC

    Joined:
    Oct 2, 2014
    Posts:
    11
    So in our project we use it to check if a UI animation should play. In that case we use a coroutine that checks either every frame or with some time delay (i think its the latter). It really depends. If you need to know with highest precision, then yes, put it in an update function. But you could probably get away with calling it only every 5th frame or so - in that that just use a coroutine :)
     
    Sir-Gatlin likes this.
  8. CalebBarton

    CalebBarton

    Joined:
    Jun 24, 2017
    Posts:
    3
    Thanks for the post KGC. I was just about to develop something similar, but you've taken the hard work out for me.
     
  9. YoungDeveloper

    YoungDeveloper

    Joined:
    Jun 28, 2013
    Posts:
    50
    If you are caching Vector3 tempScreenSpaceCorner then you might especially do it with .Length
     
    diegoadrada likes this.
  10. jjlehtinen

    jjlehtinen

    Joined:
    Aug 7, 2018
    Posts:
    1
    This doesn't seem to take into account the fact that the UI element might be covered by something else, so not actually visible, only if the bounds are contained within the camera bounds?
     
  11. Assambra

    Assambra

    Joined:
    May 22, 2017
    Posts:
    2
    @KGC, is it possible that I use the RenderExtension.cs in an open source unity asset, if I specify you as author?
     
  12. KGC

    KGC

    Joined:
    Oct 2, 2014
    Posts:
    11
    Sure, go right ahead :)
     
  13. Biggix

    Biggix

    Joined:
    Dec 8, 2014
    Posts:
    41
    I have faced a similar issue (I needed to know whether a RectTransform is at least partially visible within my camera viewport) and somehow the solution posted by @KGC didn't work for me. I believe this might happen because my RectTransforms were deeply nested inside a couple of ScrollViews so GetWorldCorners might be failing under those conditions.

    In any way, to solve this I resorted to a 3rd party plugin called EasierUI, which provides methods such as GetPosition and GetSize in world units.

    Code (CSharp):
    1. public static bool IsPartlyVisible(this RectTransform rectTransform, Camera camera)
    2.     {
    3.        
    4.         bool result = false;
    5.        
    6.         Vector2 pos = rectTransform.GetPosition(CoordinateSystem.ScreenSpacePixels, true);
    7.         Vector2 size = rectTransform.GetSize(CoordinateSystem.ScreenSpacePixels) * 0.5f;
    8.        
    9.         Vector2[] objectCorners = new Vector2[4];
    10.        
    11.         objectCorners[0] = new Vector2(pos.x - size.x, pos.y - size.y);
    12.         objectCorners[1] = new Vector2(pos.x - size.x, pos.y + size.y);
    13.         objectCorners[2] = new Vector2(pos.x + size.x, pos.y - size.y);
    14.         objectCorners[3] = new Vector2(pos.x + size.x, pos.y + size.y);
    15.        
    16.         Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height);
    17.        
    18.         int visibleCorners = 0;
    19.        
    20.         for (var i = 0; i < objectCorners.Length; i++)
    21.         {
    22.             if (screenBounds.Contains(objectCorners[i]))
    23.             {
    24.                 visibleCorners++;
    25.             }
    26.         }
    27.        
    28.         if (visibleCorners>0) // If at least one corner is inside the screen
    29.         {
    30.             result = true;
    31.         }
    32.        
    33.         return result;
    34.     }
     
  14. WorldWideGlide

    WorldWideGlide

    Joined:
    Nov 3, 2013
    Posts:
    22
    I came across this thread and it helped me solve a similar issue that I was having. Here's a similar function that finds how much a UI element is offscreen and returns the offset. You can add this offset to the UI elements transform position to position it back on screen.

    Code (CSharp):
    1. public static Vector3 GetGUIElementOffset(RectTransform rect)
    2.     {
    3.         Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height);
    4.         Vector3[] objectCorners = new Vector3[4];
    5.         rect.GetWorldCorners(objectCorners);
    6.  
    7.         var xnew = 0f;
    8.         var ynew = 0f;
    9.         var znew = 0f;
    10.  
    11.         for (int i = 0; i < objectCorners.Length; i++)
    12.         {
    13.             if (objectCorners[i].x < screenBounds.xMin)
    14.             {
    15.                 xnew = screenBounds.xMin - objectCorners[i].x;
    16.             }
    17.             if (objectCorners[i].x > screenBounds.xMax)
    18.             {
    19.                 xnew = screenBounds.xMax - objectCorners[i].x;
    20.             }
    21.             if (objectCorners[i].y < screenBounds.yMin)
    22.             {
    23.                 ynew = screenBounds.yMin - objectCorners[i].y;
    24.             }
    25.             if (objectCorners[i].y > screenBounds.yMax)
    26.             {
    27.                 ynew = screenBounds.yMax - objectCorners[i].y;
    28.             }
    29.         }
    30.  
    31.         return new Vector3(xnew, ynew, znew);
    32.  
    33.     }
     
    spiritworld likes this.
  15. Alexhator

    Alexhator

    Joined:
    May 23, 2019
    Posts:
    2
    Awesome!
    In my case, what I am trying to do is:

    Having a Scroll, detect if an item is visible or not. So, instead of considering the size of the screen is the size of the scroll. But I have not managed to make it work.

    Any ideas?

    Thank you in advance!
     
  16. Pavlko

    Pavlko

    Joined:
    Dec 23, 2012
    Posts:
    6
    Hello there,

    I successfully used your script @KGC, and I extended it to support RectTransforms in Overlay Canvasses (you just dont pass the Camera parameter).
    I also added a check for .activeInHierarchy to return false.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public static class RectTransformExtension
    4. {
    5.     /// <summary>
    6.     /// Counts the bounding box corners of the given RectTransform that are visible in screen space.
    7.     /// </summary>
    8.     /// <returns>The amount of bounding box corners that are visible.</returns>
    9.     /// <param name="rectTransform">Rect transform.</param>
    10.     /// <param name="camera">Camera. Leave it null for Overlay Canvasses.</param>
    11.     private static int CountCornersVisibleFrom(this RectTransform rectTransform, Camera camera = null)
    12.     {
    13.         Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height); // Screen space bounds (assumes camera renders across the entire screen)
    14.         Vector3[] objectCorners = new Vector3[4];
    15.         rectTransform.GetWorldCorners(objectCorners);
    16.  
    17.         int visibleCorners = 0;
    18.         Vector3 tempScreenSpaceCorner; // Cached
    19.         for (var i = 0; i < objectCorners.Length; i++) // For each corner in rectTransform
    20.         {
    21.             if (camera != null)
    22.                 tempScreenSpaceCorner = camera.WorldToScreenPoint(objectCorners[i]); // Transform world space position of corner to screen space
    23.             else
    24.             {
    25.                 Debug.Log(rectTransform.gameObject.name+" :: "+objectCorners[i].ToString("F2"));
    26.                 tempScreenSpaceCorner = objectCorners[i]; // If no camera is provided we assume the canvas is Overlay and world space == screen space
    27.             }
    28.  
    29.             if (screenBounds.Contains(tempScreenSpaceCorner)) // If the corner is inside the screen
    30.             {
    31.                 visibleCorners++;
    32.             }
    33.         }
    34.         return visibleCorners;
    35.     }
    36.  
    37.     /// <summary>
    38.     /// Determines if this RectTransform is fully visible.
    39.     /// Works by checking if each bounding box corner of this RectTransform is inside the screen space view frustrum.
    40.     /// </summary>
    41.     /// <returns><c>true</c> if is fully visible; otherwise, <c>false</c>.</returns>
    42.     /// <param name="rectTransform">Rect transform.</param>
    43.     /// <param name="camera">Camera. Leave it null for Overlay Canvasses.</param>
    44.     public static bool IsFullyVisibleFrom(this RectTransform rectTransform, Camera camera = null)
    45.     {
    46.         if (!rectTransform.gameObject.activeInHierarchy)
    47.             return false;
    48.  
    49.         return CountCornersVisibleFrom(rectTransform, camera) == 4; // True if all 4 corners are visible
    50.     }
    51.  
    52.     /// <summary>
    53.     /// Determines if this RectTransform is at least partially visible.
    54.     /// Works by checking if any bounding box corner of this RectTransform is inside the screen space view frustrum.
    55.     /// </summary>
    56.     /// <returns><c>true</c> if is at least partially visible; otherwise, <c>false</c>.</returns>
    57.     /// <param name="rectTransform">Rect transform.</param>
    58.     /// <param name="camera">Camera. Leave it null for Overlay Canvasses.</param>
    59.     public static bool IsVisibleFrom(this RectTransform rectTransform, Camera camera = null)
    60.     {
    61.         if (!rectTransform.gameObject.activeInHierarchy)
    62.             return false;
    63.  
    64.         return CountCornersVisibleFrom(rectTransform, camera) > 0; // True if any corners are visible
    65.     }
    66. }
     
    Leniaal likes this.
  17. mmmshuddup

    mmmshuddup

    Joined:
    Feb 9, 2015
    Posts:
    1
    I tried this in VR and it only partially worked. My canvas starts in the viewport and this extension returns 4 visible corners and as I slow look away it goes down to 3, then 2, and then 1 and then slowly goes back up as I turn around.

    I think this script may have assumed the camera would be static but in my case it's not, the camera viewport is constantly changing as the user looks around, so how can I rectify that?

    I tried using:

    Code (CSharp):
    1. Transform camForward = camera.forward;
    2. Rect screenBounds = new Rect(camForward.x, camForward.y, Screen.width, Screen.height);
    But that hasn't worked. I also tried replacing WorldToScreenPoint with WorldToViewportPoint but that didn't work either.

    EDIT:

    I finally fixed this by checking the z axis like so:

    Code (CSharp):
    1. if (tempScreenSpaceCorner.z >= 0 && screenBounds.Contains(tempScreenSpaceCorner)) {
    2.     visibleCorners++;
    3. }
    if tempScreenSpaceCorner.z is less than 0 then you've totally faced away from the RectTransform!
     
    Last edited: Nov 5, 2019
  18. Leniaal

    Leniaal

    Joined:
    Nov 7, 2012
    Posts:
    66
    Thanks a lot Pavlko
     
unityunity