如何设置 ui 下拉列表以在 运行 游戏时显示第一项?
How can I set the ui dropdown to show the first item when running the game?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UiDropdown : MonoBehaviour
{
//Use these for adding options to the Dropdown List
//Dropdown.OptionData m_NewData, m_NewData2;
Dropdown.OptionData[] m_NewData;
//The list of messages for the Dropdown
List<Dropdown.OptionData> m_Messages = new List<Dropdown.OptionData>();
//This is the Dropdown
Dropdown m_Dropdown;
public GameObject[] gameobjects;
void Start()
{
m_NewData = new Dropdown.OptionData[gameobjects.Length];
//Fetch the Dropdown GameObject the script is attached to
m_Dropdown = GetComponent<Dropdown>();
//Clear the old options of the Dropdown menu
m_Dropdown.ClearOptions();
for (int i = 0; i < gameobjects.Length; i++)
{
m_NewData[i] = new Dropdown.OptionData();
m_NewData[i].text = gameobjects[i].name;
m_Messages.Add(m_NewData[i]);
}
//Take each entry in the message List
foreach (Dropdown.OptionData message in m_Messages)
{
//Add each entry to the Dropdown
m_Dropdown.options.Add(message);
}
}
}
脚本附加到下拉列表 ui 对象。
问题是它正在清除选项,然后 运行 游戏中没有 selected 项目,如果我尝试 select 第一个项目它不会 select 它我需要先 select 另一个项目然后我可以 select 第一个项目。
如果我删除清除选项行,它将 select 第一个项目,但也会将所有项目添加两次。
此屏幕截图是存在清除选项行时的屏幕截图:
如果我要删除清算选项行:
m_Dropdown.ClearOptions();
然后:
我想要做的是当 运行 游戏时,下拉列表中的第一项将被 select 编辑。现在它是或空的或整个项目添加了两次。
添加所有内容后调用RefreshShownValue
。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UiDropdown : MonoBehaviour
{
//Use these for adding options to the Dropdown List
//Dropdown.OptionData m_NewData, m_NewData2;
Dropdown.OptionData[] m_NewData;
//The list of messages for the Dropdown
List<Dropdown.OptionData> m_Messages = new List<Dropdown.OptionData>();
//This is the Dropdown
Dropdown m_Dropdown;
public GameObject[] gameobjects;
void Start()
{
m_NewData = new Dropdown.OptionData[gameobjects.Length];
//Fetch the Dropdown GameObject the script is attached to
m_Dropdown = GetComponent<Dropdown>();
//Clear the old options of the Dropdown menu
m_Dropdown.ClearOptions();
for (int i = 0; i < gameobjects.Length; i++)
{
m_NewData[i] = new Dropdown.OptionData();
m_NewData[i].text = gameobjects[i].name;
m_Messages.Add(m_NewData[i]);
}
//Take each entry in the message List
foreach (Dropdown.OptionData message in m_Messages)
{
//Add each entry to the Dropdown
m_Dropdown.options.Add(message);
}
}
}
脚本附加到下拉列表 ui 对象。
问题是它正在清除选项,然后 运行 游戏中没有 selected 项目,如果我尝试 select 第一个项目它不会 select 它我需要先 select 另一个项目然后我可以 select 第一个项目。
如果我删除清除选项行,它将 select 第一个项目,但也会将所有项目添加两次。
此屏幕截图是存在清除选项行时的屏幕截图:
如果我要删除清算选项行:
m_Dropdown.ClearOptions();
然后:
我想要做的是当 运行 游戏时,下拉列表中的第一项将被 select 编辑。现在它是或空的或整个项目添加了两次。
添加所有内容后调用RefreshShownValue
。