You can do this if you make an extra Func<T,T> that does the conversion before invoking the var action (which you must rename because var is the C # keyword).
Here is one way you could take:
public static IEnumerator Tweeng<T>( this float duration , System.Action<T> varAction , T aa , T zz ) { Func<T,T,float,T> transform = MakeTransform<T>(); float sT = Time.time; float eT = sT + duration; while (Time.time < eT) { float t = (Time.time-sT)/duration; varAction(transform(aa, zz, t)); yield return null; } varAction(zz); } private static Func<T,T,float,T> MakeTransform<T>() { if (typeof(T) == typeof(float)) { Func<float, float, float, float> f = Mathf.SmoothStep; return (Func<T,T,float,T>)(Delegate)f; } if (typeof(T) == typeof(Vector3)) { Func<Vector3, Vector3, float, Vector3> f = Vector3.Lerp; return (Func<T,T,float,T>)(Delegate)f; } throw new ArgumentException("Unexpected type "+typeof(T)); }
It can even be done inline:
public static IEnumerator DasTweeng<T>( this float duration, System.Action<T> vary, T aa, T zz ) { float sT = Time.time; float eT = sT + duration; Func<T,T,float,T> step; if (typeof(T) == typeof(float)) step = (Func<T,T,float,T>)(Delegate)(Func<float, float, float, float>)Mathf.SmoothStep; else if (typeof(T) == typeof(Vector3)) step = (Func<T,T,float,T>)(Delegate)(Func<Vector3, Vector3, float, Vector3>)Vector3.Lerp; else throw new ArgumentException("Unexpected type "+typeof(T)); while (Time.time < eT) { float t = (Time.time-sT)/duration; vary( step(aa,zz, t) ); yield return null; } vary(zz); }
Perhaps a more natural idiom
Delegate d; if (typeof(T) == typeof(float)) d = (Func<float, float, float, float>)Mathf.SmoothStep; else if (typeof(T) == typeof(Vector3)) d = (Func<Vector3, Vector3, float, Vector3>)Vector3.Lerp; else throw new ArgumentException("Unexpected type "+typeof(T)); Func<T,T,float,T> step = (Func<T,T,float,T>)d;