Estoy trabajando en un juego pequeño similar a Angry Birds ya que soy nuevo tanto en Unity como en C#. Quiero cargar y desbloquear el siguiente nivel si todos los enemigos están muertos y verificar si existe un nuevo nivel (si no) volver a la escena de selección de nivel.
Esto es lo que probé:
NivelScript
using UnityEngine; using UnityEngine.SceneManagement; public class LevelScript : MonoBehaviour { [SerializeField] string _nextLevelName; Monster[] _monsters; void OnEnable() { _monsters = FindObjectsOfType<Monster>(); } private void Update() { int currentLevel = SceneManager.GetActiveScene().buildIndex ; if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked")) { PlayerPrefs.SetInt("levelsUnlocked", currentLevel ); } if (MonsterAreAllDead()) { GoToNextLevel(); } Debug.Log("Level" + PlayerPrefs.GetInt("levelsUnlocked") + "UNLOCKED"); } public void Pass() { int currentLevel = SceneManager.GetActiveScene().buildIndex; if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked") ) { PlayerPrefs.SetInt("levelsUnlocked", currentLevel + 1); }; } bool MonsterAreAllDead() { foreach (var monster in _monsters) { if (monster.gameObject.activeSelf) return false; } return true; } void GoToNextLevel() { Debug.Log("Go to next level" + _nextLevelName); SceneManager.LoadScene(_nextLevelName); }}
y Gerente de Nivel
public class LevelManager : MonoBehaviour { int levelsUnlocked; public Button[] buttons; void Start() { levelsUnlocked = PlayerPrefs.GetInt("levelsUnlocked", 1); for (int i = 0; i < buttons.Length; i++) { buttons[i].interactable = false; } for (int i = 0; i < levelsUnlocked; i++) { buttons[i].interactable = true; } } public void LoadLevel(int levelIndex) { SceneManager.LoadScene(levelIndex); }Obtuve estos 2 scripts y adjunté tanto el lienzo como mis botones y el script de nivel a mis niveles.
El problema es que cada nivel se desbloquea al principio y después de completar los niveles automáticamente, quiere pasar al siguiente nivel que aún no existe.
Por favor, ayúdame. Lo siento si mi pregunta es estúpida y mal inglés.
Debe crear una matriz de escenas en su LevelManager que conozca todos sus niveles (en orden) y para obtener el siguiente nivel, puede obtener la posición de su escena real en la matriz y verificar la siguiente.
algo como
pseudocódigo:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Example() {
Application.LoadLevel("HighScore");
}
}