0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】switch文の判定を文字列で行う

Last updated at Posted at 2020-06-19

環境

Unity 2019.3.7f1

はじめに

switch文は数値だけでなく文字列でも判定できるのでメモ。

具体例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    string a;//string型の変数aを宣言
    
    void Start()
    {
        a="算数";//変数aに算数を代入

        switch (a)
        {
            case "算数": //aが算数の時に実行する
                Debug.Log("電卓を使います"); //コンソールに表示
                break;  //switch文から抜ける

            case "理科": //aが社会の時に実行する
                Debug.Log("薬品を使います"); //コンソールに表示
                break; //switch文から抜ける

            default:    //aが上記以外の場合に実行する
                Debug.Log("そんな教科はありません"); //コンソールに表示
                break; //switch文から抜ける
        }
    }
}

実行結果

image.png

おわりに

数値判定の時と同じ感覚でできちゃいますね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?