报错 DOTWEEN ► Tween startup failed (NULL target/property - ): the tween will now be killed...的解决办法

问题描述

UI面板中使用了DOTween插件实现一个效果,在退出UI时消耗了UI面板,此时产生报错。

DOTWEEN ► Tween startup failed (NULL target/property - ): the tween will now be killed ► The object of type ‘RectTransform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

报错如下图
在这里插入图片描述

错误分析及解决办法

结合这段错描述,我在调用DOScale()方法时,产生了Tweens对象,当Ui的GameObject在运行状态下被销毁时,Tweens对象也就完成了自己的工作,之后Tweens会被自动销毁。在Gameobject被销毁到Tweens自动销毁的这段时间内,Tweens没有立即停止工作,就会弹出如上图的警告。

(Tweens仍在工作可能存在多种情况,比如我本人是因为一些特殊需求,DOScale在Update里进行调用。当然这部分的描述可能不太严谨,我也尝试在Update中移除调用DOScale,结果并没有产生任何警告或报错。只是猜测是销毁后,Update中的dotween某些插值运算还没有真正停止。)

实际上,这个警告并不影响程序流程,但它会影响DebugConsole的美观,如果Tweens自动销毁稍晚一些,就会弹出很多很多条这样的警告。如果DOTween的Preferences中没有设置检查SafeMode,那么这个警告就会变成错误。

综上所述,需要关闭该警告,好的做法需要在Ui关闭或销毁时,进行手动销毁Tweens。比如OnDIsable() 或 OnDestroy() 中调用 DOTween.KillAll() 方法。如

private void OnDisable()
{
    
    
   DOTween.KillAll();
}

这篇笔记是我在搜索解决办法时,看到的一位日本大佬的QA,感谢有道翻译。
原文地址:DOTWEEN ► Tween startup failed (NULL target/property - ): the tween will now be killed

猜你喜欢

转载自blog.csdn.net/wankcn/article/details/128617366
今日推荐