New dropdown menu sample

Hi, in a new update unity added dropdown menu, but how is this work ? i looked at reference but i can’t understand from it, i need example or tutorial, (1)i have a game and i have in first scene number and in second scene i will choose number from dropdown menu and i press button play and first scene load and the number change to my which i choosed(/1), that was example i have something similar but more sophisticated. I understand the c# from examples and script, so if you put here some example script for that i wrote(1), i will be happy, but if you put here some tutorial i will be too happy, but the expamples are better, so thanks for the help.

So, there are several ways to approach this.

  • using PlayerPrefs to store the number
  • using a static variable to store the number
  • use a seperate gameObject and call DontDestroyOnLoad(…)
  • use your own file (overkill by far, at least for one single number)

I also assume from the comments, that you want a number which is seen as text on the options and not the option’s index itself.

So here we go, I’ll demonstrate the first one (using PlayerPrefs) as the second is easy to figure out yourself, and the third and fourth are slighty an overkill:

Assign the dropdown and playButton in the inspector. Make sure you’ve got numbers (integer) on each option of the dropdown!!! By default, they’re “Option A”, “Option B”, “Option B”. You can change that in the inspector, for example “123”, “456”, “789”. If you want to work with string you only drop the parsing stuff and use SetString / GetString.

This script has to be in the first scene:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class Example : MonoBehaviour
{
    public Dropdown dropdown;
    public Button playButton;

    void Awake()
    {
        playButton.onClick.AddListener(new UnityAction(SaveNumberToPlayerPrefs));
    }

    private void SaveNumberToPlayerPrefs()
    {
        int numberToSave;
        int selectedIndex = dropdown.value;
        if (int.TryParse(dropdown.options[selectedIndex].text, out numberToSave))
        {
            PlayerPrefs.SetInt("SavedNumber", numberToSave);
            Application.LoadLevel(1);
        }
        else
        {
            Debug.LogError("Chosen option is no number: " + dropdown.options[selectedIndex].text);
            // or throw an exception or whatever
        }
    }
}

In the second scene, you need this script in order to load the number from the PlayerPrefs:

using UnityEngine;

public class Example2 : MonoBehaviour
{
    private int number;

    void Awake()
    {
        number = PlayerPrefs.GetInt("SavedNumber");
        Debug.Log("This is level 2, following number has been loaded: " + number);
    }
}

Attached this script and call this function in the
Inspector under DropDown Script - “On Value Change” .
If your options are in Integer you can convert the string to Integer

public class GetCaption : MonoBehaviour {
    	public Dropdown dropDown;
   
    	void OnValueChange(){ 		
    		string dd = dropDown.captionText.text; 		
    		print  (dd); 	
    	}
    }

You can also use Array and get the index of that array by using

public class GetIndex : MonoBehaviour {
	public Dropdown dropDown;
	public string[] items = { "Item1", "Item2", "Item3" };

	void OnValueChange(){ 		
		string index = dropDown.value; 		
		print  (items[index]); 	
	}
}

Another example:

    public void SetSomeString(Dropdown ddOptions)
    {
       sOptions= ddOptions.options[ddOptions.value].text;
    }