找回密码
 立即注册
查看: 159|回复: 0

02 uGUI源码-CoroutineTween.cs

[复制链接]
发表于 2022-11-22 17:20 | 显示全部楼层 |阅读模式
ITweenValue Interface

定义


  • Base interface for tweeners
    Tweener的基本接口。
Code summary: using an interface instead of an abstract class as we want the tweens to be structs.
这里使用接口而不是抽象类的原因是我们希望Tweener是值类型。
成员

void TweenValue(float floatPercentage);  //Runner执行方法bool ignoreTimeScale { get; } //时间增量是否受到timeScale影响float duration { get; }  //过渡需要的总体时间bool ValidTarget(); //TweenCallback是否不为nullColorTween Struct

定义


  • Color tween class, receives the TweenValue callback and then sets the value on the target.
    颜色渐变类型,接收TweenValue回调,然后设置目标器上的值。(继承ITweenValue)
字段以及属性


  • ColorTweenMode(enum)
    颜色渐变类型(All,RGB,Alpha)。
  • m_Target(ColorTweenCallback)
    每次执行完TweenValue方法后调用的回调。
public class ColorTweenCallback : UnityEvent<Color> {}

  • m_StartColor/startColor(Color)
    起始Color值。
  • m_TargetColor/targetColor(Color)
    目标Color值。
  • duration & ignoreTimeScale
    接口定义的属性。
成员方法

TweenValue


定义
提供给TweenRunner执行的方法。
public void TweenValue(float floatPercentage){        //若回调m_Target为null,则直接返回        if (!ValidTarget()) return;        //Lerp插值函数,floatPercentage为返回比例。        //当floatPercentage为0时返回m_StartColor;        //floatPercentage为1时返回m_TargetColo。        var newColor = Color.Lerp(m_StartColor, m_TargetColor, floatPercentage);        //ColorTweenMode为Alpha值,则RGB保持startColor值        if (m_TweenMode == ColorTweenMode.Alpha){                newColor.r = m_StartColor.r;                newColor.g = m_StartColor.g;                newColor.b = m_StartColor.b;        }        //ColorTweenMode为RGB值,则Alpha保持startColor值        else if (m_TweenMode == ColorTweenMode.RGB){                newColor.a = m_StartColor.a;        }        //调用m_Target回调        m_Target.Invoke(newColor);}AddOnChangedCallback


定义
向m_Target注册回调事件。
public void AddOnChangedCallback(UnityAction<Color> callback);
GetIgnoreTimescale/GetDuration 获取对应属性值。
ValidTarget 确认m_Target是否为null。
FloatTween Struct

与ColorTween实现基本相同。
TweenRunner<T> Class

定义


  • Tween runner, executes the given tween. The coroutine will live within the given behaviour container.
    补间运行器(TweenRunner),执行给定的补间(Tween)。协程将存在于给定的行为容器(Behaviour Container)中。
where T : struct, ITweenValue //T必须是Tween
字段以及属性


  • m_CoroutineContainer(MonoBehaviour)
    协程依附的行为容器。
  • m_Tween(IEnumerator)
    补间协程。
成员方法

Start


定义
将Tweener包装成一个IEnumerator。
private static IEnumerator Start(T tweenInfo){      if (!tweenInfo.ValidTarget())             yield break;  //m_Taregt为null,直接中断协程      var elapsedTime = 0.0f;      while (elapsedTime < tweenInfo.duration){             elapsedTime += tweenInfo.ignoreTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;             var percentage = Mathf.Clamp01(elapsedTime / tweenInfo.duration);             //每个deltaTime执行m_Target             tweenInfo.TweenValue(percentage);             yield return null; //等待一帧继续执行      }      //补间完成,最后执行一次      tweenInfo.TweenValue(1.0f);}Init


定义
设置m_CoroutineContainer
public void Init(MonoBehaviour coroutineContainer);StartTween


定义
开始一个Tweener协程。
public void StartTween(T info);
首先检查 m_CoroutineContainer是否为null,然后停止当前运行的协程;
然后检查 m_CoroutineContainer在GameScene中是否显示,若不显示则不用执行补间,直接执行TweenValue(1.0f):
if (!m_CoroutineContainer.gameObject.activeInHierarchy){      info.TweenValue(1.0f);      return;}
最后检查完毕,创建Tween协程并运行。
StopTween


定义
停止一个Tweener协程。
public void StopTween();
这里停止的协程是当前的m_Tween。
总结

CoroutineTween.cs脚本主要在Graphic类与Dropdown类中使用,负责Color与Alpha值的平滑过渡。
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-27 20:54 , Processed in 0.090578 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表