只是学习Unity / C#并且在保存PlayerPrefs方面遇到了一些大问题。我们需要使用它们,尽管它们从未被解释过,而且我试图通过在线资源进行工作的所有代码迄今为止都没有成功。请记住,我还在学习,所有反馈都将非常感谢!我试图节省水平完成的时间(在舞台上一切都已经死亡之后)并使用playerpref设置功能保持单数高分。这是我目前的代码:
using UnityEngine;
using System.Collections;
public class playerScript : MonoBehaviour {
public float currentHealth = 100; // Player Hp
private float restartDelay = 5f; // Time to wait before restarting the level
public float restartTimer; // Timer to count up to restarting the level
public float thrust = 100f;
public Rigidbody rb;
private string textFieldString; // Declaring a text field into a string
public bool isFinished; // Checking if game has finished or not
public static float bestTime = PlayerPrefs.GetFloat ("HighScore", 0);
float gameTime;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
gameTime += Time.deltaTime;
if(currentHealth <= 0) {
currentHealth = 0;
restartTimer += Time.deltaTime;
// .. if it reaches the restart delay...
if(restartTimer >= restartDelay)
{
// .. then reload the currently loaded level.
Application.LoadLevel(Application.loadedLevel);
}
}
//When all enemies are cured, calls this code
if(GameObject.FindGameObjectsWithTag("Enemy").Length==0) {
isFinished = true;
//Application.LoadLevel(Application.loadedLevel);
}
if (isFinished == true) {
bool isNewHighscore = false;
if (gameTime < bestTime)
{
PlayerPrefs.SetFloat ("HighScore", gameTime);
Debug.Log ("HighScoreSet");
}
bestTime = PlayerPrefs.GetFloat("Best Time");
}
}
void OnCollisionEnter(Collision collision) {
// Reduce health
if (collision.gameObject.tag == "Enemy") {
currentHealth -= collision.gameObject.GetComponent<ZombieBotAi>().zombieDamage;
rb.AddForce (transform.forward * thrust);
}
}
//Event for running into healing bottles
void OnTriggerEnter(Collider other){
switch(other.gameObject.tag)
{
case "Heal":
ChangeHP(25);
break;
}
//Destroys healing bottle after collision
Destroy (other.gameObject);
}
//Displays Current HP on GUI
void OnGUI(){
textFieldString = GUI.TextField (new Rect (35, 500, 140, 30),"HP\t: " + currentHealth.ToString());
textFieldString = GUI.TextField (new Rect (800, 35, 140, 30),"Best Time\t: " + PlayerPrefs.GetFloat ("HighScore"));
if(GameObject.FindGameObjectsWithTag("Enemy").Length==0) {
textFieldString = GUI.TextField (new Rect (600, 300, 230, 30), "You have won the game in " + gameTime + " seconds!");
}
if (currentHealth <= 0) {
textFieldString = GUI.TextField (new Rect (600, 300, 230, 30), "You have been infected! Game Over !!");
}
}
//HP cant go above 100
void ChangeHP(float Change){
currentHealth += Change;
if (currentHealth > 100) {
currentHealth=100;
}
}
}
答案 0 :(得分:0)
你绝对应该修复这些问题:
if (isFinished == true) {
bool isNewHighscore = false;
if (gameTime < bestTime)
{
PlayerPrefs.SetFloat ("HighScore", gameTime);
Debug.Log ("HighScoreSet");
}
bestTime = PlayerPrefs.GetFloat("Best Time");
}
PlayerPrefs并不像那样工作,首先应该为变量赋值。你在这里完成了这个:
PlayerPrefs.SetFloat ("HighScore", gameTime);
然后你应该打电话:
PlayerPrefs.Save();
保存变量,如果要加载变量,则应使用与保存变量相同的密钥。
bestTime = PlayerPrefs.GetFloat("HighScore");
希望这就是你要找的东西。