I have a gallery of items dynamically populated at run-time time with Unity 4.6’s amazing GUI tools. It uses a scroll rect to handle overflow and a grid layout component. The problem is whenever I add a new item to the gallery, it moves the scrollbar to the center instead of the top. There must be a simple way to make new items not force scroll to the center and instead act like a W3C box model gallery you commonly see on the internet.
For now I’ve created a scrollbar manager script that can be used to enforce a new scroll position on demand. Hopefully there is a more natural solution to this, but maybe not.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace Menu {
public class ScrollbarManager : MonoBehaviour {
Scrollbar scrollH;
Scrollbar scrollV;
void Awake () {
ScrollRect rect = GetComponent<ScrollRect>();
scrollH = rect.horizontalScrollbar;
scrollV = rect.verticalScrollbar;
}
void Start () {
ForceScroll(1f);
}
public void ForceScroll (float pos) {
if (scrollH) scrollH.value = pos;
if (scrollV) scrollV.value = pos;
}
}
}