Files
XRLib/Assets/Scripts/UVC/Extention/ImageEx.cs
2025-06-04 23:10:11 +09:00

99 lines
3.6 KiB
C#

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