63 lines
1.6 KiB
C#
63 lines
1.6 KiB
C#
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BlueWater
|
|
{
|
|
public class CenterSlider : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private RectTransform _fillRect;
|
|
|
|
[SerializeField]
|
|
private Image _fill;
|
|
|
|
[SerializeField]
|
|
private RectTransform _handleRect;
|
|
|
|
[SerializeField]
|
|
private float _minValue;
|
|
|
|
[SerializeField]
|
|
private float _maxValue = 1f;
|
|
|
|
[SerializeField]
|
|
private float _value;
|
|
|
|
[SerializeField]
|
|
private Color _minColor = Color.white;
|
|
|
|
[SerializeField]
|
|
private Color _maxColor = Color.white;
|
|
|
|
private readonly float _centerAnchorX = 0.5f;
|
|
|
|
private void Start()
|
|
{
|
|
SetValue(_value);
|
|
}
|
|
|
|
private void UpdateSlider(float value)
|
|
{
|
|
// 0 ~ 1값
|
|
var normalizedValue = (value - _minValue) / (_maxValue - _minValue);
|
|
|
|
_fill.color = normalizedValue < 0.5f ? _minColor : _maxColor;
|
|
|
|
_handleRect.anchorMin = new Vector2(normalizedValue, 0f);
|
|
_handleRect.anchorMax = new Vector2(normalizedValue, 1f);
|
|
|
|
_fillRect.anchorMin = new Vector2(Mathf.Clamp(_handleRect.anchorMin.x, 0, _centerAnchorX), 0);
|
|
_fillRect.anchorMax = new Vector2(Mathf.Clamp(_handleRect.anchorMin.x, _centerAnchorX, 1), 1);
|
|
}
|
|
|
|
[Button("값 추가")]
|
|
public void SetValue(float value)
|
|
{
|
|
var newValue = Mathf.Clamp(value, _minValue, _maxValue);
|
|
_value = newValue;
|
|
UpdateSlider(_value);
|
|
}
|
|
}
|
|
}
|