272 lines
9.2 KiB
C#
272 lines
9.2 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Unity
|
|||
|
// Author : Kimch
|
|||
|
// Created : 2017-11-11
|
|||
|
//
|
|||
|
// Last Modified By : Kimch
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "ToastBox.View" company=""></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using DG.Tweening;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
namespace G.UI
|
|||
|
{
|
|||
|
partial class ToastBox
|
|||
|
{
|
|||
|
private class Toaster
|
|||
|
{
|
|||
|
public ToastWidget template;
|
|||
|
public readonly List<ToastWidget> _instances = new List<ToastWidget>();
|
|||
|
public readonly Queue<ToastInfo> _infoQueue = new Queue<ToastInfo>();
|
|||
|
|
|||
|
float _duration = 0.5f;
|
|||
|
float _timer;
|
|||
|
|
|||
|
int _infoMax = 8;
|
|||
|
int _showCount;
|
|||
|
|
|||
|
int _index;
|
|||
|
|
|||
|
System.Action<ToastInfo> _showAction;
|
|||
|
|
|||
|
public void SetIndex(int index)
|
|||
|
{
|
|||
|
_index = index;
|
|||
|
switch (index)
|
|||
|
{
|
|||
|
case 0:
|
|||
|
_infoMax = 3;
|
|||
|
_showAction = ShowAction;
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
_infoMax = 3;
|
|||
|
_showAction = ShowAction1;
|
|||
|
break;
|
|||
|
case 2:
|
|||
|
_infoMax = 3;
|
|||
|
_showAction = ShowAction2;
|
|||
|
break;
|
|||
|
case 3:
|
|||
|
_duration = 1.2f;
|
|||
|
_showAction = ShowAction3;
|
|||
|
break;
|
|||
|
case 4:
|
|||
|
_duration = 0.6f;
|
|||
|
_showAction = ShowAction4;
|
|||
|
break;
|
|||
|
case 6:
|
|||
|
_showAction = ShowAction6;
|
|||
|
break;
|
|||
|
case 7:
|
|||
|
_showAction = ShowAction7;
|
|||
|
break;
|
|||
|
case 8:
|
|||
|
_showAction = ShowAction8;
|
|||
|
break;
|
|||
|
case 10:
|
|||
|
_infoMax = 1;
|
|||
|
_showAction = ShowAction10;
|
|||
|
break;
|
|||
|
case 11:
|
|||
|
_infoMax = 1;
|
|||
|
_showAction = ShowAction10;
|
|||
|
break;
|
|||
|
default:
|
|||
|
_showAction = ShowAction;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
toast.transform.DOLocalMoveY(300f, 1.2f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction1(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
toast.transform.DOLocalMoveY(40f, 1.5f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction2(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetNumber(info.arg1 - info.arg2, info.arg1);
|
|||
|
((RectTransform)toast.transform).DOAnchorPosY(170, 1.8f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction3(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
toast.SetQuality(info.arg1);
|
|||
|
toast.SetIcon(info.iconInfo);
|
|||
|
toast.transform.DOLocalMoveY(300f, 1.8f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction4(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
((RectTransform)toast.transform).DOAnchorPosY(0, 1f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction6(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
toast.transform.localScale = Vector3.one;
|
|||
|
toast.transform.DOScale(1.5f, 0.8f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction7(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetFx(info.content);
|
|||
|
toast.transform.DOLocalMoveY(300f, 1.2f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
private void ShowAction8(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
toast.SetText2($"x {info.arg1}");
|
|||
|
toast.SetQuality(info.arg2);
|
|||
|
toast.SetIcon(info.iconInfo);
|
|||
|
|
|||
|
toast.transform.DOPunchScale(Vector3.one * 0.5f, 1f, 1, 1);
|
|||
|
toast.transform.DOLocalMoveY(300f, 1f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
private void ShowAction10(ToastInfo info)
|
|||
|
{
|
|||
|
var toast = GetToast();
|
|||
|
toast.SetText(info.content);
|
|||
|
toast.transform.DOLocalMoveX(215f, 0.8f).SetEase(Ease.OutQuart).OnComplete(() => OPenDelay(toast)).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
void OPenDelay(ToastWidget toast)
|
|||
|
{
|
|||
|
toast.transform.DOLocalMoveX(215f, 0.45f).SetEase(Ease.OutQuart).OnComplete(() => toast.transform.DOLocalMoveX(1000f, 0.8f).SetEase(Ease.OutQuart).OnComplete(() => ReturnToast(toast))).SetUpdate(true);
|
|||
|
}
|
|||
|
|
|||
|
public void AddInfo(ToastInfo info)
|
|||
|
{
|
|||
|
if (_infoQueue.Count > _infoMax)
|
|||
|
_infoQueue.Dequeue();
|
|||
|
_infoQueue.Enqueue(info);
|
|||
|
}
|
|||
|
|
|||
|
private ToastWidget GetToast()
|
|||
|
{
|
|||
|
ToastWidget result = null;
|
|||
|
int remain = _instances.Count;
|
|||
|
if (remain > 0)
|
|||
|
{
|
|||
|
result = _instances[remain - 1];
|
|||
|
_instances.RemoveAt(remain - 1);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
result = Object.Instantiate(template, template.transform.parent);
|
|||
|
result.index = _index;
|
|||
|
}
|
|||
|
result.gameObject.SetActive(true);
|
|||
|
result.transform.localPosition = template.transform.localPosition;
|
|||
|
result.transform.localRotation = template.transform.localRotation;
|
|||
|
result.transform.localScale = template.transform.localScale;
|
|||
|
result.transform.SetAsLastSibling();
|
|||
|
_showCount++;
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
private void ReturnToast(ToastWidget toast)
|
|||
|
{
|
|||
|
_showCount--;
|
|||
|
toast.gameObject.SetActive(false);
|
|||
|
_instances.Add(toast);
|
|||
|
}
|
|||
|
|
|||
|
public void Update()
|
|||
|
{
|
|||
|
if (_timer > 0f)
|
|||
|
_timer -= Time.unscaledDeltaTime;
|
|||
|
if (_timer <= 0f && _showCount < 3 && _infoQueue.Count > 0)
|
|||
|
{
|
|||
|
_timer = _duration;
|
|||
|
_showAction(_infoQueue.Dequeue());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region Field
|
|||
|
|
|||
|
private Toaster[] _templateInfos;
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Method
|
|||
|
|
|||
|
private void InitView()
|
|||
|
{
|
|||
|
var canvas = this.gameObject.GetComponent<Canvas>();
|
|||
|
canvas.overrideSorting = true;
|
|||
|
canvas.sortingOrder = 310;
|
|||
|
|
|||
|
_templateInfos = new Toaster[transform.childCount];
|
|||
|
for (int i = 0; i < _templateInfos.Length; i++)
|
|||
|
{
|
|||
|
var templateGO = transform.GetChild(i).gameObject;
|
|||
|
templateGO.SetActive(false);
|
|||
|
var templateItem = templateGO.AddComponent<ToastWidget>();
|
|||
|
|
|||
|
_templateInfos[i] = new Toaster
|
|||
|
{
|
|||
|
template = templateItem,
|
|||
|
};
|
|||
|
_templateInfos[i].SetIndex(i);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void RefreshView()
|
|||
|
{
|
|||
|
SetAsLastSibling();
|
|||
|
}
|
|||
|
|
|||
|
private void SetAsLastSibling()
|
|||
|
{
|
|||
|
if (gameObject)
|
|||
|
transform.SetAsLastSibling();
|
|||
|
}
|
|||
|
|
|||
|
private void UpdateView()
|
|||
|
{
|
|||
|
if (_templateInfos == null)
|
|||
|
return;
|
|||
|
|
|||
|
while (_infoQueue.Count > 0)
|
|||
|
{
|
|||
|
var info = _infoQueue.Dequeue();
|
|||
|
_templateInfos[info.type].AddInfo(info);
|
|||
|
SetAsLastSibling();
|
|||
|
}
|
|||
|
|
|||
|
foreach (var templateInfo in _templateInfos)
|
|||
|
{
|
|||
|
templateInfo.Update();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|