공부/Unity

[Unity] 전체 카메라 fade in / out 효과 (Post processing color adjustment)

굴러다니다니 2025. 9. 19. 16:04

다면 컨텐츠에서의 Fade in Fade out을 하고싶었다

Post processing의 color adjustment를 사용해 전체 화면자체에 post exposure 값을 조절한다

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using System.Collections;

public class VolumeController : MonoBehaviour
{
    [SerializeField] private Volume globalVolume; // 인스펙터에서 Global Volume 할당
    private ColorAdjustments colorAdjustments;

    [SerializeField] private float duration = 2f; // 변화하는 데 걸리는 시간 (초 단위)

    void Start()
    {
        if (globalVolume.profile.TryGet(out colorAdjustments))
        {
            // 시작할 때 -20에서 0으로 올라가도록 실행
            colorAdjustments.postExposure.value = -20f;
            StartCoroutine(CoExposure(-20f, 0f));
        }
        else
        {
            Debug.LogWarning("Color Adjustments override가 Volume Profile에 없음.");
        }
    }

    public IEnumerator CoExposure(float start, float end, bool isDouble = false)
    {
        float startValue = start;
        float endValue = end;
        float elapsed = 0f;

        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;
            float t = Mathf.Clamp01(elapsed / duration);
            colorAdjustments.postExposure.value = Mathf.Lerp(startValue, endValue, t);
            yield return null;
        }
        if (isDouble)
        {
            StartCoroutine(CoExposure(-20f, 0f));
        }
    }

    public void StartFadeOut()
    {
        StartCoroutine(CoExposure(0f, -20f));
    }

    public void StartWhiteFade()
    {
        StartCoroutine(CoFinishTimeline());
    }

    IEnumerator CoFinishTimeline()
    {
        yield return StartCoroutine(CoExposure(0f, -20f, true));
        yield return StartCoroutine(CoRestartTimeline());
    }

    private IEnumerator CoRestartTimeline()
    {
        yield return new WaitForSeconds(2f);
    }
}
728x90
반응형