简体   繁体   English

使用C#委托的Unity3D Tween库

[英]Unity3D Tween library that uses C# delegates

Is there a Unity3D tween library that uses C# delegates? 是否存在使用C#委托的Unity3D补间库? I've been using iTween and LeanTween, but instead of delegates they require method names to call in form of strings, which results in quite ugly code. 我一直在使用iTween和LeanTween,但是它们不是委托,而是需要方法名称以字符串形式调用,这导致代码很丑陋。 I'd like to replace all the custom methods with slim lambdas, but these libraries don't provide such capability. 我想用苗条的lambda替换所有自定义方法,但是这些库不提供这种功能。

Not sure about the delegates but this small class has never let me down 不确定代表们,但这个小班从来没有让我失望

public class Tween  {

    public static float TweenValue (float from,float to,float time)
    {
        if (from != to) 
        {
            if (Mathf.Abs(to-from) > 1.0)
            {
                from = Mathf.Lerp(from, to, time*Time.deltaTime);
            }
            else 
            {
                from = to;
            }       
        }

        return from;
    }

    public static Rect TweenRect (Rect from,Rect to,float time){
        if (from != to) 
        {
            from.x = TweenValue (from.x,to.x,time*Time.deltaTime);
            from.y = TweenValue (from.y,to.y,time*Time.deltaTime);
            from.width = TweenValue (from.width,to.width,time*Time.deltaTime);
            from.height = TweenValue (from.height,to.height,time*Time.deltaTime);
        }
        return from;
    }

    public static Vector3 TweenVector3 (Vector3 from ,Vector3 to,float time)
    {
        if (from != to) 
        {
            if (Mathf.Abs((to-from).magnitude) > 0.2)
            { 
                from = Vector3.Slerp(from, to, time);
            }
            else
            { 
                from = to;
            }
        }

        return from;
    }

    public static Quaternion TweenQuaternion (Quaternion from,Quaternion to,float time)  
    {
        if (from != to) 
        {
            if (Mathf.Abs((to.eulerAngles-from.eulerAngles).magnitude) > 0.2) 
            {
                from = Quaternion.Slerp(from, to, time);
            }
            else 
            {
                from = to;
            }
        }

        return from;
    }   
}

The new version of LeanTween offers onComplete and onUpdate delegates that do not rely on string names. 新版本的LeanTween提供了不依赖字符串名称的onComplete和onUpdate委托。 Find out more about all the new features in LeanTween 2.0: http://dentedpixel.com/developer-diary/leantween-2-0-faster-type-safe-and-c/ 了解有关LeanTween 2.0中所有新功能的更多信息: http ://dentedpixel.com/developer-diary/leantween-2-0-faster-type-safe-and-c/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM