Search Unity

Question Drop Down... I jsut want selected value, not onvalue changed

Discussion in 'UGUI & TextMesh Pro' started by Ne0mega, May 28, 2021.

  1. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    I have been using the OnValueChanged call for my dynamically populated dropdowns for a while now, but what if my player messed up, and wants to keep the same thing? If there is only one value in the dropdown, buggy time, the dropdown window never changes, because the value cant change.

    I've looked through about five tutorials now, and they all teach using OnValueChanged... surely there is a different way.....




    EDIT: to clarify the question, is there more of an "OnSelected" way to do this, where the original value from the dropdown can be selected? Surely there is.
     
    Last edited: May 28, 2021
  2. Why would you want to have event if the value doesn't change? What is your use case? I'm pretty sure you're using it wrong if you want ValueChanged event if the value hasn't been changed. :D
     
  3. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    like I said. It is a dynamically populated list. If there is only one value, then the player gets stuck. Or the player might click on the dropdown wanting to change "outfits" (this is for a character customizer, where you can swap around custom outfits) and then change their mind, now they have to select a different outfit... and that might mean losing the "work" on their current outfit. Or they may click on the dropdown to see what the options are, and then decide they want to stay.

    Most dropdowns I use online do not require onvaluechanged, and will let you select the option you were already on. I tried ISelectable and OnSelect, but that did not seem to do the trick.
     
  4. This doesn't make any difference. When you populate your list, you know that there is only one value, so you already made the selection. Hopefully. If not, you're doing it wrong.
    The question is: why they get stuck. If you populate your list and there is only one selection, that's already selected, whatever you do, you do there. I still don't get it.
    I still don't understand what you're doing. If there is only one value, they don't select anything. The selection is already there.
    If there is only one value, what else do you want to select? Seriously, what is your use case? Not your players', yours. You're clearly doing something wrong.
     
  5. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    The whole system is set up so when you click the "Load Outfit" button, the dropdown activates and replaces the Name bar. (a button that displays the name, and that you can click on to change the name) However, if there is only one option, you can not choose any other option, therefore OnValueChanged is never activated, therefore the Dropdown never gets the message that the value was changed, therefore it never tells the name bar to reactivate or the dropdown itself to deactivate.

    What I am looking for is instead of OnValueChanged I want an "OnValueSelected" so if they select the same value they were on before, nothing happens, except the rest of the UI gets the message and readjusts accordingly. The dropdown disables itself, and the name bar reactivates.

    And I am the player. This is a mod/customization tool. And I have run into the problem.
     
  6. Then why don't you ask the developers to provide you something? It is a design problem, not Unity problem.
     
  7. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755


    This shows the first problem of using OnValueChanged, if there is only one option, as you can see, there is no way to get rid of the dropdown
     
  8. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    I asked if there was a way that is not OnValueChanged, more of an "OnSelected", if you are sure the answer is no, you could have said so, however, I think I will wait for somebody else to chime in before making a decision on re-doing the OnValueChanged pattern.

    I did not say it was a Unity problem. If you look at the flair it says "help wanted" not "bug". I just think that there must be a different way of approaching the dropdown, where the original value can be selected. There has to be.
     
    Last edited: May 28, 2021
  9. Based on your video, when you show "the only one selection" in your dropdown, you should have already made the update.
    Check out the example code in the manual at the onValueChanged:
    https://docs.unity3d.com/Packages/c...n.html#UnityEngine_UI_Dropdown_onValueChanged
    It is how you handle dropdowns. Notice the
    Code (CSharp):
    1. //Initialise the Text to say the first value of the Dropdown
    2. m_Text.text = "First Value : " + m_Dropdown.value;
    at the end of the
    Start
    method, where the dropdown gets filled with options. This is what you need to do in your case too. Obviously instead of putting the value into a variable you should call your "changeOutfit" or "selectOutrfit" or whatever method you have to update the corresponding visuals.
     
  10. RUSSCITY

    RUSSCITY

    Joined:
    Jan 20, 2023
    Posts:
    2
    It is a really old post, but the question still be actual.

    I think Ne0mega means he need to handle Dropdown as kind of "Select an action". This is the way I need it. I have a list with a Dropdown per item in list. Each Dropdown is dynamically created with a list of possible actions for the item. Sometimes there is only one action, e.g. "DELETE ENTRY". And if I select this action, then I want to get some trigger to remove this item from the list.

    Is there another correct way, maybe using Dropdown for this is wrong?
     
  11. RUSSCITY

    RUSSCITY

    Joined:
    Jan 20, 2023
    Posts:
    2
    Ok, I found it. You have to create, then select and delete an additional item in the dropdown. In case someone search for it:


    Code (CSharp):
    1.  
    2.     TMP_Dropdown mDropDown = actionsDropdown.GetComponent<TMP_Dropdown>();
    3.     List<string> mDropOptions;
    4.     mDropDown.ClearOptions();
    5.  
    6.     if (_isFriend) {
    7.         mDropOptions = new List<string> { "- REMOVE FROM FRIENDS", "THIS WILL BE REMOVED" };      
    8.     } else {
    9.         mDropOptions = new List<string> { "+ ADD TO FRIENDS", "THIS WILL BE REMOVED" };      
    10.     }
    11.  
    12.     mDropDown.AddOptions(mDropOptions);
    13.     mDropDown.SetValueWithoutNotify(1);
    14.     mDropDown.options.RemoveAt(1);
     
  12. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Add an EventTrigger to the Object, and a Submit-Event on top of that.
    Then listen for that event.
     
  13. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    755
    This is what i eventually did too. I created an extra option that said "choose your class" as value zero and then set the dropdown current value to zero, so it is at the top of the options, and if "chosen" it does not count as an "onValueChanged"
     
  14. silvervidia

    silvervidia

    Joined:
    Aug 16, 2022
    Posts:
    1
    Just change where the event is called.

    You'll see in the child object that there's "Template" and then "Item."
    There, you'll add an "Event Trigger" component with the event type "Pointer Click."

    then remove your callback method from the dropdown for "OnValueChanged".
    Modify your callback method to a no-parameter-method, and since you're populating the dropdown's options dynamically, surely you have a reference to it in the script. So, as you remove the int parameter, you access the dropdown's selected index by "dropdown.value" instead.

    example
    before:

    Code (CSharp):
    1. public void OnSelection(int value) => Debug.Log(value);
    after:

    Code (CSharp):
    1. public void OnSelection() => Debug.Log(dropdown.value);

    Finally, apply the modified callback method to the item's "Pointer Click" event.