【问题标题】:Unity - Save Progress In Game?Unity - 在游戏中保存进度?
【发布时间】:2015-04-27 18:07:32
【问题描述】:

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

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

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

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

【问题讨论】:

标签: c# android unity3d save


【解决方案1】:

这是一个名为 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";
    }
}

【讨论】:

  • 我需要创建另一个 c 尖脚本插入这个?
  • 这是一个名为 MyClass 的类的示例。您需要:复制您自己的 Save 和 Load 方法,并在最后修改 load 以从加载的对象中获取正确的信息。您还需要将可序列化添加到您的类中。
【解决方案2】:

你应该这样尝试

  PlayerPrefs.SetInt("score",ScoreManager.score);
  PlayerPrefs.Save();
  score=PlayerPrefs.GetInt("score");

我已经在我的 Web 和 WebGL 构建项目中尝试过这个,如果你需要了解序列化,我可以给你一个例子

【讨论】:

    【解决方案3】:

    检查你的Android项目的权限,如果你添加了“android.permission.WRITE_EXTERNAL_STORAGE”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2020-04-17
      • 2011-06-12
      相关资源
      最近更新 更多