Search Unity

Scrolling a ScrollArea to the bottom always

Discussion in 'Immediate Mode GUI (IMGUI)' started by lucidcoder, Mar 2, 2014.

  1. lucidcoder

    lucidcoder

    Joined:
    Mar 23, 2010
    Posts:
    138
    At lines 66-67 on the script below, I set the Y value of where the scroll view is looking to be Mathf.Infinity, which should theoretically cause the scroll area to snap to the bottom. However when I have text fill the text area within the scroll area, it doesn't automatically snap to the bottom. Why is this?

    I can't seem to find the solution on UnityAnswers, everyone is saying that using Mathf.Infinity works.

    In addition to that, I can't figure out why the ScrollArea isn't following the content of the TextArea. For some reason, even when I overflow the TextArea full of text, it doesn't adjust the size of the horizontal scrollbar, it just stays the same size. Scrolling manually with the mouse has no effect. I'm not sure what to do here.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DevConsole : MonoBehaviour {
    5.    
    6.     private Vector2 consoleScrollView = Vector2.zero;
    7.     private static string consoleText = "";
    8.     private string inputText = "";
    9.     private bool showConsole = false;
    10.     private bool setFocus = false;
    11.     public GameObject[] disableMouseInputHere = null;
    12.     public GUISkin consoleSkin;
    13.     private int currentCommandRecall = 0;
    14.    
    15.     void Update() {
    16.        
    17.        
    18.         if (Input.GetKeyDown (KeyCode.BackQuote)) {
    19.             showConsole = !showConsole;
    20.            
    21.             if (disableMouseInputHere != null) {
    22.                 foreach (GameObject obj in disableMouseInputHere) {
    23.                     MouseLook mouselook = obj.GetComponent<MouseLook>();
    24.                     if (mouselook != null) {
    25.                         mouselook.enabled = !showConsole;
    26.                     }
    27.                 }
    28.             }
    29.            
    30.             if (transform.parent.gameObject.tag.Equals("Player")) {
    31.                 CharacterMotor motor = transform.parent.gameObject.GetComponent<CharacterMotor>();
    32.                 motor.enabled = !showConsole;
    33.             }
    34.         }
    35.        
    36.         if (Input.GetKeyDown (KeyCode.UpArrow)  CommandProcessor.getNumberOfCommandsSent() != 0) {
    37.             currentCommandRecall--;
    38.             if (currentCommandRecall < 0) currentCommandRecall = 0;
    39.             inputText = CommandProcessor.getPreviousCommand(currentCommandRecall);
    40.         }
    41.        
    42.         if (Input.GetKeyDown (KeyCode.DownArrow)  CommandProcessor.getNumberOfCommandsSent() != 0) {
    43.             currentCommandRecall++;
    44.             if (currentCommandRecall >= CommandProcessor.getNumberOfCommandsSent()) {
    45.                 if (CommandProcessor.getNumberOfCommandsSent()-1 >= 0) {
    46.                     currentCommandRecall = CommandProcessor.getNumberOfCommandsSent()-1;
    47.                 } else {
    48.                     currentCommandRecall = 0;
    49.                 }
    50.             }
    51.            
    52.             inputText = CommandProcessor.getPreviousCommand(currentCommandRecall);
    53.         }
    54.     }
    55.    
    56.     void Start() {
    57.         log ("Began logging at " + System.DateTime.Now.ToString ());
    58.     }
    59.    
    60.     void OnGUI () {
    61.         GUI.skin = consoleSkin;
    62.         if (showConsole) {
    63.            
    64.            
    65.             // Begin the ScrollView
    66.             consoleScrollView = GUI.BeginScrollView (new Rect (25, 25, Screen.width/2, Screen.height/2),
    67.                 new Vector2(consoleScrollView.x, Mathf.Infinity), new Rect (0, 0, Screen.width/2, Screen.height/2), true, true);
    68.            
    69.             GUI.SetNextControlName ("ConsoleView");
    70.        
    71.             // Put something inside the ScrollView
    72.             GUI.TextArea (new Rect (0, 0, Screen.width/2, Screen.height/2), consoleText);
    73.            
    74.             // End the ScrollView
    75.             GUI.EndScrollView();
    76.            
    77.             GUI.SetNextControlName ("ConsoleEntry");
    78.             inputText = GUI.TextField (new Rect (25, (Screen.height/2) + 28, Screen.width/2, 20), inputText);
    79.             inputText = inputText.Replace("`", "");
    80.             inputText = inputText.Replace("~", "");
    81.            
    82.             if (Input.GetKeyDown (KeyCode.Return)) {
    83.                 if (!inputText.Equals ("")) {
    84.                     log(">" + inputText);
    85.                     bool command = CommandProcessor.process (inputText, true);
    86.                     inputText = "";
    87.                    
    88.                     if (command) {
    89.                         currentCommandRecall = CommandProcessor.getNumberOfCommandsSent();
    90.                     }
    91.                 }
    92.             }
    93.            
    94.            
    95.            
    96.             if (!setFocus) {
    97.                 GUI.FocusControl ("ConsoleEntry");
    98.                 setFocus = true;
    99.             }
    100.         } else {
    101.             setFocus = false;
    102.         }
    103.        
    104.         GUI.skin = null;
    105.     }
    106.    
    107.     public static void log(string text) {
    108.         log(text, true);
    109.     }
    110.    
    111.     public static void log(string text, bool newline) {
    112.         consoleText += text;
    113.         if (newline) consoleText += "\n";
    114.     }
    115.    
    116. }
     
    Last edited: Mar 2, 2014