[英]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
. 假设
gameOverText
是Text
。
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.