Remove Keyboard Input Field Unity [duplicate] - unity3d

I know how to hide or display Virtual Keyboard using InputMethodManager.
But I want to enter text in EditText using Physical keyboard but I don't want to display Virtual Keyboard in Unity 3D Android.
How can I do that in Unity 3D?

There is no such thing as EditText in Unity. InputField is used to receive input from a device.
You can disable Virtual Keyword with InputField on Android. Not sure if this will work for other platforms.
Your InputField:
public InputField inputField;
Disable Virtual Keyboard:
inputField.keyboardType = (TouchScreenKeyboardType)(-1);
Enable Virtual Keyboard:
inputField.keyboardType = TouchScreenKeyboardType.Default;
If you run into weird issues, consider deriving your script from InputField, then disable Virtual Keyboard and finally, call the base Start function of the InputField:
public class HideVirtualKeyboard : InputField
{
protected override void Start()
{
keyboardType = (TouchScreenKeyboardType)(-1);
base.Start();
}
}

Old question I know, but what you really want is to set inputField.touchScreenKeyboard.active = false; You may need to hold it false in a LateUpdate
[SerializeField]
private bool keyboardEnabled = true;
// toggle this to toggle native keyboard activity
public bool KeyboardEnabled { get { return keyboardEnabled; } set { keyboardEnabled = value; } }
private void LateUpdate()
{
#if UNITY_ANDROID
if (inputField.touchScreenKeyboard != null && !keyboardEnabled)
{
inputField.touchScreenKeyboard.active = false;
}
#endif
}

Related

Cursor not visible first time escape is pressed but all subsequent times it is

I am new to learning C# so I'm trying to figure out little bits at a time. Today's task is to have my cursor appear and disappear as I open a panel. I know that unity is weird and you have to build your project to see the cursor changes. However, on said build, when I press escape my panel appears but my cursor doesn't. If I close and reopen the panel the cursor will show up and then everything works as it should.
Something in my code that is causing this or is it just a unity bug?
Also, since I am new to this, any advice is appreciated. Thank you for your time!
public class MenuManager : MonoBehaviour
{
public GameObject pauseMenu;
public bool gamePause = false;
bool cursorHide = true;
// Start is called before the first frame update
void Start()
{
UpdateCursor();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
cursorHide = !cursorHide;
UpdateCursor();
PauseMenuOpen();
}
}
private void PauseMenuOpen()
{
if (!gamePause)
{
pauseMenu.SetActive(true);
gamePause = true;
}
else
{
pauseMenu.SetActive(false);
gamePause = false;
}
}
private void UpdateCursor()
{
Cursor.visible = !cursorHide;
}
public void QuitGame()
{
Application.Quit();
}
}
Use this code. it'll work
private bool isCursorHide;
private void Update(){
if (Input.GetKeyDown(KeyCode.Escape))
CursorToggler();
}
private void CursorToggler(){
if(isCursorHide){
isCursorHide =!isCursorHide;
Cursor.visible = isCursorHide;
}
else{
isCursorHide =!isCursorHide;
Cursor.visible = isCursorHide;
}
}

How to change Vuforia AnchorInputListenerBehaviour?

How to change Vuforia the AnchorInputListenerBehaviour, the original setting is clicked on the screen, I want to change it to judge the Micro Switch high or low signal?
You don't need the Vuforia code at all!
You seem to rather just want to have your own custom UnityEvent in the Inspector you can invoke whenever you want to ;)
If you need it you can even extend this and add some parameters as described here using UnityEvent<T> e.g. something like
[Serializable]
public class CustomEvent : UnityEvent<bool> { }
public class Example : MonoBehaviour
{
public CustomEvent _onButtonStateChanged;
private bool lastState;
// Now however you check your button states and get your pin data
// on whatever condition you can simply invoke your event
private void Update()
{
//TODO
var currentState = GetYourButtonStateByMagic();
if(currentState != lastState)
{
_onButtonStateChanged.Invoke(currentState);
lastState = currentState;
}
}
}

can the ui.inputfield have the data from both text box and input displayed overlaying each other?

I have a Unity Droid app with an ui.inputfield. I am giving the user the ability to enter numbers or move a slider. In either case I need to have the slider data appear in the inputfield or update the slider position with the number typed into the inputfield. I can get the data from both the inputfield or the slider but I need to have the data displayed. My main concern is can I have the data from both inputs displayed at the exact same location as the inputfield, like on top?
I can not find any examples of the data display on top of the input field. Is this the correct way to think about this?
Thank you in advance.
There are callbacks whenever the user changes a UI.Slider or finishes entering a value in a UI.InputField:
UI.Slider.onValueChanged
UI.InputField.onEndEdit
So yes you could link them to each other like e.g.
public class Example : MonoBehaviour
{
public InputField inputField;
public Slider slider;
private void Awake()
{
inputField.onEndEdit.AddListener(OnInputFieldChanged);
slider.onValueChanged .AddListener( OnSliderChanged);
OnSliderChanged(slider.value);
}
private void OnInputFieldChanged(string newText)
{
if (float.TryParse(newText, out var value))
{
value = Mathf.Clamp(value, slider.minValue, slider.maxValue);
inputField.text = value.ToString("F");
slider.value = value;
}
else
{
Debug.LogWarning("Input Format Error!", this);
slider.value = Mathf.Clamp(0, slider.minValue, slider.maxValue);
inputField.text = slider.value.ToString("F");
}
}
private void OnSliderChanged(float newValue)
{
inputField.text = newValue.ToString("F");
}
}

Write a letter in InputField at Caret Position

I have created some buttons in Unity. When a button is clicked a function is called to write text on an InputText component. The function is following for example:
public void JButton()
{
textshowed.text = textshowed.text + "J";
}
But if I want to write a letter in between a line, the word is written at last of line.
What should i do for the letter to written where the cursor/caret is?
note that it doesnt handle selections in InputField. Below might be enough for your purpose?
public class button : MonoBehaviour
{
public InputField myInput;
int caretposition;
public void JButton()
{
myInput.text = myInput.text.Insert(caretposition, "J");
//Debug.Log(myInput.caretPosition);
}
private void Update()
{
if (myInput.isFocused)
{
caretposition = myInput.caretPosition;
}
}
}

Unity UI Button keeps double clicking, how can I work around this?

I have two buttons set up in an interactive fiction game, each press calls a new line of text. The problem is, every time I press on the button, I get two logged debug messages informing me of the click and my game moves two sections of text.
I've tried many different things to try to work around this including trying to alter the submit input in project settings and many different code forms. Any help would be greatly appreciated.
Here's my current code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AdventureGame : MonoBehaviour
{
[SerializeField]
private Text _textComponent;
[SerializeField]
private State _startingState;
private State state;
[SerializeField]
private Button _input0Button;
[SerializeField]
private Button _input1Button;
[SerializeField]
private Text _choices1;
[SerializeField]
private Text _choices2;
private bool _buttonOnePressed;
private bool _buttonTwoPressed;
void Start()
{
state = _startingState;
_textComponent.text = state.GetStateStory();
_input0Button.onClick.AddListener(Input0Button);
_input1Button.onClick.AddListener(Input1Button);
_buttonOnePressed = false;
_buttonTwoPressed = false;
}
void Update()
{
ManageState();
}
private void ManageState()
{
if (state._choice == true)
{
_choices1.text = state.GetChoiceOne();
_choices2.text = state.GetChoiceTwo();
_textComponent.text = state.GetStateStory();
_input0Button.gameObject.SetActive(true);
_input1Button.gameObject.SetActive(true);
if(_buttonOnePressed == true)
{
StartCoroutine(WaitForItOne());
}
else if(_buttonTwoPressed == true)
{
StartCoroutine(WaitForItTwo());
}
}
else if (state._choice == false)
{
_choices1.text = state.GetChoiceOne();
_choices2.text = state.GetChoiceTwo();
_textComponent.text = state.GetStateStory();
_input0Button.gameObject.SetActive(true);
_input1Button.gameObject.SetActive(false);
if(_buttonOnePressed == true)
{
StartCoroutine(WaitForItOne());
}
}
}
private void ManageChoiceOne()
{
_buttonOnePressed = false;
State[] _newState = state.GetNextStatesArray();
state = _newState[0];
}
private void ManageChoiceTwo()
{
_buttonTwoPressed = false;
State[] _newState = state.GetNextStatesArray();
state = _newState[1];
}
public void Input0Button()
{
Debug.Log("Input 0 pressed");
_buttonOnePressed = true;
}
public void Input1Button()
{
Debug.Log("Input 1 pressed");
_buttonTwoPressed = true;
}
IEnumerator WaitForItOne()
{
yield return new WaitForSeconds(3.0f);
ManageChoiceOne();
}
IEnumerator WaitForItTwo()
{
yield return new WaitForSeconds(3.0f);
ManageChoiceTwo();
}
}
First of all you keep starting new Coroutines each frame as long as e.g. _buttonOnePressed == true .. you wait 3 seconds before you finally unset this flag!
Then for the double call make sure the callbacks are not configured also in the Inspector! It seems like you have them once in the Inspector and additionally add them in your Start method so they are called twice!
Note that you won't see the callbacks added on runtime in the Inspector!
Why are you even using Update here at all? It is quite redundant to poll the state and the bool values and constantly check and handle their states in each frame. I would rather simply start the routine in the button method itself and make the whole code event driven instead!
(optionally) To give the user better feedback I would additionally in the meantime during the 3 seconds make the buttons non-interactable .. keep them active but not clickable:
// Remove state;
// Remove _buttonOnePressed
// Remove _buttonTwoPressed
private void Start()
{
// Either remove this two lines or the callbacks set in the Inspector
_input0Button.onClick.AddListener(Input0Button);
_input1Button.onClick.AddListener(Input1Button);
ManageState(_startingState);
}
// Remove Update
// This will be called only when actually needed
// since the state is passed in as parameter you don't need the private field
private void ManageState(State state)
{
// These happen in both cases anyway
_choices1.text = state.GetChoiceOne();
_choices2.text = state.GetChoiceTwo();
_textComponent.text = state.GetStateStory();
_input0Button.gameObject.SetActive(true);
// Here directly use the state flag
// since the button is disabled when needed
// there is no need for having different "states"
// since anyway only the according button(s) is(are) available
_input1Button.gameObject.SetActive(state._choice);
}
// (optional) Flag for avoiding concurrent routines
// Should be impossible since buttons get disabled but you never know
private bool alreadyHandlingButton;
private IEnumerator ManageChoice(bool isButtonOne)
{
// (optional) Skip if another routine running
if(alreadyHandlingButton) yield break;
// (optional) Block other routines just in cade
alreadyHandlingButton = true;
// Disable interactions
_input0Button.interactable = false;
_input1Button.interactable = false;
// This is the same for both buttons
yield return new WaitForSeconds(3f);
State[] _newState = state.GetNextStatesArray();
var state = _newState[isButtonOne ? 0 : 1];
// Only call ManageState when the state is actually changing
ManageState(state);
// (optional) Allow a new routine
alreadyHandlingButton = false;
// Enable interactions
_input0Button.interactable = true;
_input1Button.interactable = true;
}
public void Input0Button()
{
// (optional) Ignore if other button is already handled
if(alreadyHandlingButton) return;
Debug.Log("Input 0 pressed");
StartCoroutine(ManageChoice(true));
}
public void Input1Button()
{
// (optional) Ignore if other button is already handled
if(alreadyHandlingButton) return;
Debug.Log("Input 1 pressed");
StartCoroutine(ManageChoice(false));
}