通过使用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;
}
}
}
}
}
}
}
答案 0 :(得分:0)
如果每个场景都代表项目中的一个关卡,则可以创建一个按钮,并在按下该按钮时将该方法添加到按钮中:
using UnityEngine.SceneManagement;
public void RestartScene(){
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}//RestartScene
对于您当前的得分,我认为您应该将其存储在一个脚本中,通过包含此脚本,该脚本在加载场景时不会删除
DontDestroyOnLoad(this.gameObject);
我希望这是您所需要的。