Unity Contrôle de la navigation dans l’interface utilisateur et utilisation de la touche TAB.

YouTube player
Les éléments de l’interface utilisateur ont un ordre de navigation définit implicitement et automatiquement.

Cependant vous pouvez les définir manuellement comme vous l’explique ce tutoriel vidéo.

Pour naviguer d’un bouton à un autre ou encore vers un champ de saisie, vous utilisez les touches directionnelles.

Cependant pour une application de bureau cela peut être déroutant car on utilise souvent la touche TAB.

Voici un script qui permet d’utiliser cette touche TAB en plus des touches directionnelles :


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TabScript : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
GameObject c = EventSystem.current.currentSelectedGameObject;
if (c == null) { return; }
Selectable s = c.GetComponent<Selectable>();
if (s == null) { return; }
//Expression conditionelle avec Opérateur ternaire
Selectable next = Input.GetKey(KeyCode.LeftShift) ? s.FindSelectableOnUp() : s.FindSelectableOnDown();
if (next != null) { next.Select(); }
}
}
}
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class TabScript : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Tab)) { GameObject c = EventSystem.current.currentSelectedGameObject; if (c == null) { return; } Selectable s = c.GetComponent<Selectable>(); if (s == null) { return; } //Expression conditionelle avec Opérateur ternaire Selectable next = Input.GetKey(KeyCode.LeftShift) ? s.FindSelectableOnUp() : s.FindSelectableOnDown(); if (next != null) { next.Select(); } } } }
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class TabScript : MonoBehaviour
{
  
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {            
            GameObject c = EventSystem.current.currentSelectedGameObject;
            if (c == null) { return; }

            Selectable s = c.GetComponent<Selectable>();
            if (s == null) { return; }

            //Expression conditionelle avec Opérateur ternaire
            Selectable next = Input.GetKey(KeyCode.LeftShift) ? s.FindSelectableOnUp() : s.FindSelectableOnDown();
            if (next != null) { next.Select(); }
        }
    }
}

UPLN

A propos de upln 280 Articles
En informatique le problème se situe souvent entre la chaise et le clavier !

Soyez le premier à commenter

Poster un Commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur la façon dont les données de vos commentaires sont traitées.