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
Answer by Mindmapfreak · Jun 25, 2016 at 01:31 PM
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;
}
}
Thank you so much for your Post! I appreciate both the example and the links you provided to point me in the right direction. After I figure out how to do it, I'll provide my solution.
Answer by oscarlundberg · Jun 26, 2016 at 06:00 AM
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 ^^
Answer by Indigital · May 24, 2017 at 07:43 AM
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 [i]);
}
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 [i]);
}//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 [i];
//btn.GetComponent<Button> ().onClick.AddListener (stinputfield.swaptext);
btn.GetComponentInChildren<Text> ().text = found0 [i];
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 [i]);
}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 [i].Substring (1, found0 [i].Length - (found0 [i].Length - 1)));
if (SecondWord == found0 [i].Substring (1, found0 [i].Length - (found0 [i].Length - 1))) {
GameObject btn = Instantiate (Resources.Load ("PanelButton/SearchResultButton"))as GameObject;
btn.transform.SetParent (PanelToHoldTheSearchButton.transform, false);
btn.name = found0 [i];
//btn.GetComponent<Button> ().onClick.AddListener (stinputfield.swaptext);
btn.GetComponentInChildren<Text> ().text = found0 [i];
twotime = false;
}// if loop
}// for loop
}//if loop
}// main start if loop
}// upadate loop
}// main loop
Answer by atulpateltmspl · Jun 10, 2020 at 06:11 AM
Important Links.
1) Bitbucket (AutoCompleteComboBox) ==>> https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/wiki/Controls/AutoCompleteComboBox
2) AutoCompleteTextField free On Asset Store ==>> https://assetstore.unity.com/packages/tools/gui/autocomplete-textfield-112403?_ga=2.40981291.863498915.1591682260-886699922.1589433192
Answer by Jaeger47 · Jul 24, 2020 at 10:10 AM
Watch This https://www.youtube.com/watch?v=ncu_GdhPNVQ