一位朋友让我帮他完成他的游戏,这是由其他两位程序员编写的。其中一个不是很清楚,因此部分代码搞砸了,但这不是问题。
此游戏允许玩家编辑和保存新关卡。以前的程序员使用PlayerPrefs来存储它们的每一个数据,而不是结构或类对象(我没有看到它的重点,但重新编程一切都需要一些时间我没有)。
我做了什么:我为每个级别添加了一个名为Difficulty的新参数。保存在编辑器中时,级别应按顺序保存(从Easy A-Z到Hard A-Z)。我做了两个保存/加载级别的常规函数,基本上是getString / setString和getInt / setInt,我调用它来存储和检索它们。它在我的电脑上工作正常。
问题:构建时,没有Unity的人不能保存新的编辑级别,也不能更新他们正在编辑的级别。 PlayerPrefs只是不存储信息。 但是,它确实在我的身上。我在编辑器中改变了一些东西,它们正确地发生了。我在我的游戏计算机上开始构建(.exe),它确实存储了。我注意到的奇怪之处在于,至少在我的计算机中,编辑器和构建器共享相同的信息,因此如果我保存或编辑一个级别,则更改将持续用于编辑器,反之亦然。
作为旁注,他们没有编制"退出"按钮或至少我还没有找到它,所以我必须使用alt + f4退出。我在saveLevel函数的末尾添加了PlayerPref.Save(),但它没有做任何事情。这不重要,因为当我得到游戏时,它功能正常并且保存正确。
有什么想法吗?
编辑:
插入已排序的新级别:
public void SaveLevel()
{
if (inputLevelName != null)
{
SortBeatList();
// - Get difficulty chosen by the player
string trackDiff = "";
if (easyDifficulty.isOn == true)
{
trackDiff = " (Easy)";
}
else if (mediumDifficulty.isOn == true)
{
trackDiff = " (Medium)";
}
else if (hardDifficulty.isOn == true)
{
trackDiff = " (Hard)";
}
string auxDiff = PlayerPrefs.GetString (PlayerPrefs.GetInt ("CurrentLevel") + "Difficulty");
string auxName = PlayerPrefs.GetString (PlayerPrefs.GetInt ("CurrentLevel") + "Name");
// - Save current level
if (inputLevelName.text.CompareTo (auxName) == 0 && CompareDifficulties(trackDiff, auxDiff) == 0)
{
SaveTrackInPosition(PlayerPrefs.GetInt ("CurrentLevel"), inputLevelName.text, trackDiff, beatStruct);
}
else
{
int numberOfLevels = PlayerPrefs.GetInt ("LevelCount",-1);
int cont = 0;
// - Search for correct difficulty place
while ( cont < numberOfLevels && CompareDifficulties( PlayerPrefs.GetString (cont + "Difficulty") , trackDiff ) > 0 )
cont++;
auxDiff = PlayerPrefs.GetString (cont + "Difficulty");
auxName = PlayerPrefs.GetString (cont + "Name");
if ( CompareDifficulties(trackDiff, auxDiff) == 0 )
// - Search for correct space for name
while ( cont < numberOfLevels && CompareDifficulties(trackDiff, auxDiff) == 0 &&
inputLevelName.text.CompareTo (auxName) > 0 )
{
cont++;
auxName = PlayerPrefs.GetString (cont + "Name");
auxDiff = PlayerPrefs.GetString (cont + "Difficulty");
}
auxName = PlayerPrefs.GetString (cont + "Name");
if ( CompareDifficulties(trackDiff, auxDiff) == 0 && inputLevelName.text.CompareTo (auxName) == 0 ) // -- Replace existing track
{
// - Update or replace
PlayerPrefs.SetInt ("CurrentLevel", cont);
SaveTrackInPosition(PlayerPrefs.GetInt ("CurrentLevel"), inputLevelName.text, trackDiff, beatStruct);
}
else if (cont >= numberOfLevels)
{
// - Insert at last
PlayerPrefs.SetInt ("LevelCount", numberOfLevels + 1);
PlayerPrefs.SetInt ("CurrentLevel", numberOfLevels);
SaveTrackInPosition(PlayerPrefs.GetInt ("CurrentLevel"), inputLevelName.text, trackDiff, beatStruct);
}
else if( CompareDifficulties(trackDiff, auxDiff) > 0 || (CompareDifficulties(trackDiff, auxDiff) == 0 && inputLevelName.text.CompareTo (auxName) < 0) )
{
// - Move and insert
Track auxTrack;
PlayerPrefs.SetInt ("LevelCount", numberOfLevels + 1);
for (int i = numberOfLevels; i > cont; i--) // -- Move everything one position to the right
{
auxTrack = GetTrackFromPosition ( i - 1 );
SaveTrackInPosition ( i, auxTrack.name, auxTrack.difficulty, auxTrack.beatTrack );
}
PlayerPrefs.SetInt ("CurrentLevel", cont);
SaveTrackInPosition ( cont, inputLevelName.text, trackDiff, beatStruct );
}
}
}
}
基本存储/检索功能:
public Track GetTrackFromPosition ( int position )
{
Track auxTrack;
auxTrack.name = PlayerPrefs.GetString (position + "Name");
auxTrack.level = PlayerPrefs.GetString (position + "Level");
auxTrack.difficulty = PlayerPrefs.GetString (position + "Difficulty");
auxTrack.beatTrack = Serialisation.DeserialiseStruct<BeatStruct>(PlayerPrefs.GetString (auxTrack.level));
return auxTrack;
}
public void SaveTrackInPosition ( int position, string name, string difficulty, BeatStruct beatTrack)
{
PlayerPrefs.SetString (position + "Name", name);
PlayerPrefs.SetString (position + "Difficulty", difficulty);
PlayerPrefs.SetString (position + "Level",
Serialisation.SerialiseStructToString<BeatStruct>(beatTrack));
}
答案 0 :(得分:0)
尝试在Awake方法中调用GetTrackFromPosition()并在SaveTrackInPosition()方法中调用PlayerPrefs.Save()(在那些SetString()之后)