Unity的C#编程教程_24_Else-If条件语句

  • 多重条件的判断
  • 假设选择不同难度等级的游戏
  • 选择之后弹出对应的难度提示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{

    public int easyLevel = 0;
    public int mediumLevel = 1;
    public int hardLevel = 2;
    // 3 个不同等级难度

    public int chooseLevel;
    // 用于存储选择的难度

    // Start is called before the first frame update
    void Start()
    {
        
        // 在 unity中选择一个难度
        if (chooseLevel == easyLevel) // 判断选择了简单模式
        {
            Debug.Log("Easy level is runing!");
            // 弹出提示:简单模式正在运行
        }
        else if (chooseLevel == mediumLevel) // 第2层判断
        {
            Debug.Log("Medium level is runing");
        }
        else if(chooseLevel == hardLevel) // 第3层判断
        {
            Debug.Log("Hard level is runing!");
        }
        else
        {
            Debug.Log("You choose wrong level!");
            // 选择错误弹出提示
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
Tags: