using UnityEngine;
using UnityEngine.UI;
namespace UVC.Extension
{
///
/// Unity UI Image 컴포넌트를 위한 확장 메소드 모음입니다.
///
public static class ImageEx
{
///
/// Image 컴포넌트의 알파값을 설정합니다.
///
/// 알파값을 변경할 Image 컴포넌트
/// 적용할 알파값 (0.0f ~ 1.0f)
///
///
/// // Image 컴포넌트의 알파값을 0.5로 설정
/// Image myImage = GetComponent();
/// myImage.SetAlpha(0.5f);
///
/// // 또는 한 줄로 체이닝 가능
/// GetComponent().SetAlpha(0.5f);
///
///
public static void SetAlpha(this Image image, float alpha)
{
Color tempColor = image.color;
tempColor.a = alpha;
image.color = tempColor;
}
///
/// Image 컴포넌트에 페이드 인(투명 → 불투명) 효과를 적용합니다.
///
/// 페이드 효과를 적용할 Image 컴포넌트
/// 페이드 효과 지속 시간(초)
/// 페이드 인 목표 알파값 (기본값: 1.0f)
/// 코루틴으로 사용할 수 있는 IEnumerator
///
///
/// // 기본 사용법 (알파값 1.0까지 페이드 인)
/// StartCoroutine(myImage.FadeIn(1.0f));
///
/// // 알파값 0.8까지만 페이드 인
/// StartCoroutine(myImage.FadeIn(1.0f, 0.8f));
///
///
public static System.Collections.IEnumerator FadeIn(this Image image, float duration, float targetAlpha = 1.0f)
{
float startAlpha = image.color.a;
float time = 0;
while (time < duration)
{
time += Time.deltaTime;
float alpha = Mathf.Lerp(startAlpha, targetAlpha, time / duration);
image.SetAlpha(alpha);
yield return null;
}
image.SetAlpha(targetAlpha);
}
///
/// Image 컴포넌트에 페이드 아웃(불투명 → 투명) 효과를 적용합니다.
///
/// 페이드 효과를 적용할 Image 컴포넌트
/// 페이드 효과 지속 시간(초)
/// 페이드 아웃 목표 알파값 (기본값: 0.0f)
/// 코루틴으로 사용할 수 있는 IEnumerator
///
///
/// // 기본 사용법 (알파값 0.0까지 페이드 아웃)
/// StartCoroutine(myImage.FadeOut(1.0f));
///
/// // 알파값 0.2까지만 페이드 아웃
/// StartCoroutine(myImage.FadeOut(1.0f, 0.2f));
///
///
public static System.Collections.IEnumerator FadeOut(this Image image, float duration, float targetAlpha = 0.0f)
{
float startAlpha = image.color.a;
float time = 0;
while (time < duration)
{
time += Time.deltaTime;
float alpha = Mathf.Lerp(startAlpha, targetAlpha, time / duration);
image.SetAlpha(alpha);
yield return null;
}
image.SetAlpha(targetAlpha);
}
}
}