Game Development Reference
In-Depth Information
Implementing a
ScrollView
is fairly easy, for example:
1. Define the visible area along with a virtual background layer where the
controls will be drawn using a
BeginScrollView
function.
2.
Draw your controls in the virtual area. Any GUI drawing between the
ScrollView calls is drawn within the scroll area.
Note that 0,0 in the
ScrollView
is from the top-left of the
ScrollView
area and not the top-left-hand corner of the screen.
3.
Complete it by closing the control with the
EndScrollView
function. For
example, the previous example view was created with the following code:
private Vector2 scrollPosition = Vector2.zero;
private bool blnToggleState = false;
void OnGUI()
{
scrollPosition = GUI.BeginScrollView(
new Rect(25, 325, 300, 200),
scrollPosition,
new Rect(0, 0, 400, 400));
for (int i = 0; i < 20; i++)
{
//Add new line items to the background
addScrollViewListItem(i, "I'm listItem number " + i);
}
GUI.EndScrollView();
}
//Simple function to draw each list item, a label and checkbox
void addScrollViewListItem(int i, string strItemName)
{
GUI.Label(new Rect(25, 25 + (i * 25), 150, 25), strItemName);
blnToggleState = GUI.Toggle(
new Rect(175, 25 + (i * 25), 100, 25),
blnToggleState, "");
}