user2745184
user2745184

Reputation:

Unity3D ScrollView/ScrollBar from bottom to top

I'm trying to create a terminal emulator in Unity. I'm able to enter text and have it displayed as output above, and the output will generate a scrollbar when enough text is entered via a ScrollView.

However I can't figure out how to make the scrollbar jump to the bottom when a user enters text.

This is the method used to draw the GUI Window.

// relevant instance variables
    private Rect _windowPosition = new Rect();
            List<string> output = new List<string>();
            string user_input = "";
            Vector2 scroll_pos = new Vector2(1,1);

        ...
        ...
        ...

    private void onWindow(int windowID){
                GUILayout.BeginVertical();

            // Output is placed here. Is Scrollable
                scroll_pos = GUILayout.BeginScrollView( scroll_pos );
                GUILayout.Label( String.Join("\n", output.ToArray()) );
                GUILayout.EndScrollView();
            // Output ends here. Next piece is user input

                user_input = GUILayout.TextField(user_input);
                if (Event.current.Equals(Event.KeyboardEvent("return"))) {
                    output.Add(user_input);
                    user_input = ""; //clears the TextField
                    scroll_pos.x = 1;
                    scroll_pos.y = 1;
                }
                GUILayout.EndVertical();
                GUI.DragWindow();
            }

From my searching, I'd seen it said that I should just change the scroll_pos variable, since that's used to control/read the scrollbar's position. The scrollbar's value is normalized between 0 - 1.

I've tried forcing the scroll_pos values to be both, 0 and 1, but it has no impact. Beyond this I'm not sure how to approach the problem.

Upvotes: 2

Views: 4951

Answers (1)

Frohlich
Frohlich

Reputation: 963

Try to change

scroll_pos.x = 1;
scroll_pos.y = 1;

to

scroll_pos.y += 9999;

if you want to force scroll on horizontal do it with X too but usually consoles doesn't generates horizontal scroll, they force line breaks based on how many columns you configure.

Upvotes: 2

Related Questions