之前学习了扩展方法,今天看了DoTween的源码,豁然开朗,极致的运用了扩展方法
先上代码
/// <summary>Tweens a Material's named color property to the given value. /// Also stores the material as the tween's target so it can be used for filtered operations</summary> /// <param name="endValue">The end value to reach</param> /// <param name="property">The name of the material property to tween (like _Tint or _SpecColor)</param> /// <param name="duration">The duration of the tween</param> public static TweenerCore<Color, Color, ColorOptions> DOColor( this Material target, Color endValue, string property, float duration) { if (!target.HasProperty(property)) { if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(property); return (TweenerCore<Color, Color, ColorOptions>) null; } TweenerCore<Color, Color, ColorOptions> t = DOTween.To((DOGetter<Color>) (() => target.GetColor(property)), (DOSetter<Color>) (x => target.SetColor(property, x)), endValue, duration); t.SetTarget<TweenerCore<Color, Color, ColorOptions>>((object) target); return t; }
当然这只是众多重载其中的一个,我们就拿出一个来一起看下。
1.首先必须是静态static方法
2.第一个参数要用this关键字,是作用的目标类
3.后面的参数就比较随意了,就看你要用啥了,比如上面代码中,又用到了3个参数,就照这种写法是没问题的。
装模作样的声明一下:本博文章若非特殊注明皆为原创,若需转载请保留原文链接
https://blog.csdn.net/Wrinkle2017/article/details/118608863
及作者信息