Search Unity

OnBecameVisible does not fire for CanvasRenderers

Discussion in 'UGUI & TextMesh Pro' started by casimps1, Jan 12, 2015.

  1. casimps1

    casimps1

    Joined:
    Jul 28, 2012
    Posts:
    254
    Not sure if this is a bug or by design, but I noticed that the OnBecameVisible event does not fire for UI widgets (CanvasRenderers). As soon as I add a MeshRenderer to my object, the event fires.

    Should this be firing? If not, is there an appropriate way to determine when a UI widget comes on screen? My use case is a long scrolling list of widgets that I only want to initialize once they are visible.

    I know I could just do some kind of RectTransform calculations in Update, but I was hoping for something more efficient/built-in.
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yea we dont have anything like onBecameVisible for UI widgets, this is by "design". Everything is technically visible from the codes perspective (even if its being hidden by a mask). We will eventually start culling things but thats a while out yet.
     
    JohnTube likes this.
  3. OldManAnimal

    OldManAnimal

    Joined:
    Jul 10, 2014
    Posts:
    45
    Would there be any reason to avoid putting mesh renderers on UI elements that you wanted to track in this way? I'm having the same kind of issue as casimps1 with a scroll list where I run into issues when updating just from scroll rect OnValueChanged so I think i'll have to change it to Update, which I would like to avoid. If I can simply add mesh renderers to the specific UI elements and use OnBecameVisible/Invisible that would probably work much better.
     
  4. KGC

    KGC

    Joined:
    Oct 2, 2014
    Posts:
    12
    Sorry for necroing an old post, but I had to do something similar today, although I avoided trying to mimic the callback OnBecameVisible/OnBecameInvisible and instead used a coroutine to check once in a while (0.5s intervals) for visibility, as I wanted to start a UI animation when a particular UI element became fully visible on screen.

    Here's a link to another forum answer where I posted the code: https://forum.unity3d.com/threads/test-if-ui-element-is-visible-on-screen.276549/#post-2978773

    Hope this helps someone out there :)
     
    sntahmaz and OreoSplitter like this.
  5. juan-jo

    juan-jo

    Joined:
    May 15, 2010
    Posts:
    162
    In my current case, working in RV, I am putting a canvas at the user's feet, which is not always visible, and whose calculations I want to do only when it is.

    Canvases can be in world space and other situations than in front of the camera, so will be usefull if UI renderers send this message too.
     
    nilsdr and Munchy2007 like this.
  6. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Any word on this?
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Having such functionality would have been handy for me right now too. I would like to only update UI widgets that are visible in their ScrollView.

    Is OnBecameVisible or similar functionality for UI on the roadmap?
     
  8. Dazo1985

    Dazo1985

    Joined:
    Mar 13, 2019
    Posts:
    26
    Also would like to see something like OnBecameVisible for ui
    including hidding by masks.
     
  9. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    I need an event when UI item became visible while scrooling to send an api call to server to retrieve profile photo. I am really surprised unity doesnt have such a method. @phil-Unity you can at least show us a way to do it???
     
  10. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Every maskable graphic (i.e. everything that renders that Unity provides) have a onCullStateChanged event that is triggered. assuming you have a Mask or RectMask2D on the scrollview you should be able to have a script that ties into that callback.
     
  11. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    I was in the need of OnBecameVisible & OnBecameInvisible events on uGUI widgets recently once again and decided to implement a generic Component to do the job.

    I call the Component
    UICullingBehaviour
    and I released it in the form of a Unity package on github under the MIT license.

    https://github.com/pschraut/UnityUICulling
     
    Last edited: Jul 13, 2022
    adrenak likes this.
  12. Aanandkumar

    Aanandkumar

    Joined:
    Apr 27, 2019
    Posts:
    3

    How we can set viewport runtime?