在游戏中,我正在创建一个“Game Over”场景,一旦玩家输掉游戏,该场景就会加载。在游戏过程中,分数按玩家在屏幕上的位置计算,并随着玩家 y 轴位置的增加而增加。
我使用此代码在实际玩游戏的场景中显示分数:
using UnityEngine;
using UnityEngine.UI;
public class PlayerScore : MonoBehaviour
{
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.y.ToString("0");
}
}
我尝试使用相同的编码在“Game Over”屏幕上显示玩家的最终得分。我面临的问题是我无法识别“Game Over”场景中的玩家,因为该玩家不是该场景中的对象或精灵。
有没有一种简单的方法来引用玩家的精灵从上一个场景到最后(“游戏结束”)场景,所以我可以用它来确定最终得分?
这是我在游戏场景中使用“EndScene”的游戏对象尝试的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class EndMenu: MonoBehaviour
{
public Text scoreText;
public Transform player;
public static bool GameEnds = false;
public GameObject endMenuUI;
void OnCollisionEnter2D(Collision2D exampleCol)
{
if(exampleCol.collider.tag == "Obstacle")
{
endMenuUI.SetActive(true);
Time.timeScale = 0.0001f;
GameEnds = true;
}
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void BackToMenu()
{
SceneManager.LoadScene("Menu");
}
public void Quit()
{
Debug.Log("Quitting game...");
Application.Quit();
}
// Update is called once per frame
void Update()
{
scoreText.text = player.position.y.ToString("0");
}
}

您可以使用DontDestroyOnLoad保持播放器
您可以将此添加到播放器:
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
你可以做其他的东西,如保持数据...但更简单的方法应该是这个。
请记住,玩家将留在你的游戏画面。
在我看来,最好的主意可能是在游戏场景中创建一个名为“GameOverScreen”的新游戏对象,并禁用它,直到你需要它。

我认为最简单的解决方案是简单地将分数存储为静态变量,该变量将在场景加载中持续存在,然后在重新开始时手动重置它。
public class PlayerScore : MonoBehaviour
{
public Transform player;
public Text scoreText;
public static string scoreString;
// Update is called once per frame
void Update()
{
scoreString = player.position.y.ToString("0");
scoreText.text = scoreString;
}
}
现在,您将能够从代码中的任何位置访问 scoreString,它将是 PlayerScore 组件上次运行其 Update()时的任何内容。然后在EndMenu
类中,您只需更新Retry()
和BackToMenu()
方法如下:
public void Retry()
{
PlayerScore.scoreString = "0";
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void BackToMenu()
{
PlayerScore.scoreString = "0";
SceneManager.LoadScene("Menu");
}
你的EndMenu.Update()
方法如下:
// Update is called once per frame
void Update()
{
scoreText.text = PlayerScore.scoreString;
}

/ / 尝试使用 playerprefs 保存分数
public class PlayerScore : MonoBehaviour
{
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.y.ToString("0");
PlayerPrefs.SetInt("CurrentScore", player.position.y);
}
}
/ / 对于另一个场景,使用 playerprefs.getInt 获取保存的分数的值
public class EndMenu: MonoBehaviour
{
public Text scoreText;
public Transform player;
public static bool GameEnds = false;
public GameObject endMenuUI;
void OnCollisionEnter2D(Collision2D exampleCol)
{
if(exampleCol.collider.tag == "Obstacle")
{
endMenuUI.SetActive(true);
Time.timeScale = 0.0001f;
GameEnds = true;
}
}
public void Retry()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void BackToMenu()
{
SceneManager.LoadScene("Menu");
}
public void Quit()
{
Debug.Log("Quitting game...");
Application.Quit();
}
// Update is called once per frame
void Update()
{
scoreText.text = PlayerPrefs.GetInt("Score",0);
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(41条)