Unity3d Extension methods example

Extension methods are awesome for extending the functionality of existing classes. What is really cool is that if you use an IDE that supports autocompletion, your extension methods will show up when using the classes you extend.

For this example, let’s suppose that you want to extend Unity’s AnimationClip to support the idea of adding an “OnAnimationEnd” event. When the animation clip ends, a callback will be called.

using UnityEngine;

public static class AnimationClipExtensions
{
    public static void AddOnAnimationEndEvent (this AnimationClip animationClip, string onAnimationEndCallbackName)
    {
        AddOnAnimationEndEvent (animationClip, onAnimationEndCallbackName, 0, 0.0f, "", null);
    }

    public static void AddOnAnimationEndEvent (this AnimationClip animationClip, string onAnimationEndCallbackName, int intParameter)
    {
        AddOnAnimationEndEvent (animationClip, onAnimationEndCallbackName, intParameter, 0.0f, "", null);
    }

    public static void AddOnAnimationEndEvent (this AnimationClip animationClip, string onAnimationEndCallbackName, float floatParameter)
    {
        AddOnAnimationEndEvent (animationClip, onAnimationEndCallbackName, 0, floatParameter, "", null);
    }

    public static void AddOnAnimationEndEvent (this AnimationClip animationClip, string onAnimationEndCallbackName, string stringParameter)
    {
        AddOnAnimationEndEvent (animationClip, onAnimationEndCallbackName, 0, 0.0f, stringParameter, null);
    }

    public static void AddOnAnimationEndEvent (this AnimationClip animationClip, string onAnimationEndCallbackName, Object objectReferenceParameter)
    {
        AddOnAnimationEndEvent (animationClip, onAnimationEndCallbackName, 0, 0.0f, "", objectReferenceParameter);
    }

    private static void AddOnAnimationEndEvent (this AnimationClip animationClip, string onAnimationEndCallbackName, int intParameter, float floatParameter, string stringParameter, Object objectReferenceParameter)
    {
        AnimationEvent animEvent = new AnimationEvent ();
        animEvent.time = animationClip.length;
        animEvent.functionName = onAnimationEndCallbackName;
        animEvent.intParameter = intParameter;
        animEvent.floatParameter = floatParameter;
        animEvent.stringParameter = stringParameter;
        animEvent.objectReferenceParameter = objectReferenceParameter;
        animEvent.messageOptions = SendMessageOptions.DontRequireReceiver;
        animationClip.AddEvent (animEvent);
    }
}

Now that we have a few extension methods, let’s test it out. Create a new component and fill in the animation clip in the inspector

using UnityEngine;
using System.Collections;

public class ExtensionMethodTest : MonoBehaviour
{
    public AnimationClip testClip;

    void Start ()
    {
        testClip.AddOnAnimationEndEvent("TestOnAnimationEndEvent");
        testClip.name= "TestClip";
        animation.Play("TestClip");
    }

    void Update ()
    {

    }

    void TestOnAnimationEndEvent()
    {
        Debug.Log("TestOnAnimationEndEvent() was called.");
    }
}

Leave a Reply

Your email address will not be published.