namespace UnityEngine.XR.Interaction.Toolkit.Samples.SpatialKeyboard { /// /// Manage the reuse and updating of data for each child button. /// public class XRKeyboardLayout : MonoBehaviour { [SerializeField] XRKeyboardConfig m_DefaultKeyMapping; /// /// Default key mapping for resetting key layout. /// public XRKeyboardConfig defaultKeyMapping { get => m_DefaultKeyMapping; set => m_DefaultKeyMapping = value; } [SerializeField] XRKeyboardConfig m_ActiveKeyMapping; /// /// Active which data is populated in these keys. /// public XRKeyboardConfig activeKeyMapping { get => m_ActiveKeyMapping; set { m_ActiveKeyMapping = value; PopulateKeys(); } } /// /// See . /// void Start() { PopulateKeys(); } /// /// Sets the active key mapping to the default key mapping. /// /// /// public void SetDefaultLayout() { if (m_DefaultKeyMapping != null) activeKeyMapping = m_DefaultKeyMapping; } /// /// Updates all child buttons with the data from the . /// /// /// This function returns without changing the keys if the number of child buttons is less than /// the number of mappings in the . /// public void PopulateKeys() { if (m_ActiveKeyMapping == null) return; var keyMappings = m_ActiveKeyMapping.keyMappings; var keys = GetComponentsInChildren(); if (keys.Length < keyMappings.Count) { Debug.LogWarning("Keyboard layout update failed: There are fewer keys than key mappings in the current config. Ensure there is a correct number of keys and key mappings.", this); return; } for (var i = 0; i < keyMappings.Count; ++i) { var mapping = keyMappings[i]; var key = keys[i]; key.character = mapping.character; key.displayCharacter = mapping.displayCharacter; key.shiftCharacter = mapping.shiftCharacter; key.shiftDisplayCharacter = mapping.shiftDisplayCharacter; key.keyFunction = mapping.overrideDefaultKeyFunction ? mapping.keyFunction : m_ActiveKeyMapping.defaultKeyFunction; key.keyCode = mapping.keyCode; key.displayIcon = mapping.displayIcon; key.shiftDisplayIcon = mapping.shiftDisplayIcon; key.SetButtonInteractable(!mapping.disabled); } } } }