Unity c# PlayerPrefs /无法使其工作

本文关键字:工作 PlayerPrefs Unity | 更新日期: 2023-09-27 17:51:15

刚刚学习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;
                }
    }
}

Unity c# PlayerPrefs /无法使其工作

你一定要修正这些行:

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");

希望这就是你要找的