Upgrading menu with input from a keybors probelm

Hi, i have just finished all the 4 courses for the RPG game and i’m trying to recreate it but adding others input types, like keyboard or gamepads, not only mouse, i have created a script to control menu buttons but i have a problem in the new game menu when i try to start in the input field digiting when i enter in the menu, at the moment it start selecting the first button (New game button) and i can go up to enter in the InputTextField, what i want is to start inside the input field so if i press some buttons in my keyboard it start to digit text in the field

using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace RpgAdventure.Utils
{
    public class MenuController : MonoBehaviour
    {
        [SerializeField] Color deselectedColor = Color.white;
        [SerializeField] Color selectedColor = Color.red;

        List<Button> buttons = new();
        TMP_InputField textField = null;
        int selectedbutton = 0;
        public bool active = false;

        private Input input;

        private void Awake()
        {
            input = new Input();
            InitializeInput();
            buttons = GetComponentsInChildren<Button>().ToList();
            textField = GetComponentInChildren<TMP_InputField>();
        }

        private void OnEnable()
        {
            active = true;
            input.General.Enable();
        }
        private void OnDisable()
        {
            active = false;
            input.General.Disable();
            DeselectAllButtons();
        }

        private void Update()
        {
            if (active)
            {
                //in caso non si stia usando un dispoitivo touch tipo cellulare o tablet
                if (SystemInfo.deviceType != DeviceType.Handheld)
                {
                    if (selectedbutton == -1)
                    {
                        //deseleziono tutti i bottoni
                        EventSystem.current.SetSelectedGameObject(null);
                        DeselectAllButtons();

                        return;
                    }
                    
                    EventSystem.current.SetSelectedGameObject(buttons[selectedbutton].gameObject);
                }


                //se il bottone è disattivato vado al bottone precedente
                if (selectedbutton > 0 && !buttons[selectedbutton].gameObject.activeInHierarchy)
                    selectedbutton--;

                foreach(Button button in buttons)
                {
                    if (button == buttons[selectedbutton])
                        button.GetComponentInChildren<TMP_Text>().color = selectedColor;
                    else
                        button.GetComponentInChildren<TMP_Text>().color = deselectedColor;
                }
            }
            else
            {
                //disattivo tutto
                selectedbutton = -1;
                EventSystem.current.SetSelectedGameObject(null);
            }
        }

        private void InitializeInput()
        {
            input.General.MoveThrow.started += ctx =>
            {
                if (active)
                {
                    //leggo l'input
                    Vector2 val = ctx.ReadValue<Vector2>();
                     //se premo giù o destra vado all'elemento successivo
                    if(val.x > 0 || val.y < 0)
                    {
                        //controllo di non essere già all'ultimo elemento
                        if(selectedbutton < buttons.Count - 1)
                        {
                            //se l'elemento in cui mi devo spostare è attivo proseguo
                            if (buttons[selectedbutton + 1].gameObject.activeInHierarchy)
                                selectedbutton++;
                            //se l'elemento successivo non è attivo provo a saltare a quello successivo ancora
                            else if (selectedbutton < buttons.Count - 2)
                                selectedbutton += 2;
                        }
                    }

                    //se premo su o sinistra vado all'elemento precedente
                    else if(val.x < 0 || val.y > 0)
                    {
                        //controllo di non essere già al primo elemento
                        if (selectedbutton > 0)
                        {
                            //se l'elemento in cui mi devo spostare è attivo proseguo
                            if (buttons[selectedbutton - 1].gameObject.activeInHierarchy)
                                selectedbutton--;
                            //se l'elemento successivo non è attivo provo a saltare a quello precedente ancora
                            else if (selectedbutton - 1 > 0)
                                selectedbutton -= 2;
                        }
                    }
                }
            };
        }

        /// <summary>
        /// Seleziona manualmente il bottone da rendere attivo
        /// </summary>
        /// <param name="selection"></param>
        public void SetButtonSelection(int selection)
        {
            selectedbutton = selection;
        }

        /// <summary>
        /// Rende tutti i bottoni con il colore di quando sono deselezionati
        /// </summary>
        private void DeselectAllButtons()
        {
            foreach (Button button in buttons)
                button.GetComponentInChildren<TMP_Text>().color = deselectedColor;
        }
    }
}

Anyone have any idea on how to do what i’m trying? i can also change the way of manipulate menu buttons, but the final result need to be that i can move throw the menus also via keyboard or via mouse or with a gamepad

If you’ve worked out switching between the buttons, actually setting the Inputfield to be first is relatively easy. Here’s what I put in on my own Inputfield:

using TMPro;
using UnityEngine;

public class SelectOnEnable : MonoBehaviour
{

    [SerializeField] private TMP_InputField inputField;

    private void OnEnable()
    {
        inputField.Select();
    }
}

I had tried but it didn’t work for me…
I had also tried adding inputField.ActivateInputField();

The problem is that if i enter in that menu the first button is always selected and the input field didn’t start to be writtable
To block button to work at the start is not a real problem, i have ideas to do that, but the input field don’t start at the enter of the menu.
I want to enter in the new game menu and if i click on a letter i want it to be added to the name of the game

If inputField.Select() isn’t working, then something else is grabbing the selection… Is the input field included in the list of buttons you’re using in your script? This may be yanking control away from the input field.

Privacy & Terms