如何将数据保存到选定的保存槽

时间:2016-10-19 08:17:14

标签: c# unity3d

我制作了saveGame功能,我有6个插槽用于保存。怎么说,我应该在哪个插槽中保存数据?我应该为6个插槽中的每一个创建变量

enter image description here

 private int currentActiveSlot;
 private int latestSaveSlot;
 const int _numberOfSlots = 7;

 string[] dateTime = new string[_numberOfSlots];
 int[] actNumber = new int[_numberOfSlots];
 int[] stepNumber = new int[_numberOfSlots];
 int[] transformPositionX = new int[_numberOfSlots];
 int[] transformPositionY = new int[_numberOfSlots];
 int[] transformPositionZ = new int[_numberOfSlots];
 int[] transformRotationX = new int[_numberOfSlots];
 int[] transformRotationY = new int[_numberOfSlots];
 int[] transformRotationZ = new int[_numberOfSlots];

 void Start()
 {
     latestSaveSlot = PlayerPrefs.GetInt("latestSaveSlot");
 }

 public void ButtonSave()
 {
     // How to say, to which slot should I save?
     latestSaveSlot = currentActiveSlot;
     PlayerPrefs.SetString("date time", "");
     PlayerPrefs.SetInt("act number", 0);
     PlayerPrefs.SetInt("step number", 0);
     //..      
     PlayerPrefs.Save();
     dateTime[latestSaveSlot] = PlayerPrefs.GetString("date time");
 }

 public void ButtonLoad()
 {
     dateTime[currentActiveSlot]  = PlayerPrefs.GetString("date time");
     actNumber[currentActiveSlot] = PlayerPrefs.GetInt("act number");
     stepNumber[currentActiveSlot] = PlayerPrefs.GetInt("step number");
     //..
 }

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那么我会做以下事情:

每个保存位置都有" SavedOn(日期时间)"," SavedById(int->执行保存操作的用户),""。

然后,当你想要进行新的保存时,我会首先搜索" null" (按id asc排序),如果没有,我会显示最旧的一个并询问用户是否要重写它(通过丢失旧数据并保存新数据)。

这是你在找什么?

答案 1 :(得分:1)

最基本的方法(我知道不是最好的方法)是将插槽的索引直接添加到PlayerPrefs的键中。你只有6个,所以键的数量仍然可以接受。 像这样:

 public void ButtonSave()
 {
     // How to say, to which slot should I save?
     latestSaveSlot = currentActiveSlot;
     PlayerPrefs.SetString("date time" + latestSaveSlot, "");
     //...

并且,类似于加载阶段。

 public void ButtonLoad()
 {
     dateTime[currentActiveSlot]  = PlayerPrefs.GetString("date time" + currentActiveSlot);
     //...