I am wanting to use Unity’s UI Input Field as a means for searching predefined words, aided by autocomplete prompts. The user input needs to be restricted only to valid search terms (perhaps defined in a generic dictionary or what ever is appropriate).
Can anyone provide me any direction as to how this may be achieved w/o resorting to purchasing assets?
Try this code out. It will restrict wrong input, and it gives you a list with all matching words.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WordSearch : MonoBehaviour
{
//All words you want your user to pick from
string[] dictionary = new string[] { "Hallo", "Input", " Abde", "Hey", "kill" };
//the inputfield your user is typing to
public InputField inputField;
public bool caseSensetive;
public bool restrictUserInput;
ArrayList possibleWords;
void Start()
{
inputField.onValueChange.AddListener(delegate { ValueChangeCheck(); });
possibleWords = new ArrayList(dictionary.Length);
}
void ValueChangeCheck()
{
possibleWords.Clear();
//Go through all words in your databas (could be slow with many words)
for (int i = 0; i < dictionary.Length; i++)
{
//Check if the words start matches the start from the input
if (dictionary*.StartsWith(inputField.text, !caseSensetive, null))*
possibleWords.Add(i); } if (possibleWords.Count == 0 && restrictUserInput) inputField.text = inputField.text.Remove(inputField.text.Length -1,1); // Iterate through all possible words for (int i = 0; i < possibleWords.Count; i++) { Debug.Log(dictionary[(int)possibleWords*]); // do something with the word* //may display it } } }