• 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 laharl219 · Mar 15, 2016 at 02:50 PM · dropdowndynamicallyfillsqldatabase

Can i fill a dropdown with data from a database or a Json file?

I need to dynamically fill a dropdown at unity, i can get the information from a database or a Json file(I just know how to fill a dropdown manually at the dropdown's component). Is there a way to do it?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by matthewseaward · Mar 15, 2016 at 08:49 PM

I'm assuming you are using the new UI elements and canvas?

This is simple:

 public Dropdown myDropdown; // Make sure to assign this
 private List<string> data;
 
 void Start()
 {
      data = LoadDataFromDBOrJSON(); // Or whatever way you will read your data in
 
      myDropdown.options.Clear();
      foreach(string str in data)
      {
         myDropdown.options.Add(new Dropdown.OptionData(str));
     }
 }
Comment
Add comment · Show 1 · 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 tsondhi2006 · Dec 18, 2017 at 04:37 PM 0
Share

how to read the data from the mysql db??

avatar image
0

Answer by unity_eVEKXzm04KI-og · Jul 03, 2018 at 01:02 AM

The same question cómo leer los datos del mysql db ??,Lo mismo que pregunto tsondhi2006 cómo leer los datos del mysql db ??

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 tsondhi2006 · Jul 12, 2018 at 09:03 PM

Did it:-

1) Create a c# file

 void Func1()
     {        
         WWW www = new WWW ("127.0.0.1/somefolder/somefile.php");// give location of Php file
         while (!www.isDone) {
         }
         string itemsDataString = www.text;
         string[] wwwItems = itemsDataString.Split (';'); //the values in mysql db is picked in a single string and should have some marker so as to split
         DD.options.Clear ();
         foreach (string str in wwwItems) {
             DD.options.Add (new Dropdown.OptionData (str));
         }
 }

2) Create a php file which will pass the values from mysql to unity [somefile.php]:-

 <?php
     $conn = new mysqli("127.0.0.1", "root", "", "somedb");
 
     if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
     }
     $sql = "SELECT * FROM sometable";
     $result = $conn->query($sql);
     if ($result->num_rows > 0) {
         // output data of each row
             while($row = $result->fetch_assoc()) {
             echo ";".$row["somefield"];
         }
     }
     $conn->close();
 ?>
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

40 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 avatar image avatar image

Related Questions

How do i make a dropdown via script(not using the new UI) 1 Answer

Create a Dropdown Menu with values from Database 0 Answers

Filling array with gameObjects 2 Answers

What is the best way to evenly distribute objects to fill a curved space? 1 Answer

Question about variabl lists 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges