Search Unity

Resolved Replace If-Statements by Switch-Case Statement

Discussion in 'Scripting' started by DonPuno, May 1, 2021.

  1. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    Hello everyone,

    I built a little animation controller which plays 4 different animations depending on the conditions in the background of the game. It is working fine. However, I think the code below can be expressed in a more elegant way. Currently I solved it with a row of if-statements.

    I was thinking about a Switch-Case Statement, but I cannot figure out, how I could bring the bool-operators into play. Does anyone from you has an idea how to make this a more elegant code?





    Code (CSharp):
    1.     public void OnMouseOver()
    2.     {
    3.         if (!_isActive)
    4.         {
    5.             if (Input.GetMouseButton(0))
    6.             {
    7.                 _isActive = true;
    8.  
    9.                 if (_selectedColorisGreen && _isGreen)
    10.                 {
    11.                     _animator.SetTrigger("GreenEvent");
    12.                 }
    13.  
    14.                 if (!_selectedColorisGreen && !_isGreen)
    15.                 {
    16.                     _animator.SetTrigger("RedEvent");
    17.                 }
    18.  
    19.                 if (_selectedColorisGreen && !_isGreen)
    20.                 {
    21.                     _animator.SetTrigger("ErrorRedEvent");
    22.                 }
    23.  
    24.                 if (!_selectedColorisGreen && _isGreen)
    25.                 {
    26.                     _animator.SetTrigger("ErrorGreenEvent");
    27.                 }
    28.             }
    29.         }  
    30.     }
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    I think C# 8.0 supports tuple patterns in switch, so this should work in at least Unity 2021, maybe 2020. I Just tried it on 2021.

    Code (CSharp):
    1. switch (_selectedColorisGreen, _isGreen)
    2. {
    3.     case (true, true):
    4.         _animator.SetTrigger("GreenEvent");
    5.         break;
    6.     case (false, false):
    7.         _animator.SetTrigger("RedEvent");
    8.         break;
    9.     case (true, false):
    10.         _animator.SetTrigger("ErrorRedEvent");
    11.         break;
    12.     case (false, true):
    13.         _animator.SetTrigger("ErrorGreenEvent");
    14.         break;
    15. }
    And shorter way would be to use the arrow operators:

    Code (CSharp):
    1. string trigger = (_selectedColorisGreen, _isGreen) switch
    2. {
    3.     (true, true) => ("GreenEvent"),
    4.     (false, false) => ("RedEvent"),
    5.     (true, false) => ("ErrorRedEvent"),
    6.     (false, true) => ("ErrorGreenEvent"),
    7. };
    8. _animator?.SetTrigger(trigger);
    See this documentation:
    https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8
     
    Last edited: May 1, 2021
    Kurt-Dekker and DonPuno like this.
  3. DonPuno

    DonPuno

    Joined:
    Dec 22, 2020
    Posts:
    57
    Hey @Olmi !
    Many thanks, I'm using Unity Version 2020.3.1f1 and the code works fine. That looks so much better and easier to understand.