Я изо всех сил пытался придумать, как добавить общий счет за всю жизнь к моему менеджеру очков.
Я хочу, чтобы общий счет был накопительным, даже когда игрок умирает
Я пытался ввести playerprefs,однако при запуске он сбрасывается до 0.
Мой скрипт, который в настоящее время работает и сохраняет рекорд с помощью playerprefs;
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;
}
}
при запуске currentLifetimeScore
при обновлении: эффективноtotalalscore = расстояние + оценка + продолжительность жизни;