2026-05-06 15:07:56 +02:00

93 lines
2.7 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
using DuloGames.UI.Tweens;
namespace DuloGames.UI
{
public class Test_UIBulletBar : MonoBehaviour
{
public enum TextVariant
{
Percent,
Value,
ValueMax
}
public UIBulletBar bar;
public float Duration = 5f;
public TweenEasing Easing = TweenEasing.InOutQuint;
public Text m_Text;
public TextVariant m_TextVariant = TextVariant.Percent;
public int m_TextValue = 100;
// Tween controls
[NonSerialized]
private readonly TweenRunner<FloatTween> m_FloatTweenRunner;
// Called by Unity prior to deserialization,
// should not be called by users
protected Test_UIBulletBar()
{
if (this.m_FloatTweenRunner == null)
this.m_FloatTweenRunner = new TweenRunner<FloatTween>();
this.m_FloatTweenRunner.Init(this);
}
protected void OnEnable()
{
if (this.bar == null)
return;
this.StartTween(0f, (this.bar.fillAmount * this.Duration));
}
protected void SetFillAmount(float amount)
{
if (this.bar == null)
return;
this.bar.fillAmount = amount;
if (this.m_Text != null)
{
if (this.m_TextVariant == TextVariant.Percent)
{
this.m_Text.text = Mathf.RoundToInt(amount * 100f).ToString() + "%";
}
else if (this.m_TextVariant == TextVariant.Value)
{
this.m_Text.text = Mathf.RoundToInt((float)this.m_TextValue * amount).ToString();
}
else if (this.m_TextVariant == TextVariant.ValueMax)
{
this.m_Text.text = Mathf.RoundToInt((float)this.m_TextValue * amount).ToString() + "/" + this.m_TextValue;
}
}
}
protected void OnTweenFinished()
{
if (this.bar == null)
return;
this.StartTween((this.bar.fillAmount == 0f ? 1f : 0f), this.Duration);
}
protected void StartTween(float targetFloat, float duration)
{
if (this.bar == null)
return;
var floatTween = new FloatTween { duration = duration, startFloat = this.bar.fillAmount, targetFloat = targetFloat };
floatTween.AddOnChangedCallback(SetFillAmount);
floatTween.AddOnFinishCallback(OnTweenFinished);
floatTween.ignoreTimeScale = true;
floatTween.easing = this.Easing;
this.m_FloatTweenRunner.StartTween(floatTween);
}
}
}