유니티

[유니티] IEnumrator를 사용한 스토리 컷 편집하는 법

유니티 게임 개발 2025. 2. 27. 22:16
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class CutSceneManager : MonoBehaviour
{
    public Image[] images;
    public Text[] texts;

    [SerializeField]
    private float fadeDuration;

    float cutTime = 0;

    private void Start()
    {
        foreach (Image image in images)
        {
            Color imageColor = image.color;
            imageColor.a = 0f;
            image.color = imageColor;
        }

        foreach (Text text in texts)
        {
            Color textColor = text.color;
            textColor.a = 0f;
            text.color = textColor;
        }
        
        StartCoroutine(FadeCut());
    }

    IEnumerator FadeCut()
    {
        for (int i = 0; i < images.Length; i++)
        {
            StartCoroutine(FadeIn(images[i], texts[i]));
            yield return new WaitForSeconds(fadeDuration + 4);
            StartCoroutine(FadeOut(images[i], texts[i]));
            yield return new WaitForSeconds(fadeDuration);
        }
    }

    IEnumerator FadeIn(Image image, Text text)
    {
        Color imageColor = image.color;
        Color textColor = text.color;

        while (cutTime <= fadeDuration)
        {
            cutTime += Time.deltaTime;
            imageColor.a = cutTime / fadeDuration;
            image.color = imageColor;
            textColor.a = cutTime / fadeDuration;
            text.color = textColor;
            yield return null;
        }

        cutTime = 0f;
    }

    IEnumerator FadeOut(Image image, Text text)
    {
        Color imageColor = image.color;
        Color textColor = text.color;

        while (cutTime <= fadeDuration)
        {
            cutTime += Time.deltaTime;
            imageColor.a = 1 - cutTime / fadeDuration;
            image.color = imageColor;
            textColor.a = 1 - cutTime / fadeDuration;
            text.color = textColor;
            yield return null;
        }

        cutTime = 0f;
    }
}

 

스토리 컷 편집을 유니티에서 보여줄 때,

일반적으로 사용하는 방법은 스타트 코루틴과 이넘레이터를 사용해서 구현한다.