Unity DropDown基本用法
创建一个Dropdown,再创建一个GameObject
GameObject挂上一个脚本testDropdown
using UnityEngine; using UnityEngine.UI; public class testDropdown : MonoBehaviour { public Dropdown dropdown; public void EventListening() { Debug.Log(dropdown.value); } }
将GameObject拖入Dropdown中,刚刚的代码作为监听脚本
此时控制台能根据选择项位置输出对应数字了:
补充:通过选项改变物体颜色:
public class testDropdown : MonoBehaviour { public Dropdown dropdown; public GameObject gameObject; public void EventListening() { switch(dropdown.value){ case 0: gameObject.GetComponent<MeshRenderer>().material.color = Color.white; break; case 1: gameObject.GetComponent<MeshRenderer>().material.color = Color.red; break; case 2: gameObject.GetComponent<MeshRenderer>().material.color = Color.green; break; case 3: gameObject.GetComponent<MeshRenderer>().material.color = Color.blue; break; default: break; } } }
将需要改变颜色的GameObject拖进来即可。