본문 바로가기

유니티/유니티 엔진

[유니티] Color, 오브젝트 페이드 아웃(FadeOut), 페이드 인(FadeIn)

728x90
반응형

Color

 

Renderer 컴포넌트를 통해서 오브젝트의 color에 접근할 수 있다.

 

color에는 R, G, B, A 값이 존재하며 RGB는 색상, A는 투명도를 의미한다.

 

또한 유니티에서는 다음과 같은 색상과 관련된 정적 변수를 제공하고 있다.

 

 

 

 

이를 활용하여 오브젝트의 색상을 변경하는 코드를 작성해 볼 수 있다.

 

Renderer MyRenderer;

void Start()
{
    MyRenderer = gameObject.GetComponent<Renderer>();
}

void Update()
{
    if (Input.GetKeyDown(KeyCode.R))
        MyRenderer.material.color = Color.red;
    if (Input.GetKeyDown(KeyCode.G))
        MyRenderer.material.color = Color.green;
    if (Input.GetKeyDown(KeyCode.B))
        MyRenderer.material.color = new Color(0, 0, 1, 1);
}

 

 

위와 같이 작성하면 r, g, b를 입력했을 때 각각 빨간색, 초록색, 파란색으로 오브젝트의 색상이 변경되는 것을 확인할 수 있다.

 

 

 

 

 

페이드 아웃 (FadeOut)

 

우리는 Color 의 알파값, 즉 투명도를 이용하여 오브젝트의 페이드 아웃을 구현할 수 있다.

 

Renderer MyRenderer;

IEnumerator FadeOut()
{
    float f = 1;
    while (f > 0)
    {
        f -= 0.1f;
        Color ColorAlhpa = MyRenderer.material.color;
        ColorAlhpa.a = f;
        MyRenderer.material.color = ColorAlhpa;
        yield return new WaitForSeconds(0.02f);
    }
}

 

코루틴을 사용하여 점점 오브젝트의 알파값을 낮추면 페이드 아웃 효과를 줄 수 있다.

 

 

 

 

오브젝트에 실제로 적용시켜보면 다음과 같다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class EnemyGenerate : MonoBehaviour
{
    Renderer MyRenderer;

    IEnumerator FadeOut()
    {
        float f = 1;
        while (f > 0)
        {
            f -= 0.1f;
            Color ColorAlhpa = MyRenderer.material.color;
            ColorAlhpa.a = f;
            MyRenderer.material.color = ColorAlhpa;
            yield return new WaitForSeconds(0.02f);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        MyRenderer = gameObject.GetComponent<Renderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) && MyRenderer.material.color.a>0)
            StartCoroutine(FadeOut());
    }
}

 

a를 입력하면 오브젝트가 페이드 아웃되는 것을 확인할 수 있다.

 

 

 

 

 

 

페이드 인(FadeIn)

 

페이드 인도 페이드 아웃과 같은 원리로 구현할 수 있다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class EnemyGenerate : MonoBehaviour
{
    Renderer MyRenderer;

    IEnumerator FadeIn()
    {
        float f = 0;
        while (f <= 1)
        {
            f += 0.1f;
            Color ColorAlhpa = MyRenderer.material.color;
            ColorAlhpa.a = f;
            MyRenderer.material.color = ColorAlhpa;
            yield return new WaitForSeconds(0.02f);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        MyRenderer = gameObject.GetComponent<Renderer>();
        MyRenderer.material.color = new Color(1,1,1,0);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) && MyRenderer.material.color.a <= 1)
            StartCoroutine(FadeIn());
    }
}

 

 

 

 

 

 

 

 

728x90
반응형