我有一个使用guiTexts设置的场景:等级1:,等级2:,...我有一个使用序列化的工作保存/加载系统,但我不知道如何保存每个级别的分数和将它们显示在不同场景中的guiTexts中。我也不知道如何测试我到目前为止目前是否正常工作。
这里是保存脚本,它在级别结束时保存并从其他脚本获取分数并将其保存到高分数arraylist,然后将其保存到GameControl脚本中的Highscores arraylist:
public class Saving : MonoBehaviour
{
ArrayList highscores = new ArrayList();
ScoreManager Score;
void Start(){
Score = GameObject.Find("Score").GetComponent<ScoreManager>();
}
void OnTriggerEnter( Collider other)
{
if(other.gameObject.tag == "Player") //when player hits the collider at end of the level
{
int newScore = (int)Score.score; //get score and put it in int in newScore
int rawlevelCount = Application.levelCount;
int dontCountMenus = 2;
int levelCount = rawlevelCount - dontCountMenus;
highscores.Insert(levelCount, newScore);
GameControl.control.Highscores = highscores; //save current highscores to GameControl Highscores
GameControl.control.Save(); //saveGame
}
应该序列化数组的GameControl脚本:
public class GameControl : MonoBehaviour {
public static GameControl control;
public int levelcount;
public ArrayList Highscores = new ArrayList();
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.levelcount = levelcount;
data.Highscores = Highscores;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
levelcount = data.levelcount;
Highscores = data.Highscores;
}
}
[Serializable]
class PlayerData
{
public int levelcount;
public ArrayList Highscores;
}
这些脚本都不会出错。
现在我需要知道的是:我如何将数组中的变量写入正确的GuiText这样的东西?
guiText("Level1score") "Level 1:" + score from array position 1;
guiText("Level2score") "Level 2:" + score from array position 2;
等至少至少55并且能够随时添加更多级别(因此我使用arraylist而不是普通数组)