142 lines
3.9 KiB
C#
142 lines
3.9 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Localization.Settings;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class CircularProgress : MonoBehaviour
|
|
{
|
|
[SerializeField] private int currentValue;
|
|
|
|
[SerializeField] private int maxValue = 10;
|
|
|
|
[SerializeField] private float startAngle;
|
|
|
|
[SerializeField] private float endAngle;
|
|
|
|
[SerializeField] private Transform background;
|
|
|
|
[SerializeField] private Transform foreground;
|
|
|
|
[SerializeField] private SpriteRenderer bgTemplate;
|
|
|
|
[SerializeField] private SpriteRenderer fgTemplate;
|
|
|
|
[SerializeField] private TMP_Text tmpText;
|
|
|
|
[SerializeField] private UnityEvent<int> onValueChanged;
|
|
|
|
private readonly List<Transform> _bgList = new();
|
|
private readonly List<Transform> _fgList = new();
|
|
|
|
public int CurrentValue
|
|
{
|
|
get => currentValue;
|
|
set
|
|
{
|
|
if (currentValue == value)
|
|
{
|
|
return;
|
|
}
|
|
|
|
currentValue = value;
|
|
OnValueChanged();
|
|
UpdateGUI();
|
|
}
|
|
}
|
|
|
|
private float NormalizedValue
|
|
{
|
|
get => (float)currentValue / maxValue;
|
|
set => CurrentValue = Mathf.RoundToInt(value * maxValue);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
CreateList(_bgList, background, bgTemplate);
|
|
CreateList(_fgList, foreground, fgTemplate);
|
|
OnValueChanged();
|
|
}
|
|
|
|
private void OnValueChanged()
|
|
{
|
|
if (currentValue < 0)
|
|
{
|
|
currentValue = 0;
|
|
}
|
|
|
|
if (maxValue < 0)
|
|
{
|
|
maxValue = 10;
|
|
}
|
|
|
|
if (currentValue > maxValue)
|
|
{
|
|
currentValue = maxValue;
|
|
}
|
|
|
|
onValueChanged?.Invoke(currentValue);
|
|
UpdateGUI();
|
|
}
|
|
|
|
void CreateList(List<Transform> list, Transform root, SpriteRenderer template)
|
|
{
|
|
template.gameObject.SetActive(false);
|
|
float angle = (startAngle - endAngle) / maxValue;
|
|
for (int i = 0; i < maxValue; i++)
|
|
{
|
|
Transform item = CreateItem(root, template, i);
|
|
list.Add(item);
|
|
item.RotateAround(root.position, Vector3.forward, startAngle - angle * i);
|
|
}
|
|
}
|
|
|
|
Transform CreateItem(Transform root, SpriteRenderer template, int index)
|
|
{
|
|
var obj = Instantiate(template.gameObject, root);
|
|
obj.gameObject.SetActive(true);
|
|
obj.gameObject.name = "item" + (index + 1);
|
|
|
|
return obj.transform;
|
|
}
|
|
|
|
private void UpdateGUI()
|
|
{
|
|
UpdateForeground();
|
|
UpdateText();
|
|
}
|
|
|
|
private void UpdateForeground()
|
|
{
|
|
for (int i = 0; i < _fgList.Count; i++)
|
|
{
|
|
Transform rectTrans = _fgList[i];
|
|
SpriteRenderer image = rectTrans.GetComponent<SpriteRenderer>();
|
|
|
|
rectTrans.gameObject.SetActive(i < currentValue);
|
|
|
|
image.color = i < maxValue / 2
|
|
? Color.Lerp(Color.green, Color.yellow, 2 * i / (float)maxValue)
|
|
: Color.Lerp(Color.yellow, Color.red, 2 * (i - (float)maxValue / 2) / maxValue);
|
|
}
|
|
}
|
|
|
|
private void UpdateText()
|
|
{
|
|
if (tmpText == null) return;
|
|
|
|
tmpText.text = LocalizationSettings.SelectedLocale.Identifier.Code == "en"
|
|
? $"{NormalizedValue * 25000 :0} RPM"
|
|
: $"{NormalizedValue * 25000 :0} 转";
|
|
|
|
Debug.Log(tmpText.text);
|
|
}
|
|
|
|
public void OnSpeedChanged(float originSpeed, float speedScale)
|
|
{
|
|
NormalizedValue = originSpeed;
|
|
}
|
|
}
|
|
} |