Search Unity

Search list in unity

Discussion in '2D' started by whiteshark01, Nov 11, 2019.

  1. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    Hello. I am building an app and I want to create a search list to find a button by name. The list should search the button like an airliner search list is looking for a destination. Below it is an exemple of how it should work.
    I forgot to mention the app is for both iOS and android devices.
    Captură.PNG

    Thank you.
     
  2. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    And what you tried?
     
  4. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    I don t know what to try.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,413
    are the values (results) already inside the app, or they come from some other source?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Hi @whiteshark01.
    I have read your PM.
    But please write it on the forum. I don't want respond privately per individual case, while others can contribute as well, and thread may be useful to others too in future.

    Btw array search is just iteration through.
    Or you can use dictionary, with relevant key, and corresponding value.
     
  7. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    Yes, they are inside the app. I will update this post with the PM I send to @Antypodish

    The purpose of my project is to create a free travel guide. Right now, I can choose the destination in two ways. The first one is from the main menu which is composed from a panel holder(game object) and I can swipe between panels (each panel has a button which change the scene to another destination).
    Also, in the main scene I do have a Destination Panel which is holding more buttons, everyone changing the scene to the desire destination as mentioned earlier. In this panel you can scroll down to see more destinations.
    The reason I want to create a search list is that it makes simplier to find the desire destination than it is right now.
    This list should contain buttons and when typing the recomanded buttons should show up. Each button should change the scene but I already know how to do that. I ve search on youtube but I didn t find anything relevant for what I am trying to do.
    Thank you for your help.
     
    Antypodish likes this.
  8. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    Done. I am searching to learn about dictionaries right now.
     
    Last edited: Nov 12, 2019
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    In fact dictionary may not be the best option in this case, as I realized, you want to search also partial string.
    Simple for loop of available options in arrays and checking name string, if contains searched string should do the job.

    Pseudo code.
    Code (CSharp):
    1. String [] arrayOfCityCounty = new String [n] ;
    2. // Here populate with data ....
    3. String sarchedString = "pa" ;
    4.  
    5. for ( int i = 0; i < arrayOfCityCounty.Length; i ++ )
    6. {
    7.     String cityCountyFromArray = arrayOfCityCounty [i] ;
    8.     cityCountyFromArray.Contains ( searchedString ) ;
    9. }
     
    MisterSkitz likes this.
  10. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Array : MonoBehaviour
    7. {
    8.     public string[] arrayOfCityCountry = {"Rome, Italy", "Turin, Italy", "Paris, France"};
    9.  
    10.     string searchedString = "pa";
    11.  
    12.     for (int i = 0; i < arrayOfCityCounty.Length; i ++ )
    13. {
    14.     String cityCountyFromArray = arrayOfCityCounty [I];
    15.     cityCountyFromArray.Contains(searchedString ) ;
    16. }
    17.  
    18.  
    19. }[/QUOTE][/I]
    If it should look like this I get 24 errors. I was thinking I should declare the array as game object like this.
    Code (CSharp):
    1. public class Array : MonoBehaviour
    2. {
    3.     public GameObject[] myObjects;
    4.  
    5.     void Start()
    6.     {
    7.      
    8.     }
    9.  
    10.  
    11. }
    This way I can attach the buttons to the GameObject with the script. When I have to create a script which can search for the buttons.

    Also I think I need a variable in which the user can type the text.
     
    Last edited: Nov 12, 2019
  11. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    You need put for loop inside some method.
    You can not just write directly inside class.
     
  12. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    I didn t find any solution. It still doesen t working.
     
  13. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    You should visit Getting Started forum section.
     
    MisterSkitz likes this.
  14. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well you are simulating a search bar search "Pa". That means you need a foreach loop to read each element in the array/list. Then you need a for loop inside that with the ability to read the first letter of each character of the element. Then do a check IF char1 == searchedStringChar1

    If they match, continue to search the second char. Discard the element if not a match. This really isn't easy but it's really not that hard either.
     
  15. whiteshark01

    whiteshark01

    Joined:
    Mar 10, 2019
    Posts:
    14
    Ok. Thank you. I will try.
     
    MisterSkitz likes this.
  16. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    If you run into a problem trying this, post your code and I'll help you get it working.