Projet-IA-Madelaine/Scripts/GameObjectsScripts/ScorePannelScript.cs

80 lines
2.3 KiB
C#
Raw Normal View History

2024-06-12 21:03:42 +02:00
using System;
using UnityEngine;
using UnityEngine.UIElements;
/// <summary>
/// Instanciate a ScorePannel view on the UI. This class only serves as view and does not contain any game data and does not affect the game's work
/// </summary>
public class ScorePannelScript : MonoBehaviour
{
#region PROPERTIES
#endregion
#region VARIABLES
[SerializeField] private VisualTreeAsset scorePannelTemplate; // R<>f<EFBFBD>rence au template UXML du ScorePannel
private VisualElement scoreContainer; // Le VisualElement parent o<> ajouter le ScorePannel
private VisualElement root;
private Label scoreLabel;
private Label playerNameLabel;
private VisualElement scorePanel;
#endregion
#region EVENTS
#endregion
#region ENDPOINTS
public void SetPlayerName(string playerName)
{
playerNameLabel.text = playerName;
}
public void SetScore(float score)
{
scoreLabel.text = score.ToString("0");
}
public void SetColor(Color color)
{
scorePanel.style.backgroundColor = new StyleColor(new Color(color.r, color.g, color.b, 0.43f));
}
public void Dispose()
{
scoreContainer.Remove(root);
Destroy(gameObject);
}
#endregion
#region LIFECYCLE
void OnEnable()
{
Debug.Log("SCORE Instanciatied");
var uiDocument = GetComponentInParent<UIDocument>(); // Obtenir le composant UIDocument attach<63> <20> l'objet
// V<>rifiez que les r<>f<EFBFBD>rences sont assign<67>es
if (uiDocument == null || scorePannelTemplate == null)
{
Debug.LogError("UiDocument ou scorePannelTemplate n'est pas assign<67>.");
return;
}
var rootVisualElement = uiDocument.rootVisualElement;
// V<>rifiez que ScoresContainer existe dans l'arborescence de l'UI
scoreContainer = rootVisualElement.Q<VisualElement>("ScoresContainer");
if (scoreContainer == null)
{
Debug.LogError("ScoresContainer n'a pas <20>t<EFBFBD> trouv<75> dans le UXML.");
return;
}
// Charger et instancier le ScorePannel
root = scorePannelTemplate.CloneTree();
scorePanel = root.Q<VisualElement>("panel");
scoreLabel = root.Q<Label>("ScoreValue");
playerNameLabel = root.Q<Label>("PlayerName");
scoreContainer.Add(root);
root.AddToClassList("scorePanel");
}
#endregion
}