Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Extension Methods

Discussion in 'Scripting' started by TaleOf4Gamers, Feb 16, 2017.

  1. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    800
    Hey everyone, this is more of a C# question but I have a Unity example to show.
    When do you choose to use an Extension Method?

    An example I would like feedback on is with playing animations. I made a couple of extension methods to help with the playing of animations as I felt there were missing. Such as playing an animation with a set speed and then resetting that speed back to default of 1.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public static class ExtensionMethodsAnimation {
    4.  
    5.     public static void PlayWithSetSpeed(this Animation anim, string animName, float animSpeed)
    6.     {
    7.         PlayAnimation(anim, animName, animSpeed);
    8.     }
    9.  
    10.     public static void PlayWithSpeedAndReset(this Animation anim, string animName, float animSpeed)
    11.     {
    12.         PlayAnimation(anim, animName, animSpeed);
    13.         anim[animName].speed = 1f;
    14.     }
    15.  
    16.     static void PlayAnimation(Animation anim, string animName, float animSpeed)
    17.     {
    18.         anim[animName].speed = animSpeed;
    19.         anim.Play(animName);
    20.     }
    21. }
    22.  
    Feedback and advice is always greatly appreciated.
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    In general there are four rules of thumb I found on the internet some time ago which I generally follow:
    A good extension method should:
    • Apply to any possible instance of the type it extends.
    • Simplify logic and improve readability/maintainability.
    • Apply to the most specific type or interface applicable.
    • Be isolated in a namespace so that it does not pollute IntelliSense.

    So to go over them one by one:
    • Apply to any possible instance of the type it extends: Applies. These extension methods apply too all instances of animation
    • Simplify logic and improve readability/maintainability: Applies. Naming is clear in what the method does, and each method does a single clear thing so both readability and maintainability do increase.
    • Apply to the most specific type or interface applicable: Kind of applies. Seeing as Unity barely uses interfaces using the specific Animation class is the most specific it will get.
    • Be isolated in a namespace so that it does not pollute IntelliSense: Currently does not apply. Make sure it lives it it's own namespace, preferably something like: "Assets.Scripts.Extensions". Also the name of the class should, in my opinion, be "AnimationExtensions" as that seems more like a natural sentence to me.
    source: http://geekswithblogs.net/BlackRabb...ion-methods---to-extend-or-not-to-extend.aspx

    Be careful with extension methods though (check the sample from the source), they can bite you in the butt if misused, causing some unnecessary refactoring if not planned accordingly. (f.e. you create it for a specific class, then later on inherit said class, and try to use the method on the child, which won't work, so you need to introduce an interface or refactor the method to be a standard helper method).
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    800
    Yeah, I saw these before.

    I will do this ASAP. It makes sense.

    Yeah that does make sense. haha I had a name previously then just slapped Animation on the end. I assume I can rename the class (and file) without repercussion.

    Thanks.
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You'll need to add the proper namespace to the files where you use the extension methods, but changing the class name should indeed not matter.
     
    TaleOf4Gamers likes this.