80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
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é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é à l'objet
|
|
// Vérifiez que les références sont assignées
|
|
if (uiDocument == null || scorePannelTemplate == null)
|
|
{
|
|
Debug.LogError("UiDocument ou scorePannelTemplate n'est pas assigné.");
|
|
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 été trouvé 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
|
|
|
|
|
|
} |