using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BNG { public class VRTextInput : MonoBehaviour { UnityEngine.UI.InputField thisInputField; public bool AttachToVRKeyboard = true; public bool ActivateKeyboardOnSelect = true; public bool DeactivateKeyboardOnDeselect = false; public VRKeyboard AttachedKeyboard; [Header("Keyboard Position")] [Tooltip("Transform to position the keyboard relative to (usually the player's hand or head)")] public Transform playerKeyboardPosition; [Tooltip("Distance in front of camera")] public float keyboardDistance = 10f; [Tooltip("Height offset from camera")] public float keyboardHeight = 1f; bool isFocused, wasFocused = false; float lastActivatedTime = 0; void Awake() { thisInputField = GetComponent(); if(thisInputField && AttachedKeyboard != null) { AttachedKeyboard.AttachToInputField(thisInputField); } // If no player position is set, try to find the main camera (usually attached to player's head) if (playerKeyboardPosition == null) { if (Camera.main != null) { playerKeyboardPosition = Camera.main.transform; } } } void Update() { isFocused = thisInputField != null && thisInputField.isFocused; // Check if our input field is now focused if(isFocused == true && wasFocused == false) { OnInputSelect(); } else if (isFocused == false && wasFocused == true) { OnInputDeselect(); } wasFocused = isFocused; } /// /// Set the keyboard's active state and position /// /// Whether to show or hide the keyboard /// Optional position to place the keyboard. If null, uses player position public void SetKeyboardActive(bool active, Vector3? position = null) { if (AttachedKeyboard != null) { AttachedKeyboard.gameObject.SetActive(active); if (active) { Vector3 targetPosition; if (position.HasValue) { targetPosition = position.Value; } else if (playerKeyboardPosition != null) { // Position keyboard 10 units in front and 1 unit up from camera targetPosition = playerKeyboardPosition.position + playerKeyboardPosition.forward * keyboardDistance + playerKeyboardPosition.up * keyboardHeight; } else { return; // No position to place keyboard } AttachedKeyboard.transform.position = targetPosition; } } } public void OnInputSelect() { // Enable keyboard if disabled if (ActivateKeyboardOnSelect && AttachedKeyboard != null && !AttachedKeyboard.gameObject.activeInHierarchy) { SetKeyboardActive(true); AttachedKeyboard.AttachToInputField(thisInputField); lastActivatedTime = Time.time; } } public void OnInputDeselect() { // Deslect if valid keyboard is found, and did not recently activate if (DeactivateKeyboardOnDeselect && AttachedKeyboard != null && AttachedKeyboard.gameObject.activeInHierarchy && Time.time - lastActivatedTime >= 0.1f) { SetKeyboardActive(false); } } // Assign the AttachedKeyboard variable when adding the component to a GameObject for the first time void Reset() { var keyboard = GameObject.FindObjectOfType(); if(keyboard) { AttachedKeyboard = keyboard; Debug.Log("Found and attached Keyboard to " + AttachedKeyboard.transform.name); } } } }