Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# generic with slight difference for types?

Notice the two extensions, one for float, one for Vector3.

Notice there's only a slight difference in the var( call.

In c# could these be written as one as a generic??

The essence of my question is:

within a generic, can you branch on the nature of the type?

public static IEnumerator Tweeng( this float duration,
         System.Action<float> vary, float aa, float zz )
{
    float sT = Time.time;
    float eT = sT + duration;

    while (Time.time < eT)
    {   
        float t = (Time.time-sT)/duration;
        vary( Mathf.SmoothStep(aa,zz, t) ); // slight difference here
        yield return null;
    }

    vary(zz);
}

public static IEnumerator Tweeng( this float duration,
      System.Action<Vector3> vary, Vector3 aa, Vector3 zz )
{
    float sT = Time.time;
    float eT = sT + duration;

    while (Time.time < eT)
    {
        float t = (Time.time-sT)/duration;
        vary( Vector3.Lerp(aa,zz, t) ); // slight difference here
        yield return null;
    }

    vary(zz);
}

(BTW for any c# gurus reading, the code example is in Unity, where you access the frame system in a coroutine.)

For any Unity devs reading, examples of how you call Tweeng

// tweeng z to 20 degrees in .12 seconds
StartCoroutine(.12f.Tweeng( (t)=>transform.Eulers(0f,0f,t), 0f,20f) );
// fade in alpha in .75 seconds
StartCoroutine(.75f.Tweeng( (u)=>{c.a=u;s.color=c;}, 0f,1f) );

(If you're new to Unity and not familiar with the basic concept of extensions, here's an intro.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can define your method as follows:

public static IEnumerator Tweeng<T>(this float duration,
         System.Action<T> var, T aa, T zz, Func<T,T,float,T> thing)
{
    float sT = Time.time;
    float eT = sT + duration;

    while (Time.time < eT)
    {
        float t = (Time.time - sT) / duration;
        var(thing(aa, zz, t));
        yield return null;
    }

    var(zz);
}

And then using it:

float a = 5;
float b = 0;
float c = 0;
a.Tweeng(q => {}, b, c, Mathf.SmoothStep);

Or:

float a = 0;
Vector3 b = null;
Vector3 c = null;
a.Tweeng(q => {}, b, c, Vector3.Lerp);

Alternatively, if you want to get rid of the method passing, you can have simple overloads to handle it:

public static IEnumerator Tweeng(this float duration, System.Action<float> var, float aa, float zz)
{
    return Tweeng(duration, var, aa, zz, Mathf.SmoothStep);
}
public static IEnumerator Tweeng(this float duration, System.Action<Vector3> var, Vector3 aa, Vector3 zz)
{
    return Tweeng(duration, var, aa, zz, Vector3.Lerp);
}

private static IEnumerator Tweeng<T>(this float duration,
         System.Action<T> var, T aa, T zz, Func<T,T,float,T> thing)
{
    float sT = Time.time;
    float eT = sT + duration;

    while (Time.time < eT)
    {
        float t = (Time.time - sT) / duration;
        var(thing(aa, zz, t));
        yield return null;
    }

    var(zz);
}

And then using it:

float a = 5;
float b = 0;
float c = 0;
a.Tweeng(q => {}, b, c);

Or:

float a = 0;
Vector3 b = null;
Vector3 c = null;
a.Tweeng(q => {}, b, c);


Stub methods to compile in LINQPad/without unity:
public class Mathf { public static float SmoothStep(float aa, float zz, float t) => 0; }
public class Time { public static float time => DateTime.Now.Ticks; }
public class Vector3 { public static Vector3 Lerp(Vector3 aa, Vector3 zz, float t) => null; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...