- Home /
Adding variables to Input Field and selecting an Input Field
Hello, I'm very new to Unity so I'm looking for some advice on how to do something properly. I've been asked to make a search box that will hold information from a search in different entries and allow the user to select an entry to get some feedback from the program. I've already created the search functionality by placing search results into a scroll view and it works great!
When I read an entry from the search results, I instantiate an input field and set its parent to be the content box in the scroll view. I also change the text to reflect the name of the search result. What I also need to have happen is that it holds on to an additional bit of info that has multiple fields, and I need to be able to detect when the user clicks on the entry so it can send that info to another part of the program. I'm not sure how to do either of these things properly, since the info is not a struct and I've yet to find a description on how to make an input box give a reaction by simply clicking on it.
What's the best way to accomplish this?
Answer by oStaiko · Apr 21, 2018 at 12:11 AM
You can add a Button UI element to anything. You just need to set the TargetGraphic field of it to what you want to act as a button. I usually make Text elements buttons, but for an input field, you might want to make a regular button as a child of it it, delete the text, and make the color to transparent (set alpha to zero). just two ways to solve that problem.
For the other part with multiple fields, I don't really understand what you mean, but here's a guess. You can make a UI panel appear and disapear when you hover over the component, and include the additional info you want as a Text component in that panel. To do this, you need to add a component to your input called "Event Trigger", and add two new events to it, "PointerEnter" and "PointerExt", they should be at the top. It works the same as a button, and you need to drag a function from a script that just activates and deactivates the panel.
public GameObject panel;
public void Hover (bool boo)
{
panel.SetActive(boo);
}
As a side note, I'm not sure why you are Instantiating an Input field to display results... Unless you want the user to be able to interact with it, use the Text component instead.
Your answer
Follow this Question
Related Questions
NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject() 1 Answer
Automatic navigation does not work with Inherited InputField 1 Answer
Input Field caret position is wrong when I type Chineese using Win10 System IME. 1 Answer
Making a UI.Button put text into a InputField 1 Answer
InputField limits characters unwantedly, even with no character limit 1 Answer