如何在Android商店简单的数据与统一? [英] How to store simple data in Android with unity?

查看:157
本文介绍了如何在Android商店简单的数据与统一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出的团结唱首歌样的Andr​​oid游戏,我需要存储一些数据,每次有事在游戏中,但是这将是非常简单的数据,像一些整数和字符串。是否可行做到这一点的东西,如在文件中JSON和存储序列化呢?

I am making a clicker-like Android game on Unity and I need to store some data everytime something happens in the game, but it will be very simple data, like some ints and strings. Is it viable to do it serializing it with something like json and store in a file?

推荐答案

汗Faizan说的,你可以使用播放器preFS persistentDataPath 来保存和访问数据。

As Mohammed Faizan Khan said, you can use PlayerPrefs or persistentDataPath to keep and access the data.

播放器preFS一个简单的例子

private int score = 0;
private int savedScore;

void Update () {
    if (Input.GetKeyDown (KeyCode.S)) {
        PlayerPrefs.SetInt("Score", score);
        Debug.Log(score);
        }
    if (Input.GetKeyDown (KeyCode.L)) {
        savedScore = PlayerPrefs.GetInt("Score");
        Debug.Log(savedScore);
        }

有一个简单的例子 persistentDataPath

private string savedName;
private int savedHealth;
private string loadedName;
private int loadedHealth;

public void Save(){
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/FileName.dat", FileMode.Create);
    PlayerClass newData = new PlayerClass();
    newData.health = savedHealth;
    newData.name = savedName;
    bf.Serialize(file, newData);
    file.Close();
}

public void Load(){

    if (File.Exists(Application.persistentDataPath + "/FileName.dat")){
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Open(Application.persistentDataPath + "/FileName.dat", FileMode.Open);
        ObjData newData = (ObjData)bf.Deserialize(file);
        file.Close();
        loadedHealth = newData.health;
        loadedName = newData.name;
    }
}

[Serializable]
class PlayerClass{
    public string name;
    public int health;

}

记住,你需要
使用系统;
使用System.Runtime.Serialization.Formatters.Binary;
使用System.IO;
命名空间 persistentDataPath

这篇关于如何在Android商店简单的数据与统一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆