Example #1
0
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.F12))
            {
                var enabled = !canvas.gameObject.activeSelf;
                ShowConsoleUI(enabled);
            }

            if (!canvas.gameObject.activeSelf)
            {
                return;
            }

            if (commandInputField.isFocused)
            {
                // The caret is at last position and accept suggestions keys are triggered
                if (commandInputField.caretPosition == commandInputField.text.Length &&
                    (Input.GetKeyDown(KeyCode.Tab) || Input.GetKeyDown(KeyCode.RightArrow)))
                {
                    var suggestions = DevConsole.GetSuggestions(commandInputField.text);


                    if (suggestions != null)
                    {
                        if (suggestions.Length == 1)
                        {
                            //commandInputField.text = suggestions.First();
                            var list = Helper.GetCommandParts(commandInputField.text).ToList();
                            list[list.Count - 1] = suggestions.First();

                            commandInputField.text = string.Join(" ", list);
                        }
                        else
                        {
                            consoleLog.Log($" > {commandInputField.text}");
                            consoleLog.Log(string.Join("\n", suggestions));
                        }
                    }

                    commandInputField.caretPosition = commandInputField.text.Length;
                }

                // Navigate to previous commands
                if (Input.GetKeyDown(KeyCode.UpArrow))
                {
                    navigateHistory        = Mathf.Clamp(--navigateHistory, 0, DevConsole.CommandHistory.Count - 1);
                    commandInputField.text = DevConsole.CommandHistory.ElementAt(navigateHistory);

                    commandInputField.caretPosition = commandInputField.text.Length;
                }

                // Navigate to next commands
                if (Input.GetKeyDown(KeyCode.DownArrow))
                {
                    navigateHistory        = Mathf.Clamp(++navigateHistory, 0, DevConsole.CommandHistory.Count - 1);
                    commandInputField.text = DevConsole.CommandHistory.ElementAt(navigateHistory);

                    commandInputField.caretPosition = commandInputField.text.Length;
                }
            }
        }