chicken_dy/Assets/HCMiniSdk/Demo/Scripts/Views/HCToast.cs

55 lines
1.2 KiB
C#
Raw Normal View History

2024-03-07 12:12:48 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace HC
{
public class HCToast : MonoBehaviour
{
private static HCToast instance;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
}
}
private void Start()
{
instance.gameObject.SetActive(false);
}
public static HCToast Instance => instance;
public Text toastText;
private const float ToastDuration = 2f;
public static void ShowToast(string message)
{
instance._ShowToast(message);
}
private void _ShowToast(string message)
{
instance.gameObject.SetActive(true);
StartCoroutine(ShowAndHideToast(message));
}
private IEnumerator ShowAndHideToast(string message)
{
toastText.text = message;
instance.gameObject.SetActive(true);
yield return new WaitForSeconds(ToastDuration);
instance.gameObject.SetActive(false);
}
}
}