111 lines
2.7 KiB
C#
111 lines
2.7 KiB
C#
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
public class Toast : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (toast!=null)
|
|
{
|
|
//toast.SetActive(false);
|
|
//txt = toast.transform.Find("Toast").GetComponent<Text>();
|
|
}
|
|
//if (txt != null)
|
|
//{
|
|
// txt.enabled = false;
|
|
//}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
[SerializeField] GameObject toast;
|
|
[SerializeField] Text txt;
|
|
|
|
public void showToast(Assets.Scripts.Utils.ToastParams json)
|
|
{
|
|
gameObject.SetActive(true);
|
|
if (json.func == null)
|
|
{
|
|
json.func = () => false;
|
|
}
|
|
StartCoroutine(showToastCOR(json.text, json.duration,json.func,json.showSeconds,json.endCallback));
|
|
}
|
|
|
|
private IEnumerator showToastCOR(string text,
|
|
int duration, Func<bool> action,bool showSeconds,Action endCallback)
|
|
{
|
|
|
|
gameObject.SetActive(true);
|
|
if (showSeconds)
|
|
{
|
|
txt.text = text + $"({duration}s)";
|
|
}
|
|
else
|
|
{
|
|
txt.text = text;
|
|
}
|
|
//Fade in
|
|
yield return fadeInAndOut(txt, true, 0.5f);
|
|
|
|
//Wait for the duration
|
|
float counter = 0,timer = 1f;
|
|
int seconds = 0;
|
|
while (counter < duration && !action.Invoke())
|
|
{
|
|
counter += Time.deltaTime;
|
|
if (showSeconds)
|
|
{
|
|
timer -= Time.deltaTime;
|
|
if (timer < 0)
|
|
{
|
|
txt.text = text + $"({duration - (++seconds)}s)";
|
|
timer += 1f;
|
|
}
|
|
}
|
|
yield return null;
|
|
}
|
|
if (endCallback != null)
|
|
{
|
|
endCallback.Invoke();
|
|
}
|
|
//Fade out
|
|
yield return fadeInAndOut(txt, false, 0.5f);
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
IEnumerator fadeInAndOut(Text targetText, bool fadeIn, float duration)
|
|
{
|
|
//Set Values depending on if fadeIn or fadeOut
|
|
float a, b;
|
|
if (fadeIn)
|
|
{
|
|
a = 0f;
|
|
b = 1f;
|
|
}
|
|
else
|
|
{
|
|
a = 1f;
|
|
b = 0f;
|
|
}
|
|
|
|
float counter = 0f;
|
|
|
|
while (counter < duration)
|
|
{
|
|
counter += Time.deltaTime;
|
|
float alpha = Mathf.Lerp(a, b, counter / duration);
|
|
transform.GetComponent<CanvasGroup>().alpha = alpha;
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|