Files
tools/Unity C++脚本/NumberScrollMixerBehaviour.cs
2025-08-01 10:50:57 +08:00

61 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using TMPro;
public class NumberScrollPlayableBehaviour : PlayableBehaviour
{
public TMP_Text textMesh;
public int startValue;
public int endValue;
private double clipDuration;
public override void OnGraphStart(Playable playable)
{
clipDuration = playable.GetDuration();
}
public override void OnGraphStop(Playable playable)
{
if (textMesh != null)
{
textMesh.text = endValue.ToString();
}
}
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
if (textMesh != null)
{
textMesh.text = startValue.ToString();
}
}
public override void OnBehaviourPause(Playable playable, FrameData info)
{
if (textMesh != null && playable.GetTime() >= clipDuration)
{
textMesh.text = endValue.ToString();
}
}
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
if (textMesh == null)
{
return;
}
double normalizedTime = playable.GetTime() / clipDuration;
float currentValue = Mathf.Lerp(startValue, endValue, (float)normalizedTime);
textMesh.text = Mathf.FloorToInt(currentValue).ToString();
// Ensure the text is set to the end value if the normalized time is 1.0 or more
if (normalizedTime >= 1.0)
{
textMesh.text = endValue.ToString();
}
}
}