Search Unity

Enabling/ Disabling UI Panel>Scroll Rect> content objects when Screen /Off screen

Discussion in 'UGUI & TextMesh Pro' started by ethancorssel, Apr 25, 2016.

  1. ethancorssel

    ethancorssel

    Joined:
    Apr 25, 2016
    Posts:
    6
    I made a level menu on a Scroll Rect with 30 levels on it, for a 2d puzzle game for iOS.
    I will add more levels soon, but I realize that when more objects add on the Scroll Rect content and I test the game runs more slowly.
    So I want to disable the objects (In this case are just Images and Text components for the levels button and number) when there are off screen, and enabled them again when On Screen.
    I tried adding BoxCollider2D on the objects and making a BoxTrigger on the top of the UI so when OnTriggerEnter2D disabled the Objects. The problem was (beside they don't disable) I can't enabled them again.

    I'm hoping someone known a better way to do this please.

    Also I think in something like Camera.ViewPortToScreenPoint for enable or disable in some specific Y (transform) position, but I'm getting confused applying it.
     
  2. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    902
    The lowest handing fruit is simply to attach a 2d Rect Mask component to your ScrollRect or Content view INSTEAD OF the regular stencil-based "Mask" component that you might already be using. The 2d Rect Mask actually does do culling of Graphics that are outside of its Rect.

    If that still doesn't give you enough of a boost, then you can do further "culling" of your own by using GetWorldCorners of your container and your children as they move, but this is much more involved and there are lots of tradeoffs. We ended up doing this in one of our scrollrect's because the simple act of culling from 2d Rect Mask was too expensive itself (showed up in the profiler as thousands of calls to Graphic.Cull()).

    But try the 2d Rect Mask by itself first - you might already be good with only that.
     
    Daedolon likes this.