42 lines
996 B
C#
42 lines
996 B
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class VrAmmoView : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<Image> ammoCells;
|
|
[SerializeField] private Sprite FilledSprite, EmptySprite;
|
|
[SerializeField] private bool reverseList = true;
|
|
|
|
private Weapon _weapon;
|
|
private bool initialized = false;
|
|
|
|
|
|
public void Initialize(Weapon weapon)
|
|
{
|
|
if (reverseList)
|
|
ammoCells.Reverse();
|
|
_weapon = weapon;
|
|
_weapon.OnShoot += Refresh;
|
|
Refresh();
|
|
initialized = true;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (!initialized) return;
|
|
_weapon.OnShoot -= Refresh;
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
int filledCount = Mathf.Min(ammoCells.Count, _weapon.Ammo);
|
|
|
|
for (int i = 0; i < ammoCells.Count; i++)
|
|
{
|
|
bool needToFill = i < filledCount;
|
|
ammoCells[i].sprite = needToFill ? FilledSprite : EmptySprite;
|
|
}
|
|
}
|
|
}
|