通过使用 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;
}
}
}
}
}
}
}