Autocomplete Search (Search Suggestion)

I created a simple Search bar in my example, so that I can search for gameobjects during gametime and destroy them. For the sake of simplicity and to ask this question as efficiently as I can, I created an Example with the Planets of the solar system and each planet has a duplicate. (Obviously this is not built to scale or even accuracy!!)

Each Planet has this script attached to it

using UnityEngine;
using System.Collections;

public class Planet : MonoBehaviour
{
    public void OnMouseDown()
    {
        Destroy(gameObject);
    }
}

And the script for the search bar is:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Search_Planet : MonoBehaviour
{
    public InputField Name;
    public void Search_For_Planet()
    {
        if (GameObject.Find(Name.text) != null)
        {
            GameObject Search = GameObject.Find(Name.text);      
            Search.GetComponent<Planet>().OnMouseDown();         
        }
        else
        {
            Debug.Log("I am sorry, that does not exist");
        }
    }
}

My question in further detail is: How can I create a drop down suggestion field under the current input field. So as soon As I type “Ear” in the input field I will get a suggestion of Both “Earth” and “Earth (1)”, “Jup” would yield suggestions of “Jupiter” and "Jupiter (2)… so on and so forth. So in practice it would look something similar to this

Maybe if you

foreach (GameObject planet in listOfPlanets) 
{
   if (planet.name.StartsWith(inputString)) 
   {
      DisplayPlanet(planet);
   }

}

This is more of a design suggestion rather than the actual code, but if you don’t understand it you need:

A list of all planets (listOfPlanets)
A method to display the planets in the drop down (DisplayPlanet)
A variable to store the user input (inputString)

The method will call the moment you type a letter, but you can add a condition to the if-statement like

&& inputString.length>2

Maybe this helps, maybe not ^^

This problem is not really specific to Unity (or not even to C#), so you can find general information about autocomplete-algorithms here, here and here . If your dataset is rather small you could get away with using a brute force approach like this:

public class Autocomplete : MonoBehaviour {
    List<GameObject> planets;
	// Use this for initialization
	void Start () {
        planets = new List<GameObject>();
        planets.Add(new GameObject("Earth"));
        planets.Add(new GameObject("Earth2"));
        planets.Add(new GameObject("Jupiter"));
        planets.Add(new GameObject("Saturn"));
        planets.Add(new GameObject("Mercury"));
        planets.Add(new GameObject("Mars"));

        foreach (GameObject possiblePlanet in GetPossibleMatches("M"))
        {
            Debug.Log(possiblePlanet.name);
        }
    }

    List<GameObject> GetPossibleMatches(string typedCharacters)
    {
        List<GameObject> possibleMatches = new List<GameObject>();
        int typedLength = typedCharacters.Length;
        foreach(GameObject curPlanet in planets)
        {
            if (curPlanet.name.Substring(0, typedLength).Equals(typedCharacters))
            {
                possibleMatches.Add(curPlanet);
            }
        }
        return possibleMatches;
    }

}

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;
public InputField inputfield;
List<string> found0;
bool onetime;
bool twotime;

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

void Update () {
	if (inputfield.text == "") {
		//making this because if we go to blank inputfield the to the button stay and if we re enter the world then to it 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 ().onClick.AddListener (stinputfield.swaptext);
btn.GetComponentInChildren ().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 ().onClick.AddListener (stinputfield.swaptext);
btn.GetComponentInChildren ().text = found0 ;
twotime = false;
}// if loop*_

* }// for loop*
* }//if loop*
* }// main start if loop*
* }// upadate loop*
* }// main loop*

Important Links.

  1. Bitbucket (AutoCompleteComboBox)
    ==>> UnityUIExtensions / Unity-UI-Extensions / wiki / Controls / AutoCompleteComboBox — Bitbucket

  2. AutoCompleteTextField free On Asset Store
    ==>> AutoComplete Popup | GUI Tools | Unity Asset Store

Watch This