Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Switch Case | How, When and Why to use! Why use Switch Case instead of If statements! VIDEO TUTORIAL

Discussion in 'Community Learning & Teaching' started by Sykoo, Oct 31, 2014.

?

Was this video helpful?

  1. Yes

    100.0%
  2. No

    0 vote(s)
    0.0%
  3. It could've been better

    100.0%
Multiple votes are allowed.
  1. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Hello!

    In this video, we're going to be discussing on how to use Switch Case and why to use them instead of If statements in some situations, depending on your instincts :)



    Got any feedback or constructive criticism about my improvement?
    Or maybe a suggestion for tutorial video? Let me know, please :)
     
  2. emmcd3

    emmcd3

    Joined:
    Dec 16, 2012
    Posts:
    26
    @Sykoo,
    A use case from a Unity script....

    I lifted this from the Bootcamp demo. I offer this up as a more concrete (no pun intended) implementation. In some sense, it is actually self explanatory. Note that it does include common conditional logic which is appropriate in this instance :

    It is UnityScript and you could easily point out any differences :

    #pragma strict
    #pragma implicit
    #pragma downcast

    enum HitType
    {
    CONCRETE,
    WOOD,
    METAL,
    OLD_METAL,
    GLASS,
    GENERIC
    }

    class BulletMarks extends MonoBehaviour
    {
    public var concrete : Texture2D[];
    public var wood : Texture2D[];
    public var metal : Texture2D[];
    public var oldMetal : Texture2D[];
    public var glass : Texture2D[];
    public var generic : Texture2D[];

    public function GenerateDecal(type : HitType, go : GameObject)
    {
    var useTexture : Texture2D;
    var random : int;

    switch(type)
    {
    case HitType.CONCRETE:
    if(concrete == null) return;
    if(concrete.Length == 0) return;

    random = Random.Range(0, concrete.Length);

    useTexture = concrete[random];
    break;
    case HitType.WOOD:
    if(wood == null) return;
    if(wood.Length == 0) return;

    random = Random.Range(0, wood.Length);

    useTexture = wood[random];
    break;
    case HitType.METAL:
    if(metal == null) return;
    if(metal.Length == 0) return;

    random = Random.Range(0, metal.Length);

    useTexture = metal[random];
    break;
    case HitType.OLD_METAL:
    if(oldMetal == null) return;
    if(oldMetal.Length == 0) return;

    random = Random.Range(0, oldMetal.Length);

    useTexture = oldMetal[random];
    break;
    case HitType.GLASS:
    if(glass == null) return;
    if(glass.Length == 0) return;

    random = Random.Range(0, glass.Length);

    useTexture = glass[random];
    break;
    case HitType.GENERIC:
    if(generic == null) return;
    if(generic.Length == 0) return;

    random = Random.Range(0, generic.Length);

    useTexture = generic[random];
    break;
    default:
    if(wood == null) return;
    if(wood.Length == 0) return;

    random = Random.Range(0, wood.Length);

    useTexture = wood[random];
    return;
    }

    transform.Rotate(new Vector3(0, 0, Random.Range(-180.0, 180.0)));

    Decal.dCount++;
    var d : Decal = gameObject.GetComponent("Decal");
    d.affectedObjects = new GameObject[1];
    d.affectedObjects[0] = go;
    d.decalMode = DecalMode.MESH_COLLIDER;
    d.pushDistance = 0.009 + BulletMarkManager.Add(gameObject);
    var m : Material = new Material(d.decalMaterial);
    m.mainTexture = useTexture;
    d.decalMaterial = m;
    d.CalculateDecal();
    d.transform.parent = go.transform;
    }
    }
     
  3. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Thank you very much for your example! :)
     
    emmcd3 likes this.