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

5
在我正在制作的游戏中,当玩家失败时会加载“游戏结束”场景。在游戏过程中,玩家的得分是根据其在屏幕上的位置计算的,随着玩家的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"); 
    }
}

我尝试使用相同的编码在“游戏结束”屏幕上显示玩家的最终得分。我面临的问题是,在那个场景中玩家不是对象或精灵,因此我无法确定在“游戏结束”场景中的玩家。
有没有简单的方法可以将先前场景中的玩家精灵引用到最终(“游戏结束”)场景中,以便我可以使用它来确定最终得分?
这是我在玩耍场景中使用“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");
    }
}

你想把玩家精灵从游戏场景传递到游戏结束场景吗? - MBS
@bilal1409 理想情况下,我希望将玩家的最终位置(即得分)传递到最终游戏结束场景中,然后在我创建的得分文本上显示。 - Cem Ozsoy
请按照以下解决方案定义公共静态Sprite https://dev59.com/VVgQ5IYBdhLWcg3wqF2l - MBS
@bilal1409 我已经检查了这个解决方案。然而,我对C#和Unity编码还比较新手,希望能找到更简单的解决方案。 是否有一种更简单的方法编写一个独立的脚本来在结束画面上显示玩家的最终位置? - Cem Ozsoy
你能看到这个链接 https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html 它可以帮助你。 - MBS
3个回答

1
你可以使用DontDestroyOnLoad来保留玩家。
你可以将此添加到玩家中:
void Awake() {
    DontDestroyOnLoad(transform.gameObject);
}

你可以做其他事情,比如只保留数据...但更简单的方法应该是这种。
请记住,在游戏结束画面中玩家将保持不动。
在我看来,最好的想法是在你的游戏场景中创建一个名为 "GameOverScreen" 的新游戏对象,并在需要时将其禁用。

我尝试了创建游戏对象并将其禁用直到需要它的想法。然而,现在碰撞脚本不再起作用,游戏实际上也无法结束。 - Cem Ozsoy
我将它从0f改为0.0001f,但似乎一切都保持完全不变。有没有办法可以向您展示整个脚本? - Cem Ozsoy
我用于此的脚本已经编辑到了我的原始帖子中。我非常确定在脚本中没有破坏任何内容。 - Cem Ozsoy
仍在更新scoreText吗?如果没有,请检查脚本是否正在运行/在场景中。 - Chopi
这个脚本附加在被禁用的游戏对象EndScene上? - Chopi
显示剩余8条评论

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接