在 Unity 中缩放模态 UI 预制件

Scale in modal UI prefab in Unity

我正在 Unity 中开发一款包含许多 UI 元素的学习游戏。有时我需要显示一个模态,它是预制件的一个实例,我想添加一个放大效果。我尝试使用 DOTween 使用以下代码:

GameObject modalPrefab = Instantiate 
                                (
                                    Resources.Load<GameObject>(prefabName),
                                    new Vector3(0, 0, 0), Quaternion.identity
                                );
Vector3 scale = modalPrefab.transform.localScale; // Store the current scale
modalPrefab.transform.DOScale(0, 0); // Set size to zero
modalPrefab.transform.DOScale(scale, 0.8f); // Scale in

这种方法有效,但效果不是很好,因为它会在缩小预制件之前以全尺寸显示预制件一毫秒,而且非常引人注目。关于如何以更好的方式(有或没有 DOTween)做到这一点的任何想法?谢谢

您可以使用动画来实现缩放效果。只需在激活时将 Modal 预制件的比例从 0 更改为 1,反之亦然。

我发现游戏开发指南中的教程非常有用。他在视频中使用“Lean Tween”。看看You're Animating Your UI Wrong in Unity

终于找到了解决办法,很简单。不要使用 DOTween 缩放到零,只需以 Unity 方式进行即可:

modalPrefab.transform.localScale = Vector2.zero;