MIF_E31230902/Assets/Asset/_Main/Script/Interactable/InteractableView.cs

74 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Unity.VisualScripting;
public class InteractableView : MonoBehaviour
{
[SerializeField] private CanvasGroup canvasGroup;
Coroutine co_fade;
private void Start()
{
canvasGroup.gameObject.SetActive(false);
canvasGroup.alpha = 0f;
}
public void ShowScreen()
{
if(co_fade != null)
{
StopCoroutine(co_fade);
co_fade = null;
}
co_fade = StartCoroutine(OnFade(1f, 0.5f));
}
public void HideScreen()
{
if (co_fade != null)
{
StopCoroutine(co_fade);
co_fade = null;
}
co_fade = StartCoroutine(OnFade(0f, 0.5f));
}
private IEnumerator OnFade(float targetAlpha, float duration)
{
float startValue = canvasGroup.alpha;
float currentTime = 0f;
if(targetAlpha == 1)
{
canvasGroup.gameObject.SetActive(true);
}
while(currentTime < duration)
{
currentTime += Time.deltaTime;
canvasGroup.alpha = Mathf.Lerp(startValue, targetAlpha, currentTime/duration);
yield return null;
}
canvasGroup.alpha = targetAlpha;
if(canvasGroup.alpha == 0)
{
canvasGroup.gameObject.SetActive(false);
}
co_fade = null;
yield break;
}
}