【问题标题】:Displaying score in another scene on Unity在 Unity 的另一个场景中显示乐谱
【发布时间】:2018-02-22 10:54:55
【问题描述】:

在游戏中,我正在创建一个“游戏结束”场景,一旦玩家输掉游戏就会加载。在游戏过程中,分数是根据玩家在屏幕上的位置来计算的,并且随着玩家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”场景中的玩家,因为玩家不是该场景中的对象或精灵。

是否有一种简单的方法可以将前一个场景中的玩家精灵引用到最终(“游戏结束”)场景中,以便我可以使用它来确定最终得分?

这是我在游戏场景中使用“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");
    }
}

【问题讨论】:

  • 您想将玩家精灵从一个游戏场景传递到另一个游戏场景?
  • @bilal1409 理想情况下,我想将玩家的最终位置(即得分)传递到最终的比赛结束场景中,稍后我将在我创建的得分文本中显示该场景。跨度>
  • 像这个解决方案一样定义公共静态 Sprite stackoverflow.com/questions/42393259/…
  • @bilal1409 我有检查解决方案。但是,我对 c# 和统一编码还很陌生,并希望有一个更简单的解决方案。是否有更简单的方法来编写单独的脚本以在最终屏幕上显示播放器的最终位置?

标签: c# unity3d


【解决方案1】:

您可以使用DontDestroyOnLoad保留播放器

您可以将其添加到播放器中:

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

你可以做其他事情,比如只保留数据……但更简单的方法应该是这个。

请记住,玩家将留在您的游戏结束屏幕中。

在我看来,最好的办法是在您的游戏场景中创建一个名为“GameOverScreen”的新游戏对象,并在需要时禁用它。

【讨论】:

  • 我尝试了创建一个游戏对象并禁用它直到我需要它的想法。但是,现在碰撞脚本不再起作用,游戏实际上并没有结束。
  • 这是我在 'EndMenu' 脚本中使用的碰撞脚本: void OnCollisionEnter2D(Collision2D exampleCol) { if(exampleCol.collider.tag == "Obstacle") { endMenuUI.SetActive (true );时间.timeScale = 0f;游戏结束 = 真; } }
  • 为什么将 timeScale 设置为零?你能试着把它留在 0.0001f 并检查一切是否仍然有效吗?编辑:我了解您正在使用 timeScale 来完全停止游戏,但我认为这会让您感到不安。在 endMenuUI 中添加一个按钮,该按钮调用再次设置 timeScale =1.0f 的函数
  • 我将它从 0f 更改为 0.0001f 但一切似乎都保持不变。有没有办法让我给你看整个剧本?
  • 您是否尝试不破坏游戏对象并切换到您的游戏结束场景?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-11
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多