Search Unity

Auto-complete Text Field

Discussion in 'Scripting' started by sherlockturtle, Jul 3, 2012.

  1. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    Is there a way to set up auto complete for a text input. To put in a list of words or phrases and when some of it was typed in sugest it.
     
  2. UnlimitedEdition

    UnlimitedEdition

    Joined:
    Feb 27, 2012
    Posts:
    287
    I don't know of a way in Unity with out hard coding it yourself. If you make it your self, feel free to sell it on the asset store for 5 - 10 dollars.

    :)
    Bennet
     
  3. Ing Rico

    Ing Rico

    Joined:
    Apr 19, 2013
    Posts:
    1
    Here example.... free ;)

    usingUnityEngine;
    usingSystem.Collections;
    usingSystem.Collections.Generic;

    public class AutocompleteSearch : MonoBehaviour {

    List<string> words = newList<string>();
    string myText = "";
    publicTextMesh textAutocomplete;
    void Start () {
    words.Add("Maria Martinez");
    words.Add("Maria Gonzales");
    words.Add("Mario Gonzales");
    }

    void OnGUI () {
    string oldString = myText;
    myText = GUI.TextField(newRect(10, 10, 200, 20), myText);
    if (!string.IsNullOrEmpty(myText) && myText.Length > oldString.Length) {
    List<string> found = words.FindAll( w => w.StartsWith(myText) );
    if (found.Count > 0){
    //myText = found[0];
    textAutocomplete.text = found[0];
    print (found.Count);
    }
    }
    }
    }
     
  4. AgentFoxtrot

    AgentFoxtrot

    Joined:
    Apr 6, 2015
    Posts:
    7
    It would be nice if people actually thanked others for their gracious (and free) assistance here. I had the same issue and Ing Rico's script really helped me out. Thank you very much for it.
     
    MishaMa, danny1111 and larku like this.
  5. Indigital

    Indigital

    Joined:
    May 22, 2017
    Posts:
    1
    Here is my code you can edit it and enjoy keep coding by kunal Paul
    *)what I m doing here is searching from the input field and displaying matching result by comparing it with the list
    special thank to log rico.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using UnityEngine.UI;

    public class Myautocomplete : MonoBehaviour {

    List<string> Name= new List<string>(){"hairy","harsh","hussain","huttry","habibi","hemlet","kunal","krunal","pravin","pal","paul","parkash","patharwal","pandey"};
    public GameObject PanelToHoldTheSearchButton; //this is just the panel to hold the insatiate button.
    public InputField inputfield; //this is the input field where you will enter the search string to search.
    List<string> found0; // this is the first filter result from your first string word
    bool onetime; //so that the process stop after one search.
    bool twotime; //so that the process stop after one search.


    void Start () {
    onetime = true;
    twotime = true;
    }


    void Update () {
    if (inputfield.text == "") {
    //making this because if we go to blank inputfield the to the search result stay and if we are entering the world then to it is not proseed due to onetime=false;
    GameObject[] destroy = GameObject.FindGameObjectsWithTag ("Player");
    for (int i = 0; i < destroy.Length; i++) {
    Destroy (destroy );
    }
    onetime = true;
    }

    if (inputfield.caretPosition == 1) {
    //destroying the gameobject as it created multiple
    if (twotime == false) {
    GameObject[] destroy = GameObject.FindGameObjectsWithTag ("Player");
    for (int i = 0; i < destroy.Length; i++) {
    Destroy (destroy );
    }//for loop

    }//if loop

    found0=Name.FindAll(p=>p.StartsWith(inputfield.text));
    if (onetime == true) {
    for (int i = 0; i <= found0.Count - 1; i++) {
    GameObject btn = Instantiate (Resources.Load ("PanelButton/SearchResultButton"))as GameObject;
    btn.transform.SetParent (PanelToHoldTheSearchButton.transform, false);
    btn.name = found0 ;
    //btn.GetComponent<Button> ().onClick.AddListener (stinputfield.swaptext);
    btn.GetComponentInChildren<Text> ().text = found0 ;
    onetime = false;
    twotime = true;

    }//for loop
    }//if loop
    }//if main loop

    if (inputfield.caretPosition == 2) {
    if (onetime == false) {
    GameObject[] destroy = GameObject.FindGameObjectsWithTag ("Player");
    for (int i = 0; i < destroy.Length; i++) {
    Destroy (destroy );
    }onetime = true;

    }
    string TwoWords = inputfield.text;
    string SecondWord = TwoWords.Substring (1, TwoWords.Length - (TwoWords.Length - 1));
    Debug.Log (SecondWord);
    if (twotime == true) {
    for (int i = 0; i < found0.Count; i++) {
    Debug.Log (found0 .Substring (1, found0 .Length - (found0 .Length - 1)));
    if (SecondWord == found0 .Substring (1, found0 .Length - (found0 .Length - 1))) {
    GameObject btn = Instantiate (Resources.Load ("PanelButton/SearchResultButton"))as GameObject;
    btn.transform.SetParent (PanelToHoldTheSearchButton.transform, false);
    btn.name = found0 ;
    //btn.GetComponent<Button> ().onClick.AddListener (stinputfield.swaptext);
    btn.GetComponentInChildren<Text> ().text = found0 ;
    twotime = false;
    }// if loop

    }// for loop
    }//if loop
    }// main start if loop


    }// upadate loop

    }// main loop
     
    Last edited: May 22, 2017
  6. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
  7. MaxEden

    MaxEden

    Joined:
    Jun 22, 2013
    Posts:
    82
  8. MBaadsgaard

    MBaadsgaard

    Joined:
    Mar 19, 2013
    Posts:
    39
    Thank you all!
    Very useful!
     
  9. dtoceaneering

    dtoceaneering

    Joined:
    Sep 12, 2018
    Posts:
    10
    Hi,

    Kindly give step for "PanelButton/SearchResultButton" to assign on the script.
     
  10. atulpateltmspl

    atulpateltmspl

    Joined:
    Feb 3, 2020
    Posts:
    7