Unity 中的颜色下拉列表

问题描述 投票:0回答:1

我正在尝试制作一个下拉菜单来统一更改我的播放器的颜色,但它不起作用。这是我的代码:

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class colorSelect : MonoBehaviour
{
    public Dropdown ColorDropdown;
    public static float speed;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void ChooseColor()
    {
        switch (ColorDropdown.value)
        {
            case 1:
                GetComponent<Renderer>().material.color = Color.cyan;
                break;
            case 2:
                GetComponent<Renderer>().material.color = Color.red;
                break;
            case 3:
                GetComponent<Renderer>().material.color = Color.white;
                break;
            default: // speed if no item is selected. in this case, the player does not move
                speed = 0.0f;
                break;
        }

    }
}`

我尝试使用 switch 语句,然后将脚本附加到播放器,然后将播放器附加到下拉菜单,但它不起作用。

c# unity3d
1个回答
0
投票

访问下拉列表值的通常方法是通过 OnValueChanged 事件。

这可能有用:

https://docs.unity3d.com/2023.1/Documentation/Manual/script-Dropdown.html

© www.soinside.com 2019 - 2024. All rights reserved.