Unity3d Unity-PlayerRefs未保存在独立模式中
一个朋友让我帮他玩游戏,这个游戏是由另外两个程序员编写的。其中一个不是很清楚,所以部分代码被弄乱了,但这不是问题所在 这个游戏允许玩家编辑和保存新的关卡。以前的程序员使用PlayerPref来存储其中的每一个数据,而不是一个结构或类对象(我看不出其中的意义,但重新编程所有内容需要一些时间,我没有时间) 我所做的:我为每个级别添加了一个新参数,称为“难度”。在编辑器中保存时,级别应按顺序保存(从Easy A-Z到Hard A-Z)。我创建了两个用于保存/加载级别的通用函数,它们基本上是getString/setString和getInt/setInt,我调用它们来存储和检索它们。它在我的电脑上运行得很好 问题是:构建时,没有Unity的用户无法保存新编辑的关卡,也无法更新正在编辑的关卡。PlayerPrefs只是没有存储信息。 然而,它在我的身上起作用。我在编辑器中更改了一些内容,它们正确地发生了。我在我的游戏计算机上启动了一个构建(.exe),它确实保存了。奇怪的是,我注意到,至少在我的电脑中,编辑器和构建共享相同的信息,所以如果我保存或编辑一个级别,更改将持续到编辑器和其他级别 作为旁注,他们没有设置“退出”按钮,或者至少我还没有找到它,所以我必须alt+f4退出。我在saveLevel函数的末尾添加了PlayerPref.Save(),但它什么也没做。这不重要,因为当我得到游戏时,它是功能性的并且保存正确 有什么想法吗 编辑: 插入新级别:Unity3d Unity-PlayerRefs未保存在独立模式中,unity3d,Unity3d,一个朋友让我帮他玩游戏,这个游戏是由另外两个程序员编写的。其中一个不是很清楚,所以部分代码被弄乱了,但这不是问题所在 这个游戏允许玩家编辑和保存新的关卡。以前的程序员使用PlayerPref来存储其中的每一个数据,而不是一个结构或类对象(我看不出其中的意义,但重新编程所有内容需要一些时间,我没有时间) 我所做的:我为每个级别添加了一个新参数,称为“难度”。在编辑器中保存时,级别应按顺序保存(从Easy A-Z到Hard A-Z)。我创建了两个用于保存/加载级别的通用函数,它们基本上是getStr
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 void SaveLevel()
{
if(inputLevelName!=null)
{
SortBeatList();
//-获得玩家选择的难度
字符串trackDiff=“”;
if(easyDifficulty.isOn==true)
{
trackDiff=“(容易)”;
}
else if(mediumdefestion.isOn==true)
{
trackDiff=“(中等)”;
}
else if(hardDestionment.isOn==true)
{
trackDiff=“(硬)”;
}
string auxDiff=PlayerPrefs.GetString(PlayerPrefs.GetInt(“当前级别”)+“难度”);
字符串auxName=PlayerPrefs.GetString(PlayerPrefs.GetInt(“当前级别”)+“名称”);
//-保存当前级别
if(inputLevelName.text.CompareTo(auxName)==0&&compareDifficies(trackDiff,auxDiff)==0)
{
SaveTrackInputPosition(PlayerPrefs.GetInt(“当前级别”)、inputLevelName.text、trackDiff、beatStruct);
}
其他的
{
int numberOfLevels=PlayerPrefs.GetInt(“LevelCount”,-1);
int cont=0;
//-寻找正确的困难地点
while(cont0)
cont++;
auxDiff=PlayerPrefs.GetString(续+难度);
auxName=PlayerPrefs.GetString(cont+“Name”);
if(比较困难(trackDiff,auxDiff)==0)
//-搜索名称的正确空格
而(cont0)
{
cont++;
auxName=PlayerPrefs.GetString(cont+“Name”);
auxDiff=PlayerPrefs.GetString(续+难度);
}
auxName=PlayerPrefs.GetString(cont+“Name”);
如果(比较困难(trackDiff,auxDiff)==0&&inputLevelName.text.CompareTo(auxName)==0)/--替换现有曲目
{
//-更新或替换
PlayerPrefs.SetInt(“当前级别”,续);
SaveTrackInputPosition(PlayerPrefs.GetInt(“当前级别”)、inputLevelName.text、trackDiff、beatStruct);
}
否则如果(cont>=numberOfLevels)
{
//-最后插入
PlayerPrefs.SetInt(“LevelCount”,numberOfLevels+1);
PlayerPrefs.SetInt(“当前级别”,numberOfLevels);
SaveTrackInputPosition(PlayerPrefs.GetInt(“当前级别”)、inputLevelName.text、trackDiff、beatStruct);
}
else if(比较困难(trackDiff,auxDiff)>0 | |(比较困难(trackDiff,auxDiff)==0&&inputLevelName.text.CompareTo(auxName)<0))
{
//-移动并插入
轨道支架;
PlayerPrefs.SetInt(“LevelCount”,numberOfLevels+1);
对于(inti=numberOfLevels;i>cont;i-)/——将所有内容向右移动一个位置
{
auxTrack=GetTrackFromPosition(i-1);
SaveTrackInPosition(i,auxTrack.name,auxTrack.coverity,auxTrack.beatrack);
}
PlayerPrefs.SetInt(“当前级别”,续);
SaveTrackInputPosition(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));
}
公共轨道GetTrackFromPosition(内部位置)
{
轨道支架;
auxTrack.name=PlayerPrefs.GetString(位置+“名称”);
auxTrack.level=PlayerPrefs.GetString(位置+“级别”);
auxTrack.demobility=PlayerPrefs.GetString(位置+难度);
auxTrack.beatrack=serialization.DeserialiseStruct(PlayerPrefs.GetString(auxTrack.level));
返回auxTrack;
}
public void SaveTrackInPosition(int位置、字符串名称、字符串难度、BeatStruct beatTrack)
{
PlayerPrefs.SetString(位置+“名称”,名称);
PlayerPrefs.SetString(位置+难度),难度);
PlayerPrefs.SetString(位置