从分数中设置高分并在unity2D游戏中显示
setting a high score from score and showing it in unity2D game
我的游戏中有一个分数会在某些点增加!这工作正常,但我想要一个保存的高分并且不确定如何实现播放器偏好,因为我仍在掌握统一和 C#!到目前为止,我已经在我的 canvas 中放置了一个 ui 文本元素,这称为高分:0 我希望 0 保留并保存高分!此时增加的工作分数在更新方法中。
编辑,只是指出这不是下面发布的 link 的副本!两者都在寻找一个看似相似但并非如此的问题的不同答案
public class Player : MonoBehaviour {
public string currentColor;
public float jumpForce = 10f;
public Rigidbody2D circle;
public SpriteRenderer sr;
public Color blue;
public Color yellow;
public Color pink;
public Color purple;
public static int score = 0;
public Text scoreText;
public GameObject obsticle;
public GameObject colorChanger;
void Start () {
setRandomColor ();
circle.isKinematic = true;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown (0))
{
circle.isKinematic = false;
circle.velocity = Vector2.up * jumpForce;
}
scoreText.text = score.ToString ();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Scored")
{
score++;
Destroy (collision.gameObject);
Instantiate (obsticle, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag == "ColorChanger")
{
setRandomColor ();
Destroy (collision.gameObject);
Instantiate(colorChanger, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
if (collision.tag == "Floor")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void setRandomColor()
{
int rand = Random.Range (0, 4);
switch (rand)
{
case 0:
currentColor = "Blue";
sr.color = blue;
break;
case 1:
currentColor = "Yellow";
sr.color = yellow;
break;
case 2:
currentColor = "Pink";
sr.color = pink;
break;
case 3:
currentColor = "Purple";
sr.color = purple;
break;
}
}
}
增加分数似乎没有任何问题。如果你想保存高分并让它在游戏中持续存在,我建议在你的资产文件夹中创建一个 .txt 文件,如果分数大于文件中的分数,则将其写入其中。我还没有测试过它,因为我在 phone 上,但像这样的东西应该可以工作:
using System.IO;
public Text highScoreText;
void Start() {
highScoreText.text = File.ReadAllText(TEXTFILEPATH);
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
if (File.Exists(TEXTFILEPATH) {
int highScore = int.TryParse(File.ReadAllText(TEXTFILEPATH);
if(score > highScore) {
File.WriteAllText(TEXTFILEPATH, score.ToString());
}
else {
File.WriteAllText(TEXTFILEPATH, score.ToString());
}
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
我的游戏中有一个分数会在某些点增加!这工作正常,但我想要一个保存的高分并且不确定如何实现播放器偏好,因为我仍在掌握统一和 C#!到目前为止,我已经在我的 canvas 中放置了一个 ui 文本元素,这称为高分:0 我希望 0 保留并保存高分!此时增加的工作分数在更新方法中。 编辑,只是指出这不是下面发布的 link 的副本!两者都在寻找一个看似相似但并非如此的问题的不同答案
public class Player : MonoBehaviour {
public string currentColor;
public float jumpForce = 10f;
public Rigidbody2D circle;
public SpriteRenderer sr;
public Color blue;
public Color yellow;
public Color pink;
public Color purple;
public static int score = 0;
public Text scoreText;
public GameObject obsticle;
public GameObject colorChanger;
void Start () {
setRandomColor ();
circle.isKinematic = true;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump") || Input.GetMouseButtonDown (0))
{
circle.isKinematic = false;
circle.velocity = Vector2.up * jumpForce;
}
scoreText.text = score.ToString ();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Scored")
{
score++;
Destroy (collision.gameObject);
Instantiate (obsticle, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag == "ColorChanger")
{
setRandomColor ();
Destroy (collision.gameObject);
Instantiate(colorChanger, new Vector2(transform.position.x,transform.position.y + 7f), transform.rotation);
return;
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}
if (collision.tag == "Floor")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void setRandomColor()
{
int rand = Random.Range (0, 4);
switch (rand)
{
case 0:
currentColor = "Blue";
sr.color = blue;
break;
case 1:
currentColor = "Yellow";
sr.color = yellow;
break;
case 2:
currentColor = "Pink";
sr.color = pink;
break;
case 3:
currentColor = "Purple";
sr.color = purple;
break;
}
}
}
增加分数似乎没有任何问题。如果你想保存高分并让它在游戏中持续存在,我建议在你的资产文件夹中创建一个 .txt 文件,如果分数大于文件中的分数,则将其写入其中。我还没有测试过它,因为我在 phone 上,但像这样的东西应该可以工作:
using System.IO;
public Text highScoreText;
void Start() {
highScoreText.text = File.ReadAllText(TEXTFILEPATH);
}
if (collision.tag != currentColor) {
Debug.Log ("You Died");
if (File.Exists(TEXTFILEPATH) {
int highScore = int.TryParse(File.ReadAllText(TEXTFILEPATH);
if(score > highScore) {
File.WriteAllText(TEXTFILEPATH, score.ToString());
}
else {
File.WriteAllText(TEXTFILEPATH, score.ToString());
}
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
score = 0;
}