我正在为我的UI元素制作一个简单的动画。
我有一个动画器组件,它有 2 种不同的动画 - 放大和缩小。
每当需要在屏幕上显示 UI 元素(例如按钮)时,都会显示这些动画。
我通常更喜欢在不显示时停用游戏对象。
我为动画编写了以下方法:
private IEnumerator ToggleObjectWithAnimation (GameObject gameObj) { Animator gameObjectAnimator = gameObj.GetComponent (); 动画器设置为未缩放时间 if (gameObj.activeSelf == false) { gameObj.transform.localScale = new Vector3 (0, 0, 1.0f); gameObj.SetActive (true); gameObjectAnimator.SetTrigger ("ZoomIn"); 收益率回报新的等待秒(0.5f); } else if(gameObj.activeSelf == true) { gameObjectAnimator.SetTrigger ("ZoomOut"); 收益率回报新的等待秒(0.5f); gameObj.SetActive (false); 当时间刻度 = 0 时不执行代码 } 收益率返回空;}
代码在大多数屏幕上工作正常,但当我使用时间刻度 = 0 暂停游戏时会显示问题。
当时间刻度为 0 时,行 gameObj.SetActive (false) 不起作用。
可能有点晚了,但是:
问题不在于SetActive
,而在于WaitForSeconds
受到Time.timeScale
的影响!
实际暂停时间等于给定时间乘以
Time.timeScale
。如果您希望使用未缩放的时间等待,请参阅WaitForSecondsRealtime
。
所以如果你有Time.timescale=0
,WaitForSeconds
永远不会结束!
你宁愿用WaitForSecondsRealtime
private IEnumerator ToggleObjectWithAnimation (GameObject gameObj)
{
Animator gameObjectAnimator = gameObj.GetComponent (); // Animator is set to unscaled time
if (gameObj.activeSelf == false)
{
gameObj.transform.localScale = new Vector3 (0, 0, 1.0f);
gameObj.SetActive (true);
gameObjectAnimator.SetTrigger ("ZoomIn");
yield return new WaitForSecondsRealtime(0.5f);
}
else if(gameObj.activeSelf == true)
{
gameObjectAnimator.SetTrigger ("ZoomOut");
yield return new WaitForSecondsRealtime(0.5f);
gameObj.SetActive (false); // code not execute when timescale = 0
}
yieldr eturn null;
}