- Home /
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.
It's not really clear what you're asking for. You talk about 2 scenes, in the first one you've got a number and in the second, you want to choose another number from a dropdown menu and THEN you want to hit play?
That's confusing.
No, i think this. Game start and you are in first scene when you select your number in dropdown menu, then you hit play and it load second scene where will be your number which you choosed in first scene
Is the number the index of the level that you want to load or is it a number that you want to use in the second scene?
Answer by Suddoha · Sep 13, 2015 at 03:49 PM
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);
}
}
Oh, thank you very much this really help me to understand not only dropdown, but other thing, so again thank you, big like.
can i ask u a question? how to add a item to the dropdown use script? i can't find answer anywhere ? thanks.
// adds an empty option
myDropDown.options.Add(new Dropdown.OptionData());
// or with text
myDropDown.options.Add(new Dropdown.OptionData("New option"));
There are also overloads for sprite only, and text + sprite
Answer by vfxjex · Jun 24, 2016 at 04:13 AM
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]);
}
}
Answer by SeveneduS · Dec 04, 2015 at 03:21 PM
Another example:
public void SetSomeString(Dropdown ddOptions)
{
sOptions= ddOptions.options[ddOptions.value].text;
}