- Home /
GUILayout.BeginScrollView cant seem to change where the box I create is positoned.
Hi I am trying to make a chat window for my multiplayer Game. I have done everything except for the fact that the window where the text is placed is always in the top left hand corner of the screen and it is driving me nuts.
Vector2 scrollPosition = new Vector2(5698, 312); // I have put an abstract value here to show that this values does not matter at all and no matter what value I write here it does the same thing.
// and this is the text inside OnGui()
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(450), GUILayout.Height(450));
foreach (var cm in chatHistory)
{
GUILayout.Label(cm);
}
GUILayout.EndScrollView();
This bow always ends up being placed in the top left hand corner of the screen and I do not understand why. how can I tell the Scrollview to have a certain x and Y coordinate and a certain width and height.
thanks.
Answer by Bunny83 · Aug 14, 2017 at 09:41 PM
A ScrollView is also a layouted gui element when you use the GUILayout version. Like any GUILAyout element you never explicitly specify the position. Every element gets it's position and size from the layoutsystem.
One way is to use a horizontal and a vertical layout group and use a Space before the ScrollView. However that might be a bit too complicated for your case. If you just want to specify where a certain layour group should be positioned you can use GUILayout.BeginArea
and GUILayout.EndArea
around your ScrollView. In BeginArea you have to specify a screen rect. Everything between BeginArea and EndArea will be layouted seperately.
The Scroll position only controls the scroll offset of the child objects of the ScrollView. Though you can only scroll when the child objects would overflow the scrollview area. The same way your browser won't allow any scrolling then the whole website fits onto the screen / window.
Hi Bunny and thank you for answering my question - before I accept it as correct I just want to make sure I get everything clear - I have wrapped my code with the "beginarea" and have been able to position it where I want it. What I dont fully understand is your last paragraph and the reason as to why I cannot scroll now. I have made the "scrollview" the same size and the scrollbar does appear, but it wont scroll up. I do have a Peice of code that forces it down but it doesnt seem to be affecting it in anyway in this case. Have tried commenting it out with no change.
here is the relevant code now with your input:
Vector2 scrollPosition = new Vector2(0, 0); // variable
GUILayout.BeginArea(new Rect(0, Screen.height - 255, 450, 222));
scrollPosition = GUILayout.BeginScrollView(new Vector2(0, Screen.height -255), GUILayout.Width(450), GUILayout.Height(222));
foreach (var cm in chatHistory)
{
GUILayout.Label(cm);
}
GUILayout.EndScrollView();
GUILayout.EndArea();
current$$anonymous$$essage = GUI.TextField(new Rect(0, Screen.height - 25, 450, 25), current$$anonymous$$essage);
current$$anonymous$$essage = Regex.Replace(current$$anonymous$$essage, @"[^a-zA-Z0-9/ ]", "");
You currently use a fix scroll position. You pass in
new Vector2(0, Screen.height -255)
So the scroll view will always show the same part. In your original code you correctly pass scrollPosition
.
Fantastic thanks for the help and good explanation.