I'm looking for a way to switch between input fields without having to double click the next input field twice.
What happens now is, for instance I type in something inside the first field, and then when I try to go to another field, I need to click on the other field once(or somewhere else besides the first input field) to remove focus, and then to click again on the other field to open up the keyboard and switch the focus to that field.
What can I do to make the switch between two input fields happen in one click(when clicking on the desired input field)
Thanks!
From your problem, it seems like you are working on Android/iOS devices.You could try calling TouchScreenKeyboard.Open() when the InputField is clicked, like so:
[RequireComponent(typeof(InputField))]
public class ShowKeyboardOnClick : MonoBehaviour {
private InputField inputField;
private void Awake() {
inputField = GetComponent<InputField>();
}
// Or raycast, etc
private void OnMouseDown() {
TouchScreenKeyboard.Open(inputField.text);
}
}
Note that from the documentation, it states:
Only native iPhone, Android, and Windows Store Apps are supported.
Related
Hi hope its not too dumb a question. I'm trying to see if i could drag the text in a button onto the on Click method of the button. The drag drop doesn't work.
If not, is the only way on script? Whats the best way to get the text via script?
(The text is added via a script).
Seemingly your DialogueManager.continue method takes a string as parameter
public void continue(string text)
{
...
}
-> No! You can't simply drag in a component into a string field.
Try to make your method rather take a Text/TMP_Text component as parameter
public void continue (Text /* or TMP_Text accordingly*/ text)
{
Debug.Log(text.text);
...
}
You can do something like that. The function you are calling needs to take what you want to pass as a parameter though.
So in this example the function is taking a GameObject.
public void DialogueButtonPressed(GameObject textGameObject)
{
}
You can pass whatever type you want though. Maybe you want to pass a UI.Text component.
I am creating a test project in Unity & I have an input field and a button. I would like to take the text from the input and simply Debug.Log to the Unity Console onclick of a button. All the examples show the variables and functions but don't show HOW they are getting the input to the variable. I already have UI set up including linking the function to the button. Is there some way I can give it an id like in JavaScript & HTML? I dont want to do anything fancy. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class main : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void debugInput() {
Debug.Log()
}
}
I would like to pass the variable in the Debug.Log in the debugInput function.
Can someone help me?
Thanks in advance
The InputField uses a callback for entering text to an input field called OnSubmit. If you would rather print every frame the user enters new text, there is also OnValueChanged.
These callbacks can be assigned in the inspector by selecting your InputField object. The inspector should look something like the below photo.
Simply click the + button, drag in the object that has the script that has the method you would like to callback. Make sure when selecting it to choose it from the Dynamic String list as it will auto pass in the string you have in the InputField to your function. You will need to change your code to take a string parameter tho.
public void debugInput(string str)
{
Debug.Log(str)
}
If you would rather set these values through code, you can do so by adding new listener delegates to your object. I can post a snippet for this solution if you would rather keep everything in a script.
Edit: I am using a TMPro Input Field, but the above example still works for a Unity UI Input Field. Instead of OnSubmit, you can use OnEndEdit. You can also add your own EventTriggers to use OnSubmit if you want.
I am making a 2d game in unity. I am using the UI toggle methods and wish to make one of those turn off when the other one is turned on. The method I have tried is doing the following code below (InfiniteMode and CampaignMode are the names of the two toggles):
if (InfiniteMode.isOn)
{
PlayInfinite();
CampaignMode.onValueChanged.Invoke(false);
}
if (CampaignMode.isOn)
{
PlayCampaign();
InfiniteMode.onValueChanged.Invoke(false);
}
Any help for what I can do to achieve this would be great.
As far as I understand you want to have two UI Toggles which acts as a group, when one of them is on, the rest of the items are off. In order to do this you can use Toggle Group component from Unity UI. You can find more information on that here: Toggle Group documentation.
If you look at Toggle component there is a Group variable. If you create an empty GameObject inside your Canvas and add Toggle Group component to it and assign that Toggle Group object to all your Toggle's which you want to be a part of that group
it should work.
You can add listeners to your toggles like this:
[SerializeField]
private Toggle myToggleVar;
private void Start() {
// UI event listeners
myToggleVar.onValueChanged.AddListener(delegate { onToggleExec(); });
}
private void onToggleExec() {
Debug.Log("Toggled!") //here you need to manipulate your other toggle
}
I'm using an Input Field in a Unity 3D game. When I enter text on my Windows 10 Mobile, and push the back button to dismiss the keyboard, Unity thinks I want to clear the Input Field. This behavior is not even mentioned in the documentation and I have not found a way to override it. I'd like to make it so the user can use the back button to dismiss the keyboard without reverting the Input Field. Any suggestions? Is this just a bug with Unity?
You can see the source code of InputField here: https://bitbucket.org/Unity-Technologies/ui/src/0155c39e05ca5d7dcc97d9974256ef83bc122586/UnityEngine.UI/UI/Core/InputField.cs?at=5.2&fileviewer=file-view-default
Apparently, clearing the field on escape is made by design. look at line 980 - 984:
case KeyCode.Escape:
{
m_WasCanceled = true;
return EditState.Finish;
}
What you can try is to create your own subclass of InputField and override the function
protected EditState KeyPressed(Event evt)
Of course it is not really clean, since you'll have to copy everything that the base InputField does in this function, except for lines 980 - 984.
The function KeyPressed cant be overridden.
I just added that in my function that listens on the value changes of the inputField:
if (Input.GetKeyDown (KeyCode.Escape)) {
return;
}
And it does exactly what is necessary :)
I have an issue trying to persist entities to the DB using code-first technique. For example of what I am doing you may look at the following MSDN Sample. The app generally works as intended except for one case.
If I have an existing entity and I bind it to a page that has a TextBox to hold the Title field and a AppBar icon to Save (similar to the 'New Task' screenshot in the above link, but with values pre-filled with existing entity with Two-Way binding), the following issue occurs. If I have the TextBox selected and I change the title and hit the save button, it updates the entity in-memory so that the full list now displays the new title. But the new title is not persisted to the DB (it does not auto-detect changes). This is weird, not just because the object in-memory has changed, but also because if I deselect the TextBox and then hit save, it will persist the changes to the DB.
I have seen other questions on SO with some change detection issues, they suggest adding this.Focus() or focusing some other element at the beginning of the save method. This does not help in my case. Unless I tap on screen to deselect the TextBox and hide the keyboard (or press Return key on the keyboard, which I bound to do this.Focus()), it won't detect the object as changed.
How can I address this? What exactly is stopping EF from detecting the object change when the keyboard is still visible?
Not sure if I follow exactly what you described but I think the problem is that the property you have bound your textbox to does not get updated until the TextChanged is fired on the textbox, and this is done first when you leave the Textbox, basically it will lose Focus if you tap somewhere else.
There is a simple workaround for this and it behaviors. By making a small behavior you can force the textbox to update the binding on each keystroke - so everything is updated while you type and keyboard is still there.
Behavior:
/// <summary>
/// Update property on every keystroke in a textbox
/// </summary>
public class UpdateTextSourceTriggerBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
this.AssociatedObject.TextChanged += OnTextBoxTextChanged;
}
void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
var bindingExpression = AssociatedObject.ReadLocalValue(TextBox.TextProperty) as BindingExpression;
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
}
protected override void OnDetaching()
{
this.AssociatedObject.TextChanged -= OnTextBoxTextChanged;
}
}
Now just attach this behavior to your textbox like this:
<TextBox Text="{Binding YourPropertyName, Mode=TwoWay}">
<i:Interaction.Behaviors>
<UpdateTextSourceTriggerBehavior/>
</i:Interaction.Behaviors>
</TextBox>
This will keep the property on your viewmodel updated all the time, so that when you tap on save directly after typing in the textbox it will save the correct value. Hope it helps!
Cheers,
Anders