How hide/active child in scrollview

I have a scrollview, whose child has a vertical layout group, how could I hide or activate the children depending on whether they are on the screen or not. I have used this code but it disables all the children:

    Vector3[] childCorners = new Vector3[4];
    child.GetWorldCorners(childCorners);

    Vector3[] viewportCorners = new Vector3[4];
    viewport.GetWorldCorners(viewportCorners);

    bool isAboveBottom = childCorners[0].y < viewportCorners[2].y; 

    bool isBelowTop = childCorners[2].y > viewportCorners[0].y;   

    return isAboveBottom && isBelowTop;

Thanks

Why are you doing this? If you think you must do this for performance reasons, have you actually done testing with the profiler that tells you this will be helpful?

Beware that being in a layout group and still doing this will be a challenge: only active objects are going to be considered for layout, so every time you diddle the objects active/inactive, they may potentially move.

Otherwise, nothing about being in a Scrollview changes how you call .SetActive() or anything else like that so hook it up appropriately.

I didn’t test with the profile but with the statistics window I get:
Bathes: 321
Tris: 90K

If I manually hide those that are not shown on the screen I get:
Bathes: 230
Tris: 80K

If they are shown, don’t you think it’s better if they are not active in the scene?
image

That’s not how performance is reasoned about.

For all performance and optimization issues, ALWAYS start by using the Profiler window:

Window → Analysis → Profiler

Generally optimization is:

  • avoid doing the thing that is slow (easiest)
  • do it fewer times and store its result
  • do the slow thing less frequently over time
  • do the slow thing when nobody cares much (during level loading or in another thread, etc.)
  • find a faster way to do the thing (hardest)

DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, there is only ONE way to find out: measuring with the profiler.

Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.

Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

Remember that you are gathering information at this stage. You cannot FIX until you FIND.

Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

Don’t forget about the Frame Debugger window either, available right near the Profiler in the menu system.

Notes on optimizing UnityEngine.UI setups:

At a minimum you want to clearly understand what performance issues you are having:

  • running too slowly?
  • loading too slowly?
  • using too much runtime memory?
  • final bundle too large?
  • too much network traffic?
  • something else?

If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.

Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.

“Software does not run in a magic fairy aether powered by the fevered dreams of CS PhDs.” - Mike Acton

Thank you very much for the information.

I’m going to start using the profile generator and see if I can find the exact error.

The target platform is VR (Pico 4 Enterprise) and the problem is that I have a kind of tablet in the experience, which is the culprit of lowering the performance of the game.