I'm usually not one to ask for help but I need to humble myself right now. I am working on an infinite scrolling level map similar to what Candy Crush has but no exact. I know, I know, tons of people have already asked how to mimic that. I am not looking for that. I have a working script that instantiates each item/section just fine. What I would like to do to keep it resembling infinite is I want to destroy any section that goes beyond a specified buffer below the bottom of the screen/viewport. However, if the user scrolls back, I want those same destroyed sections to re-instantiate in the original position they were in while now destroying any section that has moved past a specified buffer past the top of the screen. I thought about using object pooling but when I attempted that, it either crashed Unity or instantiated 3 sections, seemed to work but once you got to the end of the 3rd section, everything disappeared. I currently have an invisible trigger collider image on the scroll view and another on the section/item prefab. When they collide or pass each other, a new section is spawned on top of the previous. The reason I want to destroy sections that are no longer in view is that I want to improve memory efficiency and performance.
#Infinite scroll rect help
1 messages · Page 1 of 1 (latest)
Here's my code:
using UnityEngine;
using UnityEngine.UI;
public class ScrollMapManager : MonoBehaviour
{
[Header("Assign in Inspector")]
public RectTransform content;
public RectTransform scrollTrigger;
public GameObject mapSectionPrefab;
private float lastYPosition = 0f;
void Start()
{
ConfigureContentAnchors();
SpawnNewSection(); // Start with one section
}
void ConfigureContentAnchors()
{
content.pivot = new Vector2(0.5f, 0); // Bottom pivot
content.anchorMin = new Vector2(0, 0);
content.anchorMax = new Vector2(1, 0);
content.anchoredPosition = Vector2.zero;
}
public void SpawnNewSection()
{
GameObject newSection = Instantiate(mapSectionPrefab, content);
RectTransform sectionRT = newSection.GetComponent<RectTransform>();
LayoutRebuilder.ForceRebuildLayoutImmediate(sectionRT); // Ensure size is correct
// Position it above the last
float newY = lastYPosition;
sectionRT.anchoredPosition = new Vector2(0, newY);
lastYPosition += sectionRT.rect.height;
// Assign reference for overlap check
SectionTrigger trigger = newSection.GetComponent<SectionTrigger>();
if (trigger != null)
trigger.Initialize(scrollTrigger, this);
}
}
https://assetstore.unity.com/packages/templates/tutorials/endless-runner-sample-game-87901
Unity has a mobile sample with the concept of "endless running" if you wanna dissect it to get a better idea.
Oh wait, Candy Crush. I'm thinking of another game, but I don't remember that having some 'infinite' scrolling mechanic.
So if you've any references maybe I can give you an idea
I don't want to copy candy crush's logic exactly because they are using a spherical or cylindrical object and placing each themed section over that but they do have an infinite scroll feature. I am almost there but I need the sections that go off screen below the botton of the screen/viewport to be destroyed and then reinstantiated if the user scrolls back in that direction
I'd probably use this as the reference for when to create new elements on the scroll rect:
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.ScrollRect-verticalNormalizedPosition.html
As for pooling, I wouldn't bother and instead create and destroy the few UI elements you're creating
I've seen that. That is similar for an endless runner for creating new obljects and I can destroy an object but if the user scrolls back down I need to be able to reinstantiate those objects. Think of it like this, lets say you're playing an endless runner and then decide to turn around and go back. Those platforms/objects you just passed would need to come back or respawn. How can I do that efficiently?
Everything in my scene are UI elements. Including the sections
This is my hierarchy:
Endless runners are usually 3D like that project linked above, but what you're trying to do is make a scroll rect on the UI and create elements as you scroll. The idea is you're needing to create and remove those elements when you've scrolled too far ahead so any of those gameobjects need to be transfered into pure data than objects on the scene.
In an endless runner, usually three chunks at minimum are spawned and when you reach the end of the middle chunk the most previous chunk is recycled and a new chunk is spawned ahead.
If they aren't on the scene, then they are in data hanging out in memory if say you do want to make a runner game where you can turn around. This way you know exactly what chunk to respawn if the player chooses to do so.
Would that be utilizing pooling or a dictionary to store the data?