Interfaz de Usuario y Progreso + Menús de Navegación y Cierre de Escenas
Interfaz de Usuario y Progreso + Menús de Navegación y Cierre de Escenas
## Tutorial 9: Interfaz de Usuario y Progreso (`ManagerUI`)
### ¿Para qué sirve?
Sincroniza los textos de puntuación, récords y números de nivel, además de calcular de manera suave el progreso porcentual de la barra deslizadora del nivel.
### Código del Script (`ManagerUI.cs`)
```csharp
using UnityEngine;
using UnityEngine.UI;
public class ManagerUI : MonoBehaviour
{
[Header("Textos de Puntuación y Niveles")]
[SerializeField] private Text currentScoreText;
[SerializeField] private Text bestScoreText;
[SerializeField] private Text currentLevelText;
[SerializeField] private Text nextLevelText;
[Header("Referencias de Posicionamiento")]
[SerializeField] private Transform topTransform;
[SerializeField] private Transform goalTransform;
[SerializeField] private Transform ball;
[Header("Elementos de UI")]
[SerializeField] private Slider sliderNivel;
private AudioManagerH audioManager;
private float totalDistance;
private void Awake()
{
audioManager = FindFirstObjectByType<AudioManagerH>();
}
private void Start()
{
if (topTransform != null && goalTransform != null)
{
totalDistance = topTransform.position.y - goalTransform.position.y;
}
}
void Update()
{
if (ManagerGame.singlenton != null)
{
if (currentScoreText != null) currentScoreText.text = $"SCORE :
{ManagerGame.singlenton.currentScore}";
if (bestScoreText != null) bestScoreText.text = $"BEST :
{ManagerGame.singlenton.bestScore}";
}
ChangeSliderLevelProgress();
}
public void ChangeSliderLevelProgress()
{
if (ManagerGame.singlenton != null)
{
if (currentLevelText != null) currentLevelText.text =
(ManagerGame.singlenton.currentLevel + 1).ToString();
if (nextLevelText != null) nextLevelText.text =
(ManagerGame.singlenton.currentLevel + 2).ToString();
}
if (topTransform == null || goalTransform == null ||
ball == null || sliderNivel == null || totalDistance <= 0f)
return;
float distanceLeft = totalDistance -
(ball.position.y - goalTransform.position.y);
float targetValue = Mathf.Clamp01(distanceLeft / totalDistance);
sliderNivel.value = Mathf.Lerp(sliderNivel.value,
targetValue, 5f * Time.deltaTime);
}
public void Exit()
{
if (audioManager != null) audioManager.Play("ClickSound");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
```
### Implementación
Se asigna al Canvas principal del juego vinculando los elementos
de texto y el slider de progreso correspondientes.
---
## Tutorial 10: Menús de Navegación y Cierre de Escenas (`FinishMenu`)
### ¿Para qué sirve?
Gestiona la transición segura desde las pantallas de fin de juego o victoria hacia el menú principal, garantizando la reproducción previa de los efectos de audio y restaurando el tiempo de escala del juego.
### Código del Script (`FinishMenu.cs`)
```csharp
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FinishMenu : MonoBehaviour
{
[Header("Configuración de Audio y Escena")]
[SerializeField] private AudioSource soundClick;
[SerializeField] private string mainMenuSceneName = "MainMenu";
[SerializeField] private float sceneLoadDelay = 1f;
public void PlayGame()
{
if (soundClick != null) soundClick.Play();
StartCoroutine(NewSceneRoutine());
}
public void ExitGame()
{
if (soundClick != null) soundClick.Play();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
private IEnumerator NewSceneRoutine()
{
yield return new WaitForSecondsRealtime(sceneLoadDelay);
Time.timeScale = 1f;
SceneManager.LoadScene(mainMenuSceneName);
}
}
```
### Implementación
Asócialo a los paneles de UI de GameOver o Victoria que incluyan botones interactivos
de menú o salida.
---

Deja un comentario