Drag button text into button on Click function: Unity Inspector - unity3d

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.

Related

Getting input field value in unity then Debug.Log ing it

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.

Unity Switch between input fields with one click

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.

Pushing back button/escape reverts text in Input Field

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 :)

A panel to change 3d object properties when right click on it

I'm building a UI system to edit selected 3D objects. I have spheres on the screen and I want to be able to edit their proprieties (radius for example). In order to do that I want the user to be able to click on a sphere and then it show a panel next to the object. When the user changes slider value the radius of the sphere which as been clicked on change. Using the new unity event system, I think it's easy to achieve, but I'm a new to unity and even if I know the basic I don't know how to organize this properly.
At the end my goal would be to select multiple sphere, right click and edit the radius to all selected spheres.!
Any tips on how to do that ? Sorry for my poor english. Thanks
Here's how you can do it:
Keep a bool for tracking if the object is selected, say, bool _isSelected
Toggle this boolean in the OnMouseUpAsButton() function. This function is called when you click on an object and release that click on the same object. Unity's OnMouseXXX() interfaces are very good for handling mouse events. You just need to define this function in the sphere objects' script.
Put together a Slider and arrange its value interval from editor. Define a function that will edit the scale value of the spheres and tie that function to that slider.
Below you can see an example of how I did that in a game of mine.
Unity simply catches the slider's value if you provide it with a public void _f(float val). It will appear on the top of the list as "Dynamic float". So, in your case, your slider update function should look like this:
public void SphereScaleSliderUpdate(float val)
{
foreach (GameObject sphere in GameObject.FindGameObjectsWithTag("your tag here"))
{
if (sphere.IsSelected) // don't forget to provide a public getter for _isSphere variable
{
sphere.transform.localScale = new Vector3(val, val, val);
}
}
}
Also, you need to create another script for toggling the menu(Canvas/Panel(optional)) that contains the slider, and you may add it to another object, like a UIManager. Assign the Canvas in the game hierarchy to a variable in that script, and then scan for RClick in the Update() function of the UIManager.
.
void Update()
{
if (Input.GetMouseButtonDown(1) // RClick event
{
// makes the slider appear through enabling canvas
_canvasHandle.enabled = true;
}
}

GWT ClickableTextCell

I am a newbie to GWT ... need some help in understanding the below class -
What is the use of GWT ClickableTextCell ??
Is there something specific use of it. It uses something called FieldUpdater, why is that used ?
Think of a ClickableTextCell as hot spot. It looks like a regular bit of screen real estate with text in it, but it responds to clicks. What happens when you click it? The click "updates" the field. An update to a field calls the method update(). What does update() do? Whatever you want it to. You provide it by specifying the FieldUpdater. FieldUpdater is an interface, so you can construct one anonymously. Say you have a CellTable, and you have a Column that displays a String inside a ClickableTextCell. You provide your FieldUpdater to the Column:
Column<DataType, String> myIntegerColumn
= new Column<DataType, String>(new ClickableTextCell());
myIntegerColumn.setFieldUpdater(new FieldUpdater<DataType, String>(){
#Override
public void update(int index, DataType object, String value){
// execute code that reacts to a click on this hot spot
}
});
Now whenever the cell gets clicked, that code in update() fires.
A ClickableTextCell is a specific kind of cell. You can see a demo of all the different kinds of cells in this GWT showcase.
This GWT documentation explains what cell widgets are for, goes over all of the different types, and also has examples of how to use them and the ValueUpdater type.