149 lines
4.0 KiB
C#
Raw Normal View History

2024-12-25 16:33:45 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
namespace QFramework.Example
{
public class UITimeTipData : UIPanelData
{
public float time;
public string label;
public List<ValueItem> values = new List<ValueItem>();
public string finishedEvent;
public bool needClick = false;
public bool reverse = false;
public string format;
public class ValueItem
{
public float curValue;
public float startValue;
public float endValue;
public ValueItem(float startValue, float endValue)
{
this.curValue = startValue;
this.startValue = startValue;
this.endValue = endValue;
}
}
}
public partial class UITimeTip : UIPanel
{
public List<Sprite> sprites;
bool isRun = false;
List<TweenerCore<float, float, FloatOptions>> dotwens = new List<TweenerCore<float, float, FloatOptions>>();
2025-01-08 17:50:52 +08:00
TweenerCore<int, int, NoOptions> imgTwen;
2024-12-25 16:33:45 +08:00
int index = 0;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITimeTipData ?? new UITimeTipData();
// please add init code here
2025-01-09 09:43:15 +08:00
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
2024-12-25 16:33:45 +08:00
}
private void Update()
{
if (isRun)
{
object[] objects = new object[mData.values.Count];
for (int i = 0; i < mData.values.Count; i++)
{
if (string.IsNullOrEmpty(mData.format) == false)
{
objects[i] = string.Format(mData.format, mData.values[i].curValue);
}
else
{
objects[i] = mData.values[i].curValue;
}
}
Label.text = string.Format(mData.label, objects);
}
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITimeTipData ?? new UITimeTipData();
index = 0;
isRun = true;
2025-01-08 17:50:52 +08:00
int endIndex = sprites.Count - 1;
2024-12-25 16:33:45 +08:00
if (mData.reverse)
{
index = sprites.Count;
endIndex = 0;
}
2025-01-08 17:50:52 +08:00
imgTwen = DOTween.To(() => index, v =>
2024-12-25 16:33:45 +08:00
{
index = v;
Img.sprite = sprites[index];
Img.SetNativeSize();
2025-01-08 17:50:52 +08:00
}, endIndex, mData.time);
imgTwen.SetEase(Ease.Linear).onComplete = () =>
2024-12-25 16:33:45 +08:00
{
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
{
StringEventSystem.Global.Send(mData.finishedEvent);
}
if (mData.needClick == true)
{
Bg.onClick.RemoveAllListeners();
Bg.onClick.AddListener(Hide);
}
else
{
Hide();
}
isRun = false;
};
foreach (var item in mData.values)
{
dotwens.Add(DOTween.To(() => item.curValue, x =>
{
item.curValue = x;
}, item.endValue, mData.time));
}
}
protected override void OnShow()
{
}
protected override void OnHide()
{
foreach (var item in dotwens)
{
item.Kill();
}
dotwens.Clear();
2025-01-08 17:50:52 +08:00
if (imgTwen != null)
{
imgTwen.Kill();
imgTwen = null;
}
2024-12-25 16:33:45 +08:00
}
protected override void OnClose()
{
}
}
}