在播放动画完成之后,才能将物体销毁/进行操作/修改Time.timeScale。
碰到这样的需求,需要这样用到动画的回调函数:
public void LiveImagesFade() { Tweener tweener = liveImage.DOFade(1f, 0.5f); tweener.OnComplete(LiveImageVanish);//调用回调函数OnComplete,参数是指向一个没有返回值和参数的函数的委托 } void LiveImageVanish() { liveImage.DOFade(0.3f, 3f); }
但是我还想给这个回调函数带参数怎么办?我希望这个面板渐隐之后,调用它的SetActive(false)方法,而我需要根据参数来判断此时是哪一个面板。
这里就要分析一下这个OnComplete函数的参数究竟是什么了:
//
// 摘要:
// Sets the onComplete callback for the tween. Called the moment the tween reaches
// its final forward position, loops included
public static T OnComplete<T>(this T t, TweenCallback action) where T : Tween;
//
// 摘要:
// Used for tween callbacks
public delegate void TweenCallback();
可以看到:它的参数有两个,一个是调用这个函数的Tweener,一个是委托函数(无返回值无参数),因此,是不能用它本身的回调函数实现我想要的功能的。
一种解决方法是,写很多个无参函数。
另一个种解决方法是,使用协程,等待对应的播放动画的时间秒数之后执行我的消除函数。在此采用第二种:
void OnFadeComplete(string canvas) { switch (canvas) { case "Settings": SettingsPanel.SetActive(false); break; } } IEnumerator InactivePanel(string canvas,float waitTime) { yield return new WaitForSeconds(waitTime); OnFadeComplete(canvas); }
SettingsCanvas.DOFade(0f, fadeTime);//播放动画 StartCoroutine(InactivePanel("Settings", fadeTime));//调用协程,等待对应时间后,调用消除函数。