Unity 不规则进度条显示根据点对点进行
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class SpeedVehicle : MonoBehaviour
{
//加速踏板
[SerializeField] private Image AcceleratorApertureSlider;
//制动踏板
[SerializeField] private Image BrakingPedalApertureSlider;
private static readonly string TAG = "SpeedVehicle";
private void Start()
{
//当前加速踏板进度
DataCenter.AcceleratorAperture.Subscribe(f =>
{
Log.I(TAG, "AcceleratorAperture select value: " + f);
float fillAmount = f / 100f;
AcceleratorApertureSlider.fillAmount = fillAmount;
//SetAnimationFrame(AcceleratorApertureSlider, f * 100);
AcceleratorAperture(fillAmount);
}).AddTo(this);
//当前制动踏板进度
DataCenter.BrakingPedalAperture.Subscribe(f =>
{
Log.I(TAG, "BrakingPedalAperture select value: " + f);
float fillAmount = Normalize(f, 10, 45);
BrakingPedalApertureSlider.fillAmount = fillAmount;
//SetAnimationFrame(BrakingPedalApertureSlider, f * 100);
BrakingPedalAperture(fillAmount);
}).AddTo(this);
}
protected virtual void AcceleratorAperture(float fillAmount) { }
protected virtual void BrakingPedalAperture(float fillAmount) { }
private float Normalize(float value, float min, float max)
{
return (value - min) / (max - min);
}
private void SetAnimationFrame(Animation animation, float frameToFreeze)
{
// 第N帧对应的时间(秒)
float timeAtNthFrame = frameToFreeze / 25f;
animation.clip.SampleAnimation(animation.gameObject, timeAtNthFrame);
}
}