Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by polda602 · Sep 13, 2015 at 12:32 PM · unity 5uimenudropdownnumber

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Suddoha · Sep 13, 2015 at 01:31 PM 0
Share

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.

avatar image polda602 · Sep 13, 2015 at 01:52 PM 0
Share

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

avatar image Suddoha polda602 · Sep 13, 2015 at 02:07 PM 0
Share

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?

avatar image polda602 · Sep 13, 2015 at 02:57 PM 0
Share

No it isn't index, yes it's that i want to use.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image polda602 · Sep 13, 2015 at 06:34 PM 0
Share

Oh, thank you very much this really help me to understand not only dropdown, but other thing, so again thank you, big like.

avatar image ywaby · Sep 18, 2015 at 03:36 PM 0
Share

can i ask u a question? how to add a item to the dropdown use script? i can't find answer anywhere ? thanks.

avatar image Suddoha ywaby · Sep 18, 2015 at 04:06 PM 0
Share
 // 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

avatar image
1

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]);     
     }
 }







Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by SeveneduS · Dec 04, 2015 at 03:21 PM

Another example:

     public void SetSomeString(Dropdown ddOptions)
     {
        sOptions= ddOptions.options[ddOptions.value].text;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

38 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause menu works on all scenes but one... 1 Answer

New Unity Dropdown Menu to Change Game Resolution 1 Answer

Make a pop up panel asking if I am sure I want to buy a specific item 0 Answers

Button highlighted a pushable without touching it 0 Answers

How to use options in the Dropdown menu UI? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges