Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make filter with choose options

Discussion in 'Scripting' started by OkeyNik, Aug 3, 2020.

  1. OkeyNik

    OkeyNik

    Joined:
    Mar 14, 2017
    Posts:
    41
    Hello, i want to make an regular filter drop down picklist with options. But i dont have idea how to write it on C#. I kind of understand how to make it on UI.

    So as you can see on my mockup - i have filters with options and filtering objects.

    And just like in any filter functionality, only the Object should remain that matches the conditions / criteria. And if I delete the criteria, then other Objects should appear
     

    Attached Files:

  2. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I am currently not on my pc but I can write you some examples when I have access to it again. How ever what I can recommend you to do is to look at the current value of the dropdown and try to work with if/else or even add them together and then look for fitting criteria through switch/case.

    As I said I can write you an exsample as soon as I have access to my computer so no exsample right now.
     
  3. OkeyNik

    OkeyNik

    Joined:
    Mar 14, 2017
    Posts:
    41
    It would be nice, if you can help me, sir
     
  4. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Hi heres want I came up with:
    Code (CSharp):
    1. using UintyEngine.UI;
    2.  
    3. //For the dropdowns that make up the final option
    4. public class DropdownScript : MonoBehavior
    5. {
    6. //The Dropdown its attached to
    7. Dropdown dropdown;
    8. //To subtract the old value and add the new one to prevent the current Value of the button going off into infinty
    9. int oldValue;
    10. // List here all the option buttons you want to be affected
    11. public OptionScript[] affectedButtons;
    12.  
    13. void Start()
    14. {
    15.   //Get the dropdown to read its current value
    16.   dropdown = GetComponent<Dropdown>();
    17. }
    18.  
    19. void Update()
    20. {
    21.  
    22.  oldValue = dropdown.value;
    23.  
    24.  dropdown.onValueChanged.AddListener(delegate {DropdownValueChanged();});
    25.  
    26. }
    27.  
    28. void DropdownValueChanged()
    29. {
    30.  //Loop through all buttons that should get affected
    31.  for(int i = 0; i < affectedButtons.Length; i++)
    32.  {
    33.   affectedButtons.currValue -= oldValue;
    34.   affectedButtons.currValue += dropdown.value;
    35.  }
    36. }
    37.  
    38. }
    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. //For the buttons that contain the Option availavble
    4. public class OptionScript : MonoBehavior
    5. {
    6. public int currValue;
    7. public int[] valuesToChangeOption;
    8. //If you want to change the OnClick() function
    9. public Button thisButton;
    10.  
    11. void Start()
    12. {
    13.   //thisButton = GetComponent<Button>();
    14. }
    15.  
    16. void Update()
    17. {
    18.   switch(currValue)
    19.   {
    20.    case valuesToChangeOption[0]:
    21.     //subscribe to the onClick event
    22.     thisButton.onClick.AddListener(CustomButton_onClick);
    23.     void CustomButton_onClick()
    24.     {
    25.      //Do stuff...
    26.     }
    27.     break;
    28.  
    29.     case valuesToChangeOption[1]:
    30.      //subscribe to the onClick event
    31.      thisButton.onClick.AddListener(CustomButton_onClick);
    32.      void CustomButton_onClick()
    33.      {
    34.       //Do stuff...
    35.      }
    36.     break;
    37.  
    38.     //etc...
    39.   }
    40. }
    41. }
    This should do it I havn't tried it out in unity but this should give each button a value on which you can put in your code to want to do when given value(currValue) is true