unity 不规则ui进度条带有圆头效果
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UniRx;
using UnityEngine.UI;
public class Temperature : MonoBehaviour
{
public enum TempType
{
///
/// 电机
///
Motor,
/// <summary>
/// 电池
/// </summary>
Battery
}
[SerializeField] private TempType m_TempType;
[SerializeField] private TMP_Text Text_MotorCurrent;
[SerializeField] private TMP_Text Text_MotorHighest;
[SerializeField] private Image Image_Current;
[SerializeField] private Image Image_Highest;
[SerializeField] private FillAmountPoint _currentFAP;
[SerializeField] private FillAmountPoint _highestFAP;
[SerializeField] private ImageControl _currentIC;
// 起点和终点的填充值
private float fillRangeMin = 0.07f;
private float fillRangeMax = 0.94f;
private static readonly string TAG = "Temperature";
// Start is called before the first frame update
void Start()
{
if (m_TempType == TempType.Motor)
{
DataCenter.MotorTemperature.Subscribe(f =>
{
if (f == DataCenter.UNDEFIND)
{
Text_MotorCurrent.text = "-.-<size=26>\u2103</size>";
Image_Current.fillAmount = 0;
SetImageValue(Image_Current,0);
}
else
{
if (DataCenter.MotorTemperature_Max.Value < f)
DataCenter.MotorTemperature_Max.Value = f;
float showValue = BM.Snake.Utils.GetLimitValue(f, -30f, 150f);
Text_MotorCurrent.text = $"{showValue:F1}<size=26>\u2103</size>";
// 温度范围-30~150
SetImageValue(Image_Current, (f + 30f) / 180f);
}
}).AddTo(this);
DataCenter.MotorTemperature_Max.Subscribe(f =>
{
if (f == DataCenter.UNDEFIND)
{
Text_MotorHighest.text = "-.-<size=26>\u2103</size>";
Image_Highest.fillAmount = 0;
SetImageValue(Image_Highest, 0);
}
else
{
float showValue = BM.Snake.Utils.GetLimitValue(f, -30f, 150f);
Text_MotorHighest.text = $"{showValue:F1}\u2103";
// 温度范围-30~150
SetImageValue(Image_Highest, (f + 30f) / 180f);
}
}).AddTo(this);
}
else
{
DataCenter.BatteryTemperature.Subscribe(f =>
{
if (f == DataCenter.UNDEFIND)
{
Text_MotorCurrent.text = "-.-<size=26>\u2103</size>";
Image_Current.fillAmount = 0;
SetImageValue(Image_Current, 0);
}
else
{
if (DataCenter.BatteryTemperature_Max.Value < f)
DataCenter.BatteryTemperature_Max.Value = f;
float showValue = BM.Snake.Utils.GetLimitValue(f, -30f, 60f);
Text_MotorCurrent.text = $"{showValue:F1}<size=26>\u2103</size>";
// 温度范围-30~60
SetImageValue(Image_Current, (f + 30f) / 90f);
}
}).AddTo(this);
DataCenter.BatteryTemperature_Max.Subscribe(f =>
{
if (f == DataCenter.UNDEFIND)
{
Text_MotorHighest.text = "-.-<size=26>\u2103</size>";
Image_Highest.fillAmount = 0;
SetImageValue(Image_Highest,0);
}
else
{
float showValue = BM.Snake.Utils.GetLimitValue(f, -30f, 60f);
Text_MotorHighest.text = $"{showValue:F1}\u2103";
SetImageValue(Image_Highest, (f + 30f) / 90f);
}
}).AddTo(this);
}
}
private void SetImageValue(Image image, float value)
{
float pre = Mathf.Lerp(fillRangeMin, fillRangeMax, value);
if (Image_Highest == image)
{
if (_highestFAP) { _highestFAP.SetPreImage(pre); }
}
else if (Image_Current == image)
{
if (_currentFAP) { _currentFAP.SetPreImage(pre); }
if (_currentIC) { _currentIC.SetPreImage(pre); }
}
image.fillAmount = pre;
}
}