gpt4 book ai didi

c# - unity playerpref lifetime 分数

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:47 25 4
gpt4 key购买 nike

我一直在努力让我的头脑清醒地向我的评分管理器添加一个终生总分。

我希望即使玩家死亡也能累积总分

我试过引入播放器偏好设置,但是这在开始时一直重置为 0。

我的脚本当前有效并使用播放器偏好保存高分;

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;


public class ScoreManager : MonoBehaviour
{


public Text hiScoreText;
public Text totalScoreText;


public Text distanceText;
public Text scoreText;
public Text hiScoreText2;
public Text totalScoreText2;

public GameObject NewHighScorePanel;

public float scoreCount;
public float hiScoreCount;



public float pointsPerSecond;
public bool scoreIncreasing;
public bool shouldDouble;


public Transform Player;
public Transform checkpoint;

public float distance;
public float score;
public float totalScore;
private float pointsToAdd;




// Use this for initialization
void Start()
{
this.Player = GameObject.FindWithTag("Player").transform;
this.checkpoint = GameObject.FindWithTag("LevelStart").transform;

if (PlayerPrefs.HasKey("HighScore"))
{
hiScoreCount = PlayerPrefs.GetFloat("HighScore");
}
}

// Update is called once per frame
void Awake()
{
score = 0;
totalScore = 0;
}

void Update()
{


distance = 10 * (Player.transform.position.x - checkpoint.transform.position.x);

totalScore = distance + score;

/* if (scoreIncreasing)
{
scoreCount += pointsPerSecond * distance; //was Time.deltaTime


}*/

if (totalScore > hiScoreCount)
{
NewHighScorePanel.SetActive(true);
//Debug.Log("new highscore is achieved!");
hiScoreCount = totalScore;
PlayerPrefs.SetFloat("HighScore", hiScoreCount);

}

//InGame Scoring


totalScoreText2.text = "" + Mathf.Round(totalScore);
hiScoreText2.text = "" + Mathf.Round(hiScoreCount);


//GameOver Score
distanceText.text = "Distance: " + Mathf.Round (distance);
scoreText.text = "Treasures: " + Mathf.Round(score);
totalScoreText.text = "Total Score: " + Mathf.Round(totalScore);
hiScoreText.text = "High Score: " + Mathf.Round(hiScoreCount);


}


public void AddScore(float pointsToAdd)
{

score += pointsToAdd;
}
}

开始时当前生命周期得分

关于更新:effectively totalscore = distance + score + lifetimescore;

最佳答案

当您调用PlayerPrefs.SetXXX 函数时,数据不会立即保存在磁盘上,而是存储在内存中。在 Unity 中调用 OnApplicationQuit 后,您对 PlayerPrefs 的所有更改现在都会保存。这可能是问题所在。在调用 PlayerPrefs.SetXXX 函数后显式调用 PlayerPrefs.Save 函数以保存更改。

PlayerPrefs.SetFloat("HighScore", hiScoreCount);
PlayerPrefs.Save();

无关但由于脚本中不必要的舍入代码,我认为您的分数变量应该是 int 而不是 float 然后使用 PlayerPrefs.SetInt 来保存 int。如果您有充分的理由让它们 float ,请忽略它。

关于c# - unity playerpref lifetime 分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53362160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com