0
\$\begingroup\$

I am trying to improve the efficiency of a big UI Scroll View in my Unity app. These Scroll Views tend to be inefficient and can be prone to jittery/stuttering motion if they are taxed.

I came across the following thread on Scroll Rect efficiency: https://stackoverflow.com/questions/53005040/what-i-have-learned-about-unity-scrollrect-scrollview-optimization-performan

One suggestion they make to put a canvas group on all elements within the scroll view and use a script to manipulate their opacity to make them transparent if they are not visible. I presume you would run on all elements every time the scroll content shifts (the window scrolls):

using UnityEngine;
using UnityEngine.UI;

public class ScrollHider : MonoBehaviour {
    static public float contentTop;
    static public float contentBottom;


    static public bool HideObject(GameObject givenObject, CanvasGroup canvasGroup, float givenPosition, float givenHeight) {
        if ((Mathf.Abs(givenPosition) + givenHeight > contentTop && Mathf.Abs(givenPosition) + givenHeight < contentBottom) || (Mathf.Abs(givenPosition) > contentTop && Mathf.Abs(givenPosition) < contentBottom)) {
            if (canvasGroup.alpha != 1) {
                canvasGroup.alpha = 1;
            }
            return true;
        } else {
            if (canvasGroup.alpha != 0) {
                canvasGroup.alpha = 0;
            }
            return false;
        }
    }

    static public void Setup(Scroll givenScroll) {
        contentTop = (1 - givenScroll.verticalNormalizedPosition) * (givenScroll.content.rect.height - givenScroll.viewport.rect.height);
        contentBottom = contentTop + givenScroll.viewport.rect.height;
    }
}

What's strange to me is a few things:

  1. Does setting the canvas group opacity to zero stop it from being rendered, or is it still rendered transparently and thus there is no actual benefit to this?

  2. Let's say your scroll view is for a SMS style chat window and you have 50-100 bubbles in it. Would running this script on every bubble every single time the content window scrolls a pixel be any more or less expensive than just leaving the bubbles there to render even if "off screen"?

I am curious if this is truly a viable optimization. Does it do anything at all? And even if it does, are you just trading one cost for another?

Thanks.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ A case for a profiler, make a big UI with and without opacity and compare the results \$\endgroup\$
    – Zibelas
    Dec 5, 2021 at 10:32
  • \$\begingroup\$ It seams like something that you will figure out the best by testing. I did a quick test and it seams that objects with canvas group alpha set to 0 are not getting batched so it is probably valid. But isn't it simpler to turn objects that you are not using off. And also it is recommended to attach separate canvas on a scroll rect. \$\endgroup\$
    – Ivan
    Dec 6, 2021 at 10:41

1 Answer 1

1
\$\begingroup\$

There are other things going on in a canvas/UI other than rendering, such as re-organizing layouts and so on. So especially if things keep changing and happening in the UI, you may want to disable it altogether rather than setting the alpha to 0.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .