You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
71
Assets/Scripts/Game/OtherComponent/HpSlider.cs
Normal file
71
Assets/Scripts/Game/OtherComponent/HpSlider.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
using Framework.GamePool;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Game.OtherComponent
|
||||
{
|
||||
public class HpSlider : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image _prefab;
|
||||
[SerializeField] private Sprite _activeImg;
|
||||
[SerializeField] private Sprite _unActiveImg;
|
||||
[SerializeField] private float _minValue = 0;
|
||||
[SerializeField] private float _maxValue = 100;
|
||||
private float _value;
|
||||
public int RateUnit = 10;
|
||||
|
||||
public float MinValue => this._minValue;
|
||||
public float MaxValue => this._maxValue;
|
||||
public float Value => this._value;
|
||||
public float Progress => (this._value - this._minValue ) / (this._maxValue - this._minValue);
|
||||
|
||||
|
||||
private List<Image> _images = new List<Image> ();
|
||||
private int ObjMaxValue => (int)math.ceil ((this._maxValue * 1.0f / this.RateUnit));
|
||||
private int ObjActiveValue => (int)math.ceil ((this.Progress * 1.0f * this.ObjMaxValue));
|
||||
|
||||
public void SetMinMax (float minValue, float maxValue, float value)
|
||||
{
|
||||
this._minValue = minValue;
|
||||
this._maxValue = maxValue;
|
||||
SetValue (value);
|
||||
}
|
||||
|
||||
public void SetValue (float value)
|
||||
{
|
||||
this._value = ValueMono (value);
|
||||
RefreshSlider ();
|
||||
}
|
||||
|
||||
private int ValueMono (float value)
|
||||
{
|
||||
return (int)math.clamp (value + 0.5f , this._minValue , this._maxValue);
|
||||
}
|
||||
|
||||
|
||||
private void RefreshSlider ()
|
||||
{
|
||||
if (this._prefab.gameObject.activeSelf)
|
||||
{
|
||||
this._prefab.gameObject.SetActive (false);
|
||||
}
|
||||
|
||||
var amount = math.max (this._images.Count , this.ObjMaxValue);
|
||||
|
||||
for (int i = 1; i <= amount; i++)
|
||||
{
|
||||
if (this._images.Count < i)
|
||||
{
|
||||
var image = Instantiate (this._prefab , this.transform);
|
||||
this._images.Add (image);
|
||||
}
|
||||
|
||||
var obj = this._images[i - 1];
|
||||
obj.gameObject.SetActive (i <= this.ObjMaxValue);
|
||||
obj.sprite = i <= this.ObjActiveValue ? this._activeImg : this._unActiveImg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user