gpt4 book ai didi

c# 泛型,类型略有不同?

转载 作者:太空狗 更新时间:2023-10-30 00:49:48 26 4
gpt4 key购买 nike

注意两个扩展名,一个用于 float,一个用于 Vector3。

请注意,var( 调用中只有细微差别。

在 C# 中,这些可以写成一个泛型吗?

我的问题的实质是:

在泛型中,您能否根据类型的性质进行分支?

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);
}

(对于任何 c# 专家阅读,代码示例在 Unity 中,您可以在协程中访问框架系统。)

对于任何 Unity 开发人员阅读,您如何调用 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) );

(如果您是 Unity 的新手并且不熟悉扩展的基本概念,这里有一个 intro。)

最佳答案

如果你多做一个就可以了 Func<T,T>在调用 var 之前执行转换操作(您应该重命名,因为 var 是 C# 关键字)。

这是您可以采用的一种方法:

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));
}

它甚至可以在线完成:

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);
}

也许更自然的成语是

    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;

关于c# 泛型,类型略有不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36121120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com