通过使用Unity C#进行游戏,我尝试在具有多个关卡的场景中创建一个重播关卡按钮,关卡管理器已经完成, 关键是当游戏玩家在关卡 x111 中输掉并且面板游戏结束出现时,我想创建一个重播按钮关卡,谁可以让他再次重玩相同的最后一关,谁是具有分数的场景也是以前可用的。
我需要帮助吗?
这是我到目前为止必须做的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Balloon : MonoBehaviour {
private Text ScoreText;
public GameObject GameOverPanel;
//private GameObject Restartbtn;
private void Start () {
ScoreText = GameManager.gm.GameplayUI.Find ("ScorePlaceholder").Find ("Score").GetComponent<Text> ();
}
private void Update () {
ScoreText.text = Mathf.Max (0, Mathf.FloorToInt (transform.position.y)).ToString ();
}
private void OnTriggerEnter2D (Collider2D other) {
if (other.tag == "Obstacle") {
//print ("GAME OVER!");
//SceneManager.LoadScene ("Gameplay");
GameOverPanel.SetActive(true);
Time.timeScale = 0f;
}
else if (other.tag == "LevelEnd") {
other.tag = "Untagged"; //Can trigger only once (needs, bcz balloon has 2 colliders)
GameManager.gm.lm.SpawnLevel ();
}
}
public void Restartbtn() {
//SceneManager.LoadScene("Gameplay");
GameOverPanel.SetActive(false);
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
//SpawnLevel method is from levelsmanger script, he just swap unto the next level like if the last level is already success
GameManager.gm.lm.SpawnLevel();
//Application.LoadLevel("CurrentLevel");
Time.timeScale = 1f;
}
}
这是关卡管理器的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelsManager : MonoBehaviour {
public int currentLevel;
public float levelSizeY;
private Vector2 nextLevelPos;
private Text LevelText;
public GameObject [] levelPrefabs;
private GameObject newLevel;
private GameObject curLevel;
private GameObject oldLevel;
private void Start () {
LevelText = GameManager.gm.GameplayUI.Find ("LevelPlaceholder").Find ("Level").GetComponent<Text> ();
nextLevelPos = new Vector2 (nextLevelPos.x, nextLevelPos.y + levelSizeY);
SpawnLevel ();
}
public void SpawnLevel () {
LevelText.text = currentLevel.ToString () + "/69";
currentLevel++;
Destroy (oldLevel); //Removes old lvl
if (currentLevel == 2) { oldLevel = GameObject.Find ("Level_Start"); }
else { oldLevel = curLevel; } //Moves cur lvl to old lvl
SetOrderLayer (oldLevel, "OldLevel");
curLevel = newLevel; //Moves new lvl to cur lvl
SetOrderLayer (newLevel, "CurrentLevel");
int randomLvl = Random.Range (0, levelPrefabs.Length);
newLevel = Instantiate (levelPrefabs [randomLvl], nextLevelPos, Quaternion.identity); //Makes new lvl
newLevel.transform.Find ("LevelUI").Find ("LevelNr").GetComponent<Text> ().text = currentLevel.ToString ();
SetOrderLayer (newLevel, "NewLevel");
nextLevelPos = new Vector2 (nextLevelPos.x, nextLevelPos.y + levelSizeY);
}
private void SetOrderLayer (GameObject parentObject, string sortingLayerName) {
if (parentObject) {
for (int child = 0; child < parentObject.transform.childCount; child++) {
if (parentObject.transform.GetChild (child).GetComponent<SpriteRenderer> ()) {
parentObject.transform.GetChild (child).GetComponent<SpriteRenderer> ().sortingLayerName = sortingLayerName;
}
for (int subChild = 0; subChild < parentObject.transform.GetChild (child).childCount; subChild++) {
if (parentObject.transform.GetChild (child).GetChild(subChild).GetComponent<SpriteRenderer> ()) {
parentObject.transform.GetChild (child).GetChild (subChild).GetComponent<SpriteRenderer> ().sortingLayerName = sortingLayerName;
}
for (int subSubChild = 0; subSubChild < parentObject.transform.GetChild (child).GetChild (subChild).childCount; subSubChild++) {
if (parentObject.transform.GetChild (child).GetChild (subChild).GetChild (subSubChild).GetComponent<SpriteRenderer> ()) {
parentObject.transform.GetChild (child).GetChild (subChild).GetChild (subSubChild).GetComponent<SpriteRenderer> ().sortingLayerName = sortingLayerName;
}
}
}
}
}
}
}
如果每个场景代表项目中的一个关卡,则可以创建一个按钮,并在按下此方法时将其添加到按钮中:
using UnityEngine.SceneManagement;
public void RestartScene(){
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}//RestartScene
对于您当前的分数,我认为您应该将其存储在加载场景时不会删除的脚本中,方法是包含以下内容
DontDestroyOnLoad(this.gameObject);
我希望这是你需要的。
public void RePlayButton()
{
Time.timeScale = 1;
int levelNumber = GetLevelNumber();
SceneManager.LoadScene(levelNumber);
}
public int GetLevelNumber()
{
int sceneCount = SceneManager.sceneCountInBuildSettings;
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
return currentSceneIndex;
}
也许像这样