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?
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));
}
}
The same question
cómo leer los datos del mysql db ??,Lo mismo que pregunto tsondhi2006
cómo leer los datos del mysql db ??
Did it:-
-
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));
}
} -
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(); ?>