본문 바로가기
개발/C#

유니티 C# 메테리얼 알파값 간단 변경법 Unity Material Alpha

by SPNK 2022. 7. 5.
반응형

Material 의 SurfaceType 이 Transparent 이어야 알파값을 적용할 수 있습니다.

URP 기준 Material를 UI/Unlit/Transparent로 생성후 Tint의 Alpha 값을 변경하면 됩니다.

 

  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour
{
    Material material;
    
    float alpha = 0;

    private void Awake()
    {
        material = GetComponent<Material>();
        
        material.color = new Color(color.r, color.g, color.b, alpha / 255);
    }
}

 

  • 일정 주기로 깜빡거리게 하기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour
{
    public Color color;
    Material material;

    public float changeSpeed = 60;

    float tt = 0;
    bool change = false;

    private void Awake()
    {
        material = GetComponent<Material>();
    }

    private void OnEnable()
    {
        StartCoroutine(ChangeAlphaCoroution());
    }

    private void OnDisable()
    {
        StopAllCoroutines();
    }


    IEnumerator ChangeAlphaCoroution()
    {
        while(true)
        {
            if (!change)
            {
                tt += 1;

                if (tt >= changeSpeed)
                {
                    change = true;
                }
            }
            else
            {
                tt -= 1;

                if (tt <= 0)
                {
                    change = false;
                }
            }

            material.color = new Color(color.r, color.g, color.b, tt / changeSpeed);

            yield return new WaitForSeconds(0.01f);
        }
    }
}
반응형

댓글