C#中级编程——C#扩展方法,基于Unity(二)

2021/7/9 17:14:22

本文主要是介绍C#中级编程——C#扩展方法,基于Unity(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C#中级编程——C#扩展方法,基于Unity(二)

之前学习了扩展方法,今天看了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
及作者信息



这篇关于C#中级编程——C#扩展方法,基于Unity(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程