Unity¤Ë´ØÏ¢¤¹¤ëµ­»ö¤Ç¤¹

£±¡¥Switchʸ¤È¤Ï


¡¡Switchʸ¤Ï¡¢¥×¥í¥°¥é¥ß¥ó¥°¸À¸ì¤Ë¤ª¤¤¤Æ¿´ô¤Ë¤ï¤¿¤ë¾ò·ïʬ´ô¤ò¼Â¸½¤¹¤ë¤¿¤á¤ÎÀ©¸æʸ¤Î°ì¤Ä¤Ç¤¹¡£
Æäˡ¢¤¢¤ëÊÑ¿ô¤ÎÃͤˤè¤Ã¤Æ¡¢°Û¤Ê¤ë½èÍý¤ò¹Ô¤¤¤¿¤¤¾ì¹ç¤ËÊØÍø¤Ç¤¹¡£

¡¡if-elseʸ¤Ç¤âƱ¤¸¤è¤¦¤Ê¤³¤È¤Ï²Äǽ¤Ç¤¹¤¬¡¢¾ò·ï¤¬Â¿¤¯¤Ê¤ë¤È²ÄÆÉÀ­¤¬²¼¤¬¤ë¤¿¤á¡¢¤½¤Î¤è¤¦¤Ê¾ì¹ç¤Ë¤ÏSwitchʸ¤ò»ÈÍѤ¹¤ë¤È¸ú²ÌŪ¤Ç¤¹¡£

¡¡¿§¡¹¤Êµ­Ë¡¤òÃΤäƤª¤­¡¢»È¤¤¤³¤Ê¤¹¥¹¥­¥ë¤ò¿È¤Ë¤Ä¤±¤Æ¤¤¤¯¤³¤È¤Ç¡¢¥×¥í¥°¥é¥à¤Î¼ÂÁõÎϤ¬¸þ¾å¤·¤Þ¤¹¡£


£²¡¥Switchʸ¤Î´ðËÜŪ¤Ê½ñ¤­Êý


¡¡Switchʸ¤Î´ðËÜŪ¤Ê½ñ¤­Êý¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¹¡£


switch (ÊÑ¿ô) {
    case ÃÍ1:
        // ÃÍ1¤Î»þ¤Î½èÍý
        break;
    case ÃÍ2:
        // ÃÍ2¤Î»þ¤Î½èÍý
        break;
    default:
        // ¾åµ­°Ê³°¤ÎÃͤλþ¤Î½èÍý
        break;
}

¡¡³Æ¥­¡¼¥ï¡¼¥É¤ÎÀâÌÀ¤Ç¤¹¡£

¡¡switch¡§switchʸ¤ò»Ï¤á¤Þ¤¹¡£¤³¤ì¤Ë³¤±¤Æ³ç¸Ì()Æâ¤Ë¡¢Ê¬´ô¤Î¸µ¤È¤Ê¤ëÊÑ¿ô¤ò½ñ¤­¤Þ¤¹¡£

¡¡case¡§ÊÑ¿ô¤¬ÆÃÄê¤ÎÃͤò»ý¤Ä¾ì¹ç¤Î½èÍý¤òµ­½Ò¤·¤Þ¤¹¡£¤³¤ì¤Ë³¤±¤Æ¥³¥í¥ó:¤ò½ñ¤­¡¢¤½¤Î¸å¤Ë³ºÅö¤¹¤ëÃͤ¬ÊÑ¿ô¤Ë¸ºß¤¹¤ë¾ì¹ç¤Î½èÍý¤òµ­½Ò¤·¤Þ¤¹¡£

¡¡break¡§¤½¤ì¤¾¤ì¤Îcase¥Ö¥í¥Ã¥¯¤ÎºÇ¸å¤Ë¤Ïbreak¤ò½ñ¤­¤Þ¤¹¡£¤³¤ì¤Ï¡¢¤½¤Î¥Ö¥í¥Ã¥¯¤Î½èÍý¤¬½ª¤ï¤Ã¤¿¤éswitchʸ¤«¤é½Ð¤ë¤¿¤á¤Î¥­¡¼¥ï¡¼¥É¤Ç¤¹¡£¥á¥½¥Ã¥É¼«ÂΤò½ªÎ»¤·¤¿¤¤¾ì¹ç¤Ë¤Ï return ¤Ç¤âÈ´¤±½Ð¤»¤Þ¤¹¡£

¡¡default¡§ÊÑ¿ô¤¬¤É¤Îcase¤Ë¤âÅö¤Æ¤Ï¤Þ¤é¤Ê¤¤¾ì¹ç¤Î½èÍý¤òµ­½Ò¤·¤Þ¤¹¡£¤³¤ì¤Ï¥ª¥×¥·¥ç¥ó¤Ç¡¢É¬¤º¤·¤âµ­½Ò¤¹¤ëɬÍפϤ¢¤ê¤Þ¤»¤ó¡£



¡ã¥µ¥ó¥×¥ë¥³¡¼¥É¡ä
string menu = "¥Ï¥ó¥Ð¡¼¥¬¡¼";

switch (menu) {

    case "¥Ï¥ó¥Ð¡¼¥¬¡¼":
        // ¥Ï¥ó¥Ð¡¼¥¬¡¼¤òºî¤ë½èÍý
        break;

    case "¥Õ¥ì¥ó¥Á¥Õ¥é¥¤":
        // ¥Õ¥ì¥ó¥Á¥Õ¥é¥¤¤òºî¤ë½èÍý
        break;

    case "¥·¥§¥¤¥¯":
        // ¥·¥§¥¤¥¯¤òºî¤ë½èÍý
        break;

    default:
        // ¤½¤ì°Ê³°¤Î¥á¥Ë¥å¡¼¤Î¾ì¹ç¤Î½èÍý
        break;
}

¡¡case ¤È break ¤Î´Ö¤Î²þ¹Ô¤ÏÉÔÍפǤ¹¡£

¡¡¤¿¤À¤·¡¢²ÄÆÉÀ­¤ò¹â¤á¤ë¤¿¤á¤Ë¤â¡¢ºÇ½é¤ÎÆâ¤Ï¾åµ­¤Î¤è¤¦¤Ë²þ¹Ô¤·¤Æ¤ª¤¤¤¿Êý¤¬Æɤߤ䤹¤¤¤Ç¤·¤ç¤¦¡£
¡¡

£³¡¥Switchʸ¤ÈEnum¤ÎÁêÀ­


¡¡Enum¡ÊÎóµó·¿¡Ë¤È¤Ï¡¢Ê£¿ô¤ÎÄê¿ô¤ò°ì¤Ä¤Ë¤Þ¤È¤á¤Æ̾Á°¤ò¤Ä¤±¤Æ´ÉÍý¤¹¤ë¤¿¤á¤Î·¿¤Ç¤¹¡£
Switchʸ¤ÈEnum¤ÏÈó¾ï¤ËÁêÀ­¤¬Îɤ¯¡¢Enum¤ÎÃͤ˱þ¤¸¤Æ°Û¤Ê¤ë½èÍý¤ò¹Ô¤¤¤¿¤¤¾ì¹ç¤Ë¤è¤¯»ÈÍѤµ¤ì¤Þ¤¹¡£

¡¡Î㤨¤Ð¡¢¥²¡¼¥à¤Î¥­¥ã¥é¥¯¥¿¡¼¤Î¾õÂÖ¡ÊΩ¤Ã¤Æ¤¤¤ë¡¢Áö¤Ã¤Æ¤¤¤ë¡¢¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë¤Ê¤É¡Ë¤òEnum¤Ç´ÉÍý¤·¡¢
¤½¤Î¾õÂ֤˱þ¤¸¤Æ°Û¤Ê¤ë¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸¤¹¤ë¡¢¤È¤¤¤Ã¤¿¤³¤È¤¬²Äǽ¤Ç¤¹¡£


public enum CharacterState {
    Standing,
    Running,
    Jumping
}

CharacterState state = CharacterState.Running;

switch (state) {
    case CharacterState.Standing:
        // Ω¤Ã¤Æ¤¤¤ë»þ¤Î½èÍý
        break;
    case CharacterState.Running:
        // Áö¤Ã¤Æ¤¤¤ë»þ¤Î½èÍý
        break;
    case CharacterState.Jumping:
        // ¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë»þ¤Î½èÍý
        break;
    default:
        break;
}

¡¡¤³¤Î¤è¤¦¤Ë¡¢Switchʸ¤ÈEnum¤òÁȤ߹ç¤ï¤»¤ë¤³¤È¤Ç¥³¡¼¥É¤¬Æɤߤ䤹¤¯¤Ê¤ê¡¢¾õÂ֤˱þ¤¸¤¿½èÍý¤ò´Êñ¤Ë´ÉÍý¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£


£´¡¥ Switchʸ¤È¼°ÌÚ


¡¡C# 8.0¤«¤é¡¢Switchʸ¤ÏÆÃÄê¤Î·¿¤ä¾ò·ï¤Ë°ìÃפ¹¤ë¤«¤É¤¦¤«¤òȽÄê¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¥ó¥°¤È¸Æ¤Ð¤ì¤ëµ¡Ç½¤Î°ì´Ä¤Ç¤¢¤ê¡¢¤³¤ì¤Ë¤è¤ê¡¢Switchʸ¤Ï¤è¤ê½ÀÆð¤Êʬ´ô½èÍý¤òµ­½Ò¤¹¤ë¤³¤È¤¬²Äǽ¤Ë¤Ê¤ê¤Þ¤·¤¿¡£


object obj = "Hello, World!";

switch (obj)
{
    case string s:
        Console.WriteLine($"It's a string with length {s.Length}");
        break;
    case int i:
        Console.WriteLine($"It's an integer with value {i}");
        break;
    default:
        Console.WriteLine("It's something else.");
        break;
}

¡¡¤³¤ÎÎã¤Ç¤Ï¡¢obj¤¬string·¿¤«int·¿¤«¡¢¤½¤ì°Ê³°¤Î·¿¤«¤òȽÄꤷ¤Æ¤¤¤Þ¤¹¡£
case string s:¤ÎÉôʬ¤Ç¤Ï¡¢obj¤¬string·¿¤Ç¤¢¤ì¤Ð¡¢¤½¤ÎÃͤòs¤È¤¤¤¦ÊÑ¿ô¤ËÂåÆþ¤·¡¢¤½¤Î¸å¤Î½èÍý¤Ç»ÈÍѤǤ­¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£


£µ¡¥Switchʸ¤ò»È¤Ã¤¿¥á¥½¥Ã¥ÉÆâÉô¤Î½èÍý


¡¡¤Ç¤Ï¡¢¶ñÂÎŪ¤Ê¥á¥½¥Ã¥É¤ÎÃæ¤ÇSwitchʸ¤ÈEnum¤ò»È¤Ã¤¿Îã¤ò¸«¤Æ¤ß¤Þ¤·¤ç¤¦¡£
°Ê²¼¤Ï¡¢¥­¥ã¥é¥¯¥¿¡¼¤Î¾õÂ֤˱þ¤¸¤Æ°Û¤Ê¤ë¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸¤¹¤ë¥á¥½¥Ã¥É¤ÎÎã¤Ç¤¹¡£


public enum CharacterState {
    Standing,
    Running,
    Jumping
}

public void PlayAnimation(CharacterState state) {
    switch (state) {
        case CharacterState.Standing:
            // Ω¤Ã¤Æ¤¤¤ë»þ¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸
            Console.WriteLine("Standing animation");
            break;
        case CharacterState.Running:
            // Áö¤Ã¤Æ¤¤¤ë»þ¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸
            Console.WriteLine("Running animation");
            break;
        case CharacterState.Jumping:
            // ¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë»þ¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸
            Console.WriteLine("Jumping animation");
            break;
        default:
            // ͽ´ü¤·¤Ê¤¤¾õÂÖ
            Console.WriteLine("Invalid state");
            break;
    }
}

¡¡¤³¤Î¥á¥½¥Ã¥ÉPlayAnimation¤Ç¤Ï¡¢°ú¿ô¤È¤·¤ÆCharacterState·¿¤Îstate¤ò¼õ¤±¼è¤ê¡¢¤½¤Îstate¤ÎÃͤ˱þ¤¸¤ÆSwitchʸ¤Çʬ´ô¤·¤Æ¤¤¤Þ¤¹¡£
¤½¤ì¤¾¤ì¤Îcase¤Ç¡¢Âбþ¤¹¤ë¾õÂ֤Υ¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸¤¹¤ë½èÍý¡Ê¤³¤³¤Ç¤ÏConsole.WriteLine¤ÇÂåÍѡˤòµ­½Ò¤·¤Þ¤¹¡£


£¶¡¥Ìá¤êÃͤò»ý¤ÄSwitchʸ¡§¼°·Á¼°¤ÎSwitchʸ


¡¡C# 8.0°Ê¹ß¤Ç¤Ï¡¢¤è¤ê´Ê·é¤ËSwitchʸ¤ò½ñ¤¯¤³¤È¤¬²Äǽ¤È¤Ê¤ê¤Þ¤·¤¿¡£
Æäˡ¢Switch¼°¤È¸Æ¤Ð¤ì¤ë·Á¼°¤Ç¤Ï¡¢Ìá¤êÃͤò»ý¤ÄSwitchʸ¤ò°ì¹Ô¤Ç½ñ¤¯¤³¤È¤¬²Äǽ¤Ç¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢¥³¡¼¥É¤¬¤è¤ê¥·¥ó¥×¥ë¤Ë¤Ê¤ê¡¢Æɤߤ䤹¤µ¤¬¸þ¾å¤·¤Þ¤¹¡£

¡¡°Ê²¼¤Ë¡¢Switch¼°¤ò»È¤Ã¤¿Îã¤ò¼¨¤·¤Þ¤¹¡£


public enum Size {
    Small,
    Medium,
    Large
}

public int GetSizeValue(Size size) {
    return size switch {
        Size.Small => 1,
        Size.Medium => 2,
        Size.Large => 3,
        _ => 0 // ¤½¤ì°Ê³°¤ÎÃÍ
    };
}

¡¡¤³¤ÎGetSizeValue¥á¥½¥Ã¥É¤Ç¤Ï¡¢°ú¿ô¤È¤·¤ÆSize·¿¤Îsize¤ò¼õ¤±¼è¤ê¡¢¤½¤ì¤ËÂбþ¤¹¤ëÀ°¿ôÃͤòÌá¤êÃͤȤ·¤ÆÊÖ¤·¤Æ¤¤¤Þ¤¹¡£
Switch¼°¤Ç¤Ï¡¢returnʸ¤Î¸å¤ËÊÑ¿ô switch¤È½ñ¤­¡¢¤½¤Î¸å¤Ë{ }Æâ¤Ç³Æcase¤òµ­½Ò¤·¤Þ¤¹¡£³Æcase¤Ç¤Ï¡¢ÃÍ => ·ë²Ì¤Î·Á¼°¤ò¼è¤ê¤Þ¤¹¡£

¡¡¤Þ¤¿¡¢_¤Ï¤¹¤Ù¤Æ¤ÎÃͤòɽ¤¹¥ï¥¤¥ë¥É¥«¡¼¥É¤Ç¡¢¤³¤³¤Ç¤Ï¤½¤ì°Ê³°¤ÎÃͤËÂФ¹¤ë¥Ç¥Õ¥©¥ë¥È¤Î½èÍý¤òµ­½Ò¤·¤Þ¤¹¡£


£·¡¥¤Þ¤È¤á


¡¡°Ê¾å¤¬¡¢Switchʸ¤Î´ðËÜŪ¤Ê»È¤¤Êý¤È¤½¤Î±þÍÑÎã¤Ç¤¹¡£

¡¡¾ò·ïʬ´ô¤¬Â¿´ô¤Ë¤ï¤¿¤ë¾ì¹ç¤ä¡¢Enum¤ÈÁȤ߹ç¤ï¤»¤Æ»ÈÍѤ¹¤ë¾ì¹ç¤Ê¤É¤Ë¤Ï¡¢Switchʸ¤¬Èó¾ï¤ËÍ­¸ú¤Ç¤¹¡£
¤Þ¤¿¡¢Switch¼°¤ò»È¤¦¤³¤È¤Ç¡¢¤è¤ê´Ê·é¤Ê¥³¡¼¥É¤ò½ñ¤¯¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

Menu


´ðÁÃ

µ»½Ñ/Ãμ±(¼ÂÁõÎã)

3D¥¢¥¯¥·¥ç¥ó¥²¡¼¥à

2D¤ª¤Ï¤¸¤­¥²¡¼¥à(ȯŸÊÔ)

2D¶¯À©²£¥¹¥¯¥í¡¼¥ë¥¢¥¯¥·¥ç¥ó(ȯŸÊÔ)

2D¥¿¥Ã¥×¥·¥å¡¼¥Æ¥£¥ó¥°(³ÈÄ¥ÊÔ)

¥ì¡¼¥¹¥²¡¼¥à(È´¿è)

2DÊüÃÖ¥²¡¼¥à(ȯŸÊÔ)

3D¥ì¡¼¥ë¥¬¥ó¥·¥å¡¼¥Æ¥£¥ó¥°(±þÍÑÊÔ)

3Dæ½Ð¥²¡¼¥à(È´¿è)

2D¥ê¥¢¥ë¥¿¥¤¥à¥¹¥È¥é¥Æ¥¸¡¼

3D¥¿¥Ã¥×¥¢¥¯¥·¥ç¥ó(NavMeshAgent »ÈÍÑ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥«¥¨¥ë¤Î°Ù¤Ë¡Á¡¢¥Ü¥³¥¹¥«¥¦¥©¡¼¥ºÉ÷)

VideoPlayer ¥¤¥Ù¥ó¥ÈϢư¤Î¼ÂÁõÎã

VideoPlayer ¥ê¥¹¥ÈÆ⤫¤é¥à¡¼¥Ó¡¼ºÆÀ¸¤Î¼ÂÁõÎã(ȯŸ)

AR ²èÁüÉÕ¤­¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¤Î¼ÂÁõÎã

AR ¥ê¥¹¥ÈÆ⤫¤éÀ¸À®¤Î¼ÂÁõÎã(ȯŸ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥µ¥Ð¥¤¥Ð¡¼É÷)

private



¤³¤Î¥µ¥¤¥ÈÆâ¤ÎºîÉʤϥæ¥Ë¥Æ¥£¤Á¤ã¤ó¥é¥¤¥»¥ó¥¹¾ò¹à¤Î¸µ¤ËÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹¡£

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹