简体   繁体   English

我如何将我的球员得分保留到新的场景?

[英]how can i retain my player score to a new scene?

I have a simple script to count up a score for the player, how can I take this value and display it on a game over scene? 我有一个简单的脚本来为玩家计算一个分数,如何获得该值并将其显示在游戏中?

This is my score script: 这是我的分数脚本:

public class scoreScript : MonoBehaviour {

 public static int scoreValue = 0;
 Text score;

 // Use this for initialization
 void Start () {
     score = GetComponent<Text>();
 }

 // Update is called once per frame
 void Update () {
     score.text = "Score:" + scoreValue;
 }

}

Store it in PlayerPrefs while you exit current scene save it in playerpref like : 退出当前场景时将其存储在PlayerPrefs ,将其保存在playerpref中,例如:

PlayerPrefs.Setint(" CurrentScore",scoreValue);

then retrive it in new scene by : 然后通过以下方式在新场景中进行检索:

scoreValue = PlayerPrefs.Getint(" CurrentScore");

很简单,只需在游戏场景中所需的任何脚本中调用“ scoreScript.scoreValue”

You already made it public static , all that's left is to write in your game over scene's text field script: 您已经将其设置为public static ,剩下的就是在游戏中通过场景的文本字段脚本进行编写:

gameOverText.text = "Score: " + scoreScript.scoreValue;

assuming that gameOverText is a Text . 假设gameOverTextText

You don't need to create an instance to access a static member, you should access them using a class name ( scoreScript in your case). 您无需创建实例即可访问静态成员,而应使用类名称(在本例中为scoreScript )访问它们。

However, it's not good to mix storing global score and a textField for displaying it in a single class, because as you add new features to the game, all global variables will be in different classes, and you will pay increasingly more attention while modifying your code. 但是,混合存储全局得分和用于在单个类中显示它的textField并不是很好,因为在向游戏添加新功能时,所有全局变量都将位于不同的类中,并且在修改自己的代码时您将越来越关注码。 To avoid this, you may use a static class as a "core" where you store all global variables. 为避免这种情况,可以将静态类用作存储所有全局变量的“核心”。 For the first time, this will do. 这将是第一次。

DontDestroyOnLoad works with GameObject s, so they will not destroyed when a new Scene is loaded. DontDestroyOnLoad工作与GameObject S,所以当加载一个新的场景,他们将不被破坏。 If you call it in your script, your score counting GameObject will remain, and so will a text field because it's a compoment of that object, and will be present on game over scene, so don't do it. 如果在脚本中调用它,则计分GameObject的分数将保留,而文本字段也将保留,因为它是该对象的组合,并且会出现在游戏场景中,因此不要这样做。

When you are loading a new scene, the gameObject that carries the "scoreScript" is lost. 当您加载新场景时,带有“ scoreScript”的gameObject将丢失。 In order to transfer the same gameobject to a new scene (which makes sense in order to not lose your information that the script is carrying), you need to use the "DontDestroyOnLoad" method. 为了将相同的游戏对象转移到新的场景(为了避免丢失脚本所携带的信息,这是有意义的),您需要使用“ DontDestroyOnLoad”方法。 You just call the method in the Awake method of your monobehaviour, and then your gameobject (together with the data on your script) will persist new scene loading. 您只需在单行为的Awake方法中调用该方法,然后您的游戏对象(以及脚本中的数据)将继续新的场景加载。 Here is the relevant documentation: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html 以下是相关文档: https : //docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

and here is a code sample: 这是一个代码示例:

public class scoreScript : MonoBehaviour {
    public static int scoreValue = 0;
    Text score;

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

    void Start () {
        score = GetComponent<Text>();
    }

    void Update () {
        score.text = "Score:" + scoreValue;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的分数游戏 Object 从我的游戏场景转移到 Unity 中的游戏结束场景 - How can I Transfer my Score Game Object from my game scene to game over scene in Unity 如何统一重新加载场景中的分数(重置静态分数)? - How do I restart my score (reset static score) in reloading scene in unity? 在重新加载场景时,如何使我的Singleton MonoBehaviour类的实例以新的方式启动? - How can I make my instance of a Singleton MonoBehaviour class start as new when reloading the scene? 如何调整我的对象池以针对我加载的每个新场景进行重置? - How can adapt my object pooler to reset for each new scene that I load? 我怎样才能让我的球员跳起来? - How can I get my player to jump? 在Unity3D上的C#中释放按钮后,如何保持播放器方向? - How can I retain player orientation after a button is released in C# on Unity3D? 加载新场景时如何删除这些游戏对象? - How can I remove these game objects when loading a new scene? 按下按钮时如何使新场景淡入? - How can I make a new scene fade in when a button is pressed? 如何在 2D 对象上引用 Z 轴以在场景中的玩家身后生成射弹? - How can I reference the Z axis on a 2D object to spawn a projectile behind the player in the scene? Unity:将新剪辑分配给视频播放器会更改我当前场景中的所有视频播放器剪辑 - Unity: Assigning a new clip to video player changes ALL video player clips in my current scene
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM