Unity - 在游戏中保存进度? [英] Unity - Save Progress In Game?

查看:31
本文介绍了Unity - 在游戏中保存进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 Unity3d 创建一个小游戏.但现在我无法保存一些变量.现在我正在使用以下代码

Hi I am creating a small game with Unity3d. But now I have trouble saving some variables. Now I am using the following code

void Awake(){
proiettili  = PlayerPrefs.GetFloat ("PallottoleOgniSecondo");
}

void OnApplicationQuit(){
    PlayerPrefs.SetFloat ("PallottoleOgniSecondo", proiettili);
    PlayerPrefs.Save();
}

这样我可以保存变量,但只有当我尝试统一时,如果我尝试在 Android 上不起作用.你知道我该如何解决吗?

This way I can save the variable, but only when I try on unity, if I try does not work on Android. you know how I can fix?

推荐答案

这是一个名为 MyClass 的示例类的方法

This is how you do it for an example class called MyClass

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

[Serializable]
public class MyClass {

    public int myInt;

    public void Save(){
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create (FilePath());
        bf.Serialize(file, this);
        file.Close();
    }

    public void Load(){
        if(File.Exists(FilePath())){
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(FilePath(), FileMode.Open);
            MyClass loaded = bf.Deserialize(file) as Brochure;
            myInt = loaded.myInt;
        }
    }

    static string FilePath()
    {
        return Application.persistentDataPath + "/myClass.dat";
    }
}

这篇关于Unity - 在游戏中保存进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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