반응형
유니티 자체 코드 작성
using UnityEngine;
using System.Collections;
public class FadeInOut : MonoBehaviour
{
public float fadeSpeed = 1.5f;
public bool fadeInOnStart = true;
public bool fadeOutOnExit = true;
private CanvasGroup canvasGroup;
void Start()
{
canvasGroup = GetComponent<CanvasGroup>();
if (fadeInOnStart)
{
canvasGroup.alpha = 0f;
StartCoroutine(FadeIn());
}
}
IEnumerator FadeIn()
{
while (canvasGroup.alpha < 1)
{
canvasGroup.alpha += Time.deltaTime * fadeSpeed;
yield return null;
}
}
IEnumerator FadeOut()
{
while (canvasGroup.alpha > 0)
{
canvasGroup.alpha -= Time.deltaTime * fadeSpeed;
yield return null;
}
}
}
Dotween Asset 사용 (무료)
도트윈 Dotween 으로 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class FadeInOut : MonoBehaviour
{
public CanvasGroup canvasGroup;
void Awake()
{
canvasGroup.gameObject.SetActive(false);
}
public void FadeIn() //페이드 인 사용
{
StartCoroutine(Fade(true));
}
public void FadeOut() //페이드 아웃 사용
{
StartCoroutine(Fade(false));
}
private IEnumerator Fade(bool isFadeIn)
{
if (isFadeIn)
{
canvasGroup.alpha = 1;
Tween tween = canvasGroup.DOFade(0f, 1f);
yield return tween.WaitForCompletion();
canvasGroup.gameObject.SetActive(false);
}
else
{
canvasGroup.alpha = 0;
canvasGroup.gameObject.SetActive(true);
Tween tween = canvasGroup.DOFade(1f, 1f);
yield return tween.WaitForCompletion();
StartCoroutine(Fade(true));
}
}
}
참고할만한 글
반응형