Dropdown(下拉式選單)是Unity UGUI中的一個常用元件,用於在使用者點選或選擇時顯示一個下拉式選單,提供多個選項供使用者選擇。
Dropdown元件由兩部分組成:一個可點選的按鈕和一個下拉式選單。當用戶點選按鈕時,下拉式選單會展開,顯示所有選項。使用者可以通過點選選項來進行選擇。
using UnityEngine;
using UnityEngine.UI;
public class DropdownExample : MonoBehaviour
{
public Dropdown dropdown;
void Start()
{
dropdown.ClearOptions();
dropdown.AddOptions(new List<string> { "Option 1", "Option 2", "Option 3" });
}
}
操作步驟:
注意事項:
using UnityEngine;
using UnityEngine.UI;
public class DropdownExample : MonoBehaviour
{
public Dropdown dropdown;
void Start()
{
dropdown.ClearOptions();
dropdown.AddOptions(new List<string> { "Option 1", "Option 2", "Option 3" });
}
public void OnDropdownValueChanged(int value)
{
Debug.Log("Selected option: " + dropdown.options[value].text);
}
}
操作步驟:
注意事項:
using UnityEngine;
using UnityEngine.UI;
public class DropdownExample : MonoBehaviour
{
public Dropdown dropdown;
public InputField inputField;
void Start()
{
dropdown.ClearOptions();
dropdown.AddOptions(new List<string> { "Option 1", "Option 2", "Option 3" });
}
public void AddOption()
{
string newOption = inputField.text;
dropdown.options.Add(new Dropdown.OptionData(newOption));
dropdown.RefreshShownValue();
}
}
操作步驟:
注意事項:
using UnityEngine;
using UnityEngine.UI;
public class DropdownExample : MonoBehaviour
{
public Dropdown dropdown;
void Start()
{
dropdown.ClearOptions();
dropdown.AddOptions(new List<string> { "Option 1", "Option 2", "Option 3" });
dropdown.SetValueWithoutNotify(1);
}
}
操作步驟:
注意事項:
using UnityEngine;
using UnityEngine.UI;
public class DropdownExample : MonoBehaviour
{
public Dropdown dropdown;
public GameObject customTemplate;
void Start()
{
dropdown.ClearOptions();
dropdown.AddOptions(new List<string> { "Option 1", "Option 2", "Option 3" });
dropdown.template = customTemplate.transform as RectTransform;
}
}
操作步驟:
注意事項: