유니티 자체 코드 작성
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 (HOTween v2) | 애니메이션 도구 | Unity Asset Store
Use the DOTween (HOTween v2) tool from Demigiant on your next project. Find this & more animation tools on the Unity Asset Store.
assetstore.unity.com
도트윈 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));
}
}
}
참고할만한 글
유니티 C# 충돌 처리 OnTrigger Enter, Stay, Exit 간단 사용법
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "Player") { Debug.Log("플레이어와 접
parksh3641.tistory.com
유니티 C# 버튼 리스너 onClick AddListener 추가 간단 사용법
코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { public Button[] buttons; private void Start() { for(int i = 0; i < buttons.Length; i ++) //여러 버
parksh3641.tistory.com