在 Unity上的另一个场景中显示分数

在游戏中,我正在创建一个“Game Over”场景,一旦玩家输掉游戏,该场景就会加载。在游戏过程中,分数按玩家在屏幕上的位置计算,并随着玩家 y 轴位置的增加而增加。

在游戏中,我正在创建一个“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");
    }
}
1

您可以使用DontDestroyOnLoad保持播放器

您可以将此添加到播放器:

void Awake() {
    DontDestroyOnLoad(transform.gameObject);
}

你可以做其他的东西,如保持数据...但更简单的方法应该是这个。

请记住,玩家将留在你的游戏画面。

在我看来,最好的主意可能是在游戏场景中创建一个名为“GameOverScreen”的新游戏对象,并禁用它,直到你需要它。

0

我认为最简单的解决方案是简单地将分数存储为静态变量,该变量将在场景加载中持续存在,然后在重新开始时手动重置它。

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;
}
0

/ / 尝试使用 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);
    }
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(940)
在 iOS上绘制热图
上一篇
使用密码的Windows7登录脚本
下一篇

相关推荐

  • 轨道交通系统:如何在Unity游戏中有效地实现交通系统 以便在移动设备上顺利运行

    关于轨道交通系统的问题,在mobile traffic system中经常遇到,最近开发了一款赛车游戏,游戏很好,但是当我添加了交通系统(带有航点跟随器的 AI 汽车)时,游戏变得笨拙而缓慢。我使用了将近 300 多辆装有硬体和箱式对撞机的汽车。我搜索了问题的解决方案:有人说这是使游戏变慢的刚性或对撞机的数量。但是,如果我删除对撞机,那么我将无法检测到我的主要汽车玩家的…

    2022-12-04 16:01:26
    0 29 54

发表评论

登录 后才能评论

评论列表(53条)