1
Fork 0
mirror of https://github.com/Steffo99/keep-everything-alive.git synced 2024-11-27 11:34:18 +00:00
keep-everything-alive/Assets/Scripts/Core/UI/LivesPanel.cs

46 lines
1.4 KiB
C#
Raw Normal View History

2020-04-18 14:09:37 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LivesPanel : MonoBehaviour
{
private GameController gameController;
[BeforeStart]
public Image lifeImagePrefab;
[BeforeStart]
public Sprite lifeFull;
[BeforeStart]
public Sprite lifeEmpty;
2020-04-18 17:02:22 +00:00
[BeforeStart]
public float livesImagesGap = 0f;
2020-04-18 14:09:37 +00:00
private Image[] livesImages;
private void Awake() {
gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
}
private void Start() {
gameController.OnLivesChange += OnLivesChange;
livesImages = new Image[gameController.maxLives];
for(int i = 0; i < gameController.maxLives; i++) {
Image created = Instantiate(lifeImagePrefab.gameObject, transform).GetComponent<Image>();
livesImages[i] = created;
2020-04-18 17:02:22 +00:00
created.name = "Life #" + i.ToString();
created.rectTransform.anchoredPosition = new Vector2(-i * (created.rectTransform.rect.width + livesImagesGap), 0);
2020-04-18 14:09:37 +00:00
}
}
private void OnLivesChange(int previous, int current) {
for(int i = 0; i < livesImages.Length; i++) {
2020-04-19 01:48:15 +00:00
if(i < current) {
2020-04-18 14:09:37 +00:00
livesImages[i].sprite = lifeFull;
}
else {
livesImages[i].sprite = lifeEmpty;
}
}
}
}