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:
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?
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.