Passing reference of transform properties to coroutine animation?

Hello!,


I’m doing a mini-library of simple coroutine animations to change position, rotation, scales, etc. It all works fine, but the routine code for most of them is exactly the same, with the difference that one changes the transform.position, another the transform.localPosition, etc, and I was wondering if there was a way to simplify this.


I tried using callbacks, which work better and simplifies the code a lot but I think there could be ways to improve further? This is what I have atm,


public static Coroutine Transform(Transform transform, AnimationType animType, Vector3 from, Vector3 to, float duration, Easing.Type easingType = default)
        {
            if(animType == AnimationType.Move)
                return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => transform.position = result));
            else if(animType == AnimationType.MoveLocal)
                return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => transform.localPosition = result));
            else if (animType == AnimationType.Rotate)
                return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => transform.eulerAngles = result));
            else if (animType == AnimationType.RotateLocal)
                return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => transform.localEulerAngles = result));
            else if (animType == AnimationType.Scale)
                return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => transform.localScale = result));
            else
                return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => transform.position = result));
        }
 
...
 
        public static IEnumerator AnimationRoutine(Vector3 from, Vector3 to, float duration, Easing.Type easingType, Action<Vector3> result)
        {
            Easing.EasingMethod EasingMethod = Easing.GetEasing(easingType);
            for (float t = 0; t < duration; t += Time.fixedDeltaTime)
            {
                result(EasingMethod(t / duration) * (to - from) + from);
                yield return new WaitForFixedUpdate();
            }
            result(to);
        }

StaticCoroutine is a class I’m using to call static coroutines.
Is there any way to simplify this further? For instance, if I want to create other overloads, I need to write all the if’s again, as I’m not being able to return a reference to transform.position and add it to the lambda function.


Thank you :slight_smile:

Code not tested

private static Dictionary<AnimationType, Action<Transform, Vector3>> animationActions = new Dictionary<AnimationType, Action<Transform, Vector3>>
{
    {AnimationType.Move,        (t, v) => t.position = v},
    {AnimationType.MoveLocal,   (t, v) => t.localPosition = v},
    {AnimationType.Rotate,      (t, v) => t.eulerAngles = v},
    {AnimationType.RotateLocal, (t, v) => t.localEulerAngles = v},
    {AnimationType.Scale,       (t, v) => t.localScale = v}
};
public static Coroutine Transform(Transform transform, AnimationType animType, Vector3 from, Vector3 to, float duration, Easing.Type easingType = default)
{
    Action<Transform, Vector3> animationAction;
    if(animationActions.TryGetValue(animType, out animationAction) == false)
        animationAction = (t, v) => t.position = v; // fallback

    return StaticCoroutine.Start(AnimationRoutine(from, to, duration, easingType, result => animationAction(transform, result)));
}

public static IEnumerator AnimationRoutine(Vector3 from, Vector3 to, float duration, Easing.Type easingType, Action<Vector3> result)
{
    Easing.EasingMethod EasingMethod = Easing.GetEasing(easingType);
    for (float t = 0; t < duration; t += Time.fixedDeltaTime)
    {
        result(EasingMethod(t / duration) * (to - from) + from);
        yield return new WaitForFixedUpdate();
    }
    result(to);
}