Search Unity

[uGUI] Console

Discussion in 'UGUI & TextMesh Pro' started by Dampire, Sep 7, 2014.

  1. Dampire

    Dampire

    Joined:
    Jan 18, 2014
    Posts:
    7
    Any way to receive "key pressed" event? In older posts they are present.
    Main idea of console. Need help in pseudocode section.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5. using System.Collections;
    6.  
    7. public class Console : MonoBehaviour, ISubmitHandler
    8. {
    9.     public Text text;
    10.     public string input;
    11.     private ConsoleLog log; //container for console log
    12.     private ConsoleCmdPool commands;
    13.  
    14.     public void Awake()
    15.     {
    16.         log = ConsoleLog.Instance;
    17.         commands = ConsoleCmdPool.Instance;
    18.         text = GetComponent<Text>();
    19.     }
    20.  
    21.     // refresh text in uGUI element
    22.     public void InputUpdated()
    23.     {
    24.         text.text = log.Log() + "\n" + input;
    25.     }
    26.  
    27.     // PSEUDOCODE HERE
    28.     public void SomeEvent(EventData data)
    29.     {
    30.         input += data.PressedKey;
    31.         InputUpdated();
    32.     }
    33.     // END OF PSEUDOCODE
    34.  
    35.  
    36.     // on Enter pressed
    37.     public void OnSubmit(BaseEventData data)
    38.     {
    39.         log.Log(input);
    40.         input = "";
    41.         InputUpdated();
    42.     }
    43. }
    44.  
     
  2. Dampire

    Dampire

    Joined:
    Jan 18, 2014
    Posts:
    7
    Okay. New question. Why this code sends 2 events. First - clear(or some escape character, dunno), second - letter.
    Code (CSharp):
    1.  
    2.     public void OnUpdateSelected(BaseEventData data)
    3.     {
    4.         while(Event.PopEvent(mEvent))
    5.         {
    6.             if(mEvent.rawType == EventType.KeyDown)
    7.             {
    8.                 Debug.Log(mEvent.character);
    9.                 input+=mEvent.character;
    10.                 InputUpdated();
    11.             }
    12.         }
    13.     }
    14.  
    Solved it anyway.
    Code (CSharp):
    1. char c = mEvent.character;
    2. if(c != 0)
    3.     input+=c.ToString();
    4. InputUpdated();
     
    Last edited: Sep 7, 2014