Unity中Switch的用法

教程地址(观看视频需FQ):

https://learn.unity.com/project/c-survival-guide-switch-statements?language=en&courseId=5cf06bd1edbc2a58d7fc3209

功能说明:如果有多种情况选择,可用switch进行切换

public class SwitchStatement : MonoBehaviour
{
    void Start()
    {
        currentDifficulty = Random.Range(0, 3);
    }


    void Update()
    {

        switch(currentDifficulty)
        {
            case 0:
                Debug.Log("You selected easy.");
                break;
            case 1:
                Debug.Log("You selected medium.");
                break;
            case 2:
                Debug.Log("You selected hard.");
                break;
            default:
                Debug.Log("Invalid Level Selected!");
                break;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            currentDifficulty = Random.Range(0, 4);
        }
    }

你可能感兴趣的:(Unity中Switch的用法)