Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by birns92 · Jun 25, 2016 at 06:45 AM · c#inputfielddropdownsearchsuggestions

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!!)

alt text

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 alt text

planet.jpg (164.6 kB)
suggestion.jpg (55.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image birns92 · Jun 25, 2016 at 06:25 PM 0
Share

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.

avatar image
1

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 ^^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Jaeger47 · Jul 24, 2020 at 10:10 AM

Watch This https://www.youtube.com/watch?v=ncu_GdhPNVQ

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Search in Dropdown options 2 Answers

Detecting if is currently entering text on any component. 0 Answers

How to search a LIST(not an array) for game objects with a specific tag? 1 Answer

Display PlayerPref in InputField 1 Answer

Control UI Input Field entirely with code? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges