How can i improve my loop? its too heavy to run
Hi there, im creating a scroll UI and im actually done, but however with my bad coding, this will work only if there is not more than 100 object, i tested for 5000 and it works! but it became very very heavy after i scroll for over 100, i know the problem is because im doing the whole loop every time, i had been trying to make it for every 50 but...... as my coding still is still newbie level, this is a big challenge for me Please anyone could help me this, Thanks
here is my loop coding
void CheckBottom(){
var p = GameObject.Find ("Content").transform;
for (int i = 0; i < _objectHeight.Length; i++) {
if (diffMaskCont > _lengthY [i]) {
if (GameObject.Find (i.ToString ()) == null) {
var spawnObject = GameObject.Instantiate (_instantiateObject [i]);
spawnObject.transform.SetParent (p);
spawnObject.name = i.ToString ();
}
}
}
}
void CheckTop(){
var p = GameObject.Find("Content").transform;
for (int i = _objectHeight.Length; i > 0; i--) {
if (diffContMaskMax > _lengthY[i]) {
if (GameObject.Find (i.ToString ()) == null) {
var spawnObject = GameObject.Instantiate(_instantiateObject[i]);
spawnObject.transform.SetParent (p);
spawnObject.name = i.ToString();
}
}
}
}
and this is my update coding
void Update(){
diffContMaskMax = Mathf.Round(_contentRect.offsetMax.y - _outMask.offsetMax.y);
if (diffContMaskMax < 0) {
diffContMaskMax = 0;
}
diffContMaskMin = Mathf.Round(_contentRect.offsetMin.y - _outMask.offsetMin.y);
if (diffContMaskMin > 0) {
diffContMaskMin = 0;
}
diffMaskCont = Mathf.Round(_outMask.sizeDelta.y + _contentRect.offsetMax.y - _outMask.offsetMax.y);
if (Mathf.Abs(_maskScrollRect.velocity.y) < forceZero) {
_maskScrollRect.velocity = new Vector2( 0,0);
}
if (_maskScrollRect.velocity.y != 0) {
if (_maskScrollRect.velocity.y > 0) {
CheckBottom ();
} else {
CheckTop ();
}
}
}
Comment
Your answer
