Unity - Save Progress In Game?(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 - 在游戏中保存进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Unity - 在游戏中保存进度?


基础教程推荐
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01