├── .gitattributes ├── README.md └── uGUI ├── GConsoleCanvas.prefab ├── GConsoleSimpleToggle.cs ├── GConsoleuGUI.cs ├── GConsoleuGUIInput.cs └── GConsoleuGUISuggestion.cs /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GConsole-uGUI 2 | ============= 3 | 4 | uGUI Frontend for GConsole (Unity 4.6) 5 | 6 | See [GConsole](https://github.com/Rahazan/GConsole). 7 | 8 | #Setup: 9 | 10 | * Make sure GConsole is present in your project. 11 | * There is a prefab GameObject Canvas that you can add and use. 12 | 13 | 14 | GConsole: https://github.com/Rahazan/GConsole 15 | 16 | #Do note this is written for uGUI and therefore needs Unity 4.6 to work. 17 | #Latest code works well under Unity 5 18 | -------------------------------------------------------------------------------- /uGUI/GConsoleCanvas.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MuNgLo/GConsole-uGUI/d13dfa4973b5080b6c7310241b3f3782ef07bf71/uGUI/GConsoleCanvas.prefab -------------------------------------------------------------------------------- /uGUI/GConsoleSimpleToggle.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class GConsoleSimpleToggle : MonoBehaviour { 5 | public string toggleCMD = string.Empty; 6 | public GameObject consoleObject; 7 | 8 | void Update () { 9 | if (toggleCMD == string.Empty) { 10 | return; 11 | } 12 | if (Input.GetButtonDown (toggleCMD)) { 13 | consoleObject.SetActive( !consoleObject.activeSelf ); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /uGUI/GConsoleuGUI.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.UI; 3 | using UnityEngine.EventSystems; 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | using System; 7 | 8 | [AddComponentMenu("Scripts/Gconsole-uGUI/GConsoleuGUI")] 9 | public class GConsoleuGUI : MonoBehaviour 10 | { 11 | public int max_OutputLength = 500; // If we put to much text in the textarea Unity will go nuts 12 | public Text consoleOutput; 13 | public InputField input; 14 | public GConsoleuGUISuggestion[] suggestions; 15 | public bool clearOnSubmit = false; 16 | public bool reselectOnSubmit = true; 17 | public int minCharBeforeSuggestions = 1; 18 | 19 | void Start () 20 | { 21 | GConsole.OnOutput += OnOutput; //Register the "OnOutput" method as a listener for console output. 22 | input.GetComponent ().uGUI = this; 23 | foreach(GConsoleuGUISuggestion sugg in suggestions) { 24 | sugg.uGUI = this; 25 | } 26 | } 27 | 28 | void OnEnable () 29 | { 30 | input.Select (); 31 | input.ActivateInputField (); 32 | } 33 | 34 | void OnOutput (string line) 35 | { 36 | if (consoleOutput.text.Length > max_OutputLength) { // Shorten the textlength so Unity can handle it 37 | consoleOutput.text = consoleOutput.text.Substring( ( consoleOutput.text.Length - max_OutputLength ), max_OutputLength ); 38 | } 39 | consoleOutput.text += '\n' + line; // add the console output to the output textarea TODO make this clamp length 40 | } 41 | 42 | public void OnInput () 43 | { 44 | string cmd = input.text; 45 | if (string.IsNullOrEmpty (cmd)) { 46 | return; 47 | } 48 | GConsole.Eval (cmd); //Send command to the console 49 | if (clearOnSubmit) { 50 | input.text = string.Empty; 51 | } 52 | if (reselectOnSubmit) { 53 | input.Select (); 54 | input.ActivateInputField (); 55 | } 56 | } 57 | 58 | public void OnChange () 59 | { 60 | LoadSuggestions (); 61 | } 62 | 63 | private void LoadSuggestions () 64 | { 65 | List sugitems; 66 | //Not enough characters typed yet, no suggestions to be shown! 67 | if (minCharBeforeSuggestions != 0 && input.text.Length < minCharBeforeSuggestions) { 68 | sugitems = new List (); 69 | } else { 70 | sugitems = GConsole.GetSuggestionItems (input.text); //Ask GConsole for suggestions. 71 | } 72 | //Display suggestions (and hide unused suggestion boxes by passing null). 73 | for (int i = 0; i < suggestions.Length; i++) { 74 | if (i < sugitems.Count) 75 | suggestions [i].ShowSuggestion (sugitems [i]); 76 | else 77 | suggestions [i].ShowSuggestion (null); 78 | } 79 | } 80 | 81 | public void OnSuggestionClicked (string line) 82 | { 83 | input.text = line.Split(' ')[0]; 84 | input.Select (); 85 | input.ActivateInputField (); 86 | input.MoveTextEnd (false); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /uGUI/GConsoleuGUIInput.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.UI; 3 | 4 | [AddComponentMenu("Scripts/Gconsole-uGUI/GConsoleuGUIInput")] 5 | public class GConsoleuGUIInput : MonoBehaviour 6 | { 7 | [HideInInspector] 8 | public GConsoleuGUI uGUI; 9 | private string oldvalue; 10 | private InputField input; 11 | 12 | void Start () 13 | { 14 | input = GetComponent (); 15 | input.onEndEdit.AddListener (onEndEdit); 16 | input.onValueChange.AddListener (onChangeEdit); 17 | } 18 | 19 | void onEndEdit (string line) 20 | { 21 | if (Input.GetButtonDown ("Submit")) { // make sure we only realy as a submition if submit key was used same frame (Ugly but workaround since uGUI triggers submit when inputfield is deselected) 22 | uGUI.OnInput (); 23 | } 24 | } 25 | 26 | void onChangeEdit(string line) 27 | { 28 | if (input.text != oldvalue) { 29 | uGUI.OnChange (); 30 | } 31 | oldvalue = input.text; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /uGUI/GConsoleuGUISuggestion.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.UI; 3 | using System.Collections; 4 | 5 | [AddComponentMenu("Scripts/Gconsole-uGUI/GConsoleuGUISuggestion")] 6 | public class GConsoleuGUISuggestion : MonoBehaviour 7 | { 8 | [HideInInspector] 9 | public GConsoleuGUI uGUI; 10 | [HideInInspector] 11 | public Text label; 12 | 13 | void Start() { 14 | GetComponent