简体   繁体   English

Unity:如何在几秒钟内淡出场景

[英]Unity: how to fade away scene for few seconds

I'm programming a simple game, in which the player has to collect some objects.我正在编写一个简单的游戏,玩家必须在其中收集一些对象。 What I would like to do is that after he collects an object, the scene fades away for few seconds and then the player position is changed.我想要做的是,在他收集一个物体后,场景会消失几秒钟,然后改变玩家位置。

I know how to change the position, I have no idea how to make the fade away effect.我知道如何改变位置,我不知道如何制作渐隐效果。 I've tried using the Image Effect (such as Vortex and Blur) but I'm no able to slowly increment their variables (eg angle value for Vortex and blur iteration for blur) so that gives the impression of an animation.我已经尝试使用图像效果(例如 Vortex 和 Blur),但我无法缓慢增加它们的变量(例如 Vortex 的角度值和模糊的模糊迭代),因此给人一种动画的印象。

Could someone guide me through this?有人可以指导我完成这个吗?

Add the ScreenOverlay image effect to your camera, set BlendMode to ScreenBlend and Intensity to 0, then add the following script:将 ScreenOverlay 图像效果添加到您的相机,将 BlendMode 设置为 ScreenBlend 并将 Intensity 设置为 0,然后添加以下脚本:

bool fading;
float fadeValue = 0;
const float INCREMENT = 0.01f;
const float MAX_BLEND = 2;
ScreenOverlay so;

void OnTriggerEnter(Collider other)
{
    fading = true;
}

void Start()
{
    so = gameObject.GetComponent<ScreenOverlay>();
}

void Update()
{
    if (fading)
    {
        fadeValue += INCREMENT;
        so.intensity = fadeValue * MAX_BLEND;

        if (fadeValue >= 1)
        {
            fading = false;
            // change player position
        }
    }
    else if (fadeValue > 0)
    {
        fadeValue = Mathf.Max(0, fadeValue - INCREMENT);
        so.intensity = fadeValue * MAX_BLEND;
    }
}

To increase duration, just make INCREMENT smaller.要增加持续时间,只需将INCREMENT变小。 It works perfectly for me.它非常适合我。

You can add a new object with texture on the scene so it would be something like global mask.您可以在场景中添加一个带有纹理的新对象,使其类似于全局蒙版。 If you want your scene under the fade effect to be simply black, then a little square texture filled black should be enough, you can scale it properly so it would fit the camera.如果您希望淡入淡出效果下的场景只是黑色,那么填充黑色的小方形纹理就足够了,您可以正确缩放它以适合相机。 And shader on this material should support transparency so the alpha layer of the texture would be manageable.此材质上的着色器应支持透明度,因此纹理的 alpha 层将是可管理的。 Then you can operate the alpha value from code using renderer.sharedMaterial.SetFloat() method.然后您可以使用 renderer.sharedMaterial.SetFloat() 方法从代码中操作 alpha 值。

Something like this:像这样的东西:

IEnumerator SetTransparencyLevel(bool fullyTransparent)
{
for (var i = renderer.material.GetFloat("Alpha"); fullyTransparent && i > 0 || !fullyTransparent && i < 1; i+=0.05f)
{
renderer.material.SetFloat("Alpha", i);
yield return new WaitForEndOfFrame();
}
}

I didnt test this code, but it shows the way.我没有测试这段代码,但它显示了方式。

I have been doing similar stuff for an experimental 2D game recently.我最近一直在为一个实验性的 2D 游戏做类似的事情。 I found that the best way to do it is by using a really neat free library - LeanTween.我发现最好的方法是使用一个非常简洁的免费库 - LeanTween。 You can use it on normal game objects and Unity UI elements.您可以在普通游戏对象和 Unity UI 元素上使用它。 This is how a fade-out will look like.这就是淡出的样子。

LeanTween.alpha (gameObject, 0.0f, 1.0f);

The first parameter should be the game object you want to fade out, the second is the alpha value you're aiming for (0.0f will fade out the object completely), and the last one is the duration of the animation.第一个参数应该是你想要淡出的游戏对象,第二个是你想要的 alpha 值(0.0f 将完全淡出对象),最后一个是动画的持续时间。

You could also use a CanvasGroup and change the alfa with Coroutines.您还可以使用 CanvasGroup 并使用 Coroutines 更改 alfa。 I think is an easy aproach.我认为这是一个简单的方法。

This is what I do in my games:这就是我在游戏中所做的:

    public class Fader : MonoBehaviour {
        private CanvasGroup _canvasGroup;
        private Coroutine _currentActiveFade;

        private void Awake() {
            _canvasGroup = GetComponent<CanvasGroup>();
        }

        public void FadeOutImmediately() => _canvasGroup.alpha = 1;

        public Coroutine FadeOut(float time) => Fade(1, time);

        public Coroutine FadeIn(float time) => Fade(0, time);

        private Coroutine Fade(float target, float time) {
            if (_currentActiveFade != null) StopCoroutine(_currentActiveFade);
            _currentActiveFade = StartCoroutine(FadeRoutine(target, time));
            
            return _currentActiveFade;
        }

        private IEnumerator FadeRoutine(float target, float time) {
            while (!Mathf.Approximately(_canvasGroup.alpha, target)) {
                _canvasGroup.alpha = Mathf.MoveTowards(_canvasGroup.alpha, target, Time.deltaTime / time);
                yield return null;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM