Unity 5网站游戏PlayerPrefs不起作用?

时间:2015-05-10 11:48:24

标签: c# unity3d

我为网站创建了一个点击游戏。我想添加最后的东西:在本地保存游戏的能力。我不知道如何使用playerprefs,但我看了所有的视频,并尝试创建savegame和loadgame但它不起作用,但一切看起来都是正确的。我的存档或加载游戏有什么问题?

存盘:

using UnityEngine;
using System.Collections;

public class SaveGame : MonoBehaviour {

    private Databasestorage database;

    public void savegame(){

        database = GameObject.Find ("PlayerCamera").GetComponent<Databasestorage> ();

        PlayerPrefs.SetFloat ("cookies", database.cookies);
        PlayerPrefs.Save ();

    }
}

LoadGame:

using UnityEngine;
using System.Collections;

public class LoadSaveGame : MonoBehaviour {

    private Databasestorage database;

    public void loadsavegame(){

        database = GameObject.Find ("PlayerCamera").GetComponent<Databasestorage> ();

        if (PlayerPrefs.HasKey ("cookies")) {
            database.cookies = PlayerPrefs.GetFloat ("cookies");
        } else {
            Debug.Log ("Savegame was not found. Canceling load save game.");
        }
        //if (PlayerPrefs.HasKey ("cookies")) {
        //  Application.LoadLevel (PlayerPrefs.GetFloat ("cookies"));
        //} else {
        //  Debug.Log ("Savegame was not found. Canceling load save game.");
        //}
    }
}

数据库,如果你想知道它是什么:

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

public class Databasestorage : MonoBehaviour {

    public int enabletimer = 0;
    public double checktime = 3.0;
    public double checktimereset = 3.0;

    public float cookies = 0;
    public float drink = 0;

    public Text cookiestext;

    void Start () {

    }

    void Update () {

        if (enabletimer == 1) {
            checktime -= Time.deltaTime;
        } else {
            cookiestext.GetComponent<Text> ().text = "Cookies: " + cookies;
        }
        if (checktime <= 0) {
            cookiestext.GetComponent<Text> ().text = "Cookies: " + cookies;
            checktime += checktimereset;
        }
  }
}

更新:LoadGame不起作用! (加载保存游戏)

1 个答案:

答案 0 :(得分:1)

Button没有执行loadgame,因为它没有设置。 OPS