Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Turn chat on/off with "return" key

Discussion in 'Scripting' started by cruising, Aug 6, 2014.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Trying to figure out how to do so, but i cant get it to work,
    Take a look (JS)

    Code (JavaScript):
    1. import Debug;
    2.  
    3. private var log:Array = new Array();
    4. var maxLogMessages:int = 1000;
    5. var visible:boolean = true;
    6. var stringToEdit : String = "";
    7. var selectTextfield : boolean = true;
    8.  
    9. function Start() {
    10.  
    11.     Input.eatKeyPressOnTextFieldFocus = false;
    12.         log.Add("Alpha Build 1.0");
    13. }
    14.  
    15. function print(string:String)
    16.  
    17.     {
    18.  
    19.         log.push(string);
    20.         if(log.length > maxLogMessages)
    21.             log.RemoveAt(0);
    22.     }
    23.  
    24.     private var scrollPos:Vector2 = Vector2(0, 0);
    25.     private var lastLogLen:int = 0;
    26.     var printGUIStyle:GUIStyle;
    27.     var maxLogLabelHeight:float = 100.0f;
    28.  
    29. function OnGUI() {
    30.  
    31.         if(visible) {
    32.         GUI.SetNextControlName ("chatWindow");
    33.         stringToEdit = GUI.TextField (Rect (0.0, Screen.height - 50, 500, 20), stringToEdit, 250);
    34.  
    35.     if (!selectTextfield) {  
    36.             GUI.FocusControl ("chatWindow");  
    37.  
    38.         }
    39.    
    40.     var logBoxWidth:float = 500.0;
    41.     var logBoxHeights:float[] = new float[log.length];
    42.     var totalHeight:float = 0.0;
    43.     var i:int = 0;
    44.  
    45.         for(var string:String in log) {
    46.  
    47.                 var logBoxHeight = Mathf.Min(maxLogLabelHeight, printGUIStyle.CalcHeight(GUIContent(string), logBoxWidth));
    48.                 logBoxHeights[i++] = logBoxHeight;
    49.              totalHeight += logBoxHeight+10.0;
    50.         }
    51.  
    52.     var innerScrollHeight:float = totalHeight;
    53.  
    54.     // if there's a new message, automatically scroll to bottom
    55.     if(lastLogLen != log.length) {
    56.         scrollPos = Vector2(0.0, innerScrollHeight);
    57.                 lastLogLen = log.length;
    58.         }
    59.  
    60.     scrollPos = GUI.BeginScrollView(Rect(0.0, Screen.height-150.0-50.0, 500, 150), scrollPos, Rect(0.0, 0.0, 180, innerScrollHeight));
    61.  
    62.             var currY:float = 0.0;
    63.             i = 0;
    64.  
    65.             for(var string:String in log) {
    66.  
    67.                 logBoxHeight = logBoxHeights[i++];
    68.                 GUI.Label(Rect(10, currY, logBoxWidth, logBoxHeight), string, printGUIStyle);
    69.                 currY += logBoxHeight+10.0;
    70.             }
    71.  
    72.             GUI.EndScrollView();
    73.         }
    74.  
    75. }
    76.  
    77. function Update () {
    78.  
    79.     if(Input.GetKeyDown("return")) {
    80.         if(selectTextfield) {
    81.             selectTextfield = !selectTextfield;
    82.         }
    83.         if(stringToEdit != "") {
    84.             log.Add("" + stringToEdit );
    85.             stringToEdit = "";
    86.             }
    87.         }
    88.     }
     
  2. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
    Be more specific please!
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    When i press "return" i activate the chat box and that works, and when i press enter i sending my message, but doesnt unactivate the chatbox, if i walk i keep typing WASD in the chat.
     
  4. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
    Im not entirely sure, but arent you saying on line 35 that if you arent selecting the textfield it should focus chatwindow?
    Should focus nothing in gui i guess?
     
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    True, but i dont know how to unfocus the chatwindow properly
     
  6. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
  7. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329