Перезагрузка сцены с последующим перемещением персонажа
Добавлено: 10 фев 2018, 13:47
Всем доброго времени суток. При разработке загрузчика сохранений для 2D-игры столкнулся со следующей проблемой. Дело в том, что моя загрузка подразумевает перезагрузку сцены с последующим перемещением персонажа к чекпойнту. Сначала я десериализую нужные параметры из файла, затем пытаюсь перезагрузить сцену и переписать новые значения параметров. Однако, как я понял, сцена грузится лишь со следующего кадра, а параметры переписываются в текущем, т.е. в моём контексте это значит то, что сначала параметры обновляются, а затем сбрасываются на те, которые были при старте сцены - мы всегда начинаем заново. В общем, что-то не так с порядком действий. Не могли бы вы подсказать, как можно его изменить? Заранее благодарен.
Код загрузчика на C#:
Код загрузчика на C#:
Синтаксис:
Используется csharp
public void LoadGame() // НЕ РАБОТАЕТ - СЦЕНА ВСЕГДА ПОСЛЕ ПРИСВАИВАНИЙ
{
if (File.Exists(Application.dataPath + "/Saved Games/Checkpoint.dgs"))
{
FileStream f = new FileStream(Application.dataPath + "/Saved Games/Checkpoint.dgs", FileMode.OpenOrCreate);
BinaryFormatter form = new BinaryFormatter();
try
{
int curl = (int)form.Deserialize(f);
Pos p = (Pos)form.Deserialize(f);
bool fr = (bool)form.Deserialize(f);
Debug.Log("Params: " + curl + " " + p.x + " " + p.y + " " + fr);
SceneManager.LoadScene(curl);
GameObject.Find("Hero").transform.position = new Vector2(p.x, p.y);
Debug.Log(p.x + " " + p.y);
Debug.Log("Deserialized face: " + fr);
GameObject.Find("Hero").GetComponent<characterController>().facingRight = fr;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
finally
{
GameObject.Find("Hero").GetComponent<characterController>().health = 3;
GameObject.Find("Hero").GetComponent<BonusScript>().points = 0;
f.Close();
}
}
else
{
Debug.Log("Save data not found. Starting from the beginning of the level");
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
{
if (File.Exists(Application.dataPath + "/Saved Games/Checkpoint.dgs"))
{
FileStream f = new FileStream(Application.dataPath + "/Saved Games/Checkpoint.dgs", FileMode.OpenOrCreate);
BinaryFormatter form = new BinaryFormatter();
try
{
int curl = (int)form.Deserialize(f);
Pos p = (Pos)form.Deserialize(f);
bool fr = (bool)form.Deserialize(f);
Debug.Log("Params: " + curl + " " + p.x + " " + p.y + " " + fr);
SceneManager.LoadScene(curl);
GameObject.Find("Hero").transform.position = new Vector2(p.x, p.y);
Debug.Log(p.x + " " + p.y);
Debug.Log("Deserialized face: " + fr);
GameObject.Find("Hero").GetComponent<characterController>().facingRight = fr;
}
catch (System.Exception e)
{
Debug.Log(e.Message);
}
finally
{
GameObject.Find("Hero").GetComponent<characterController>().health = 3;
GameObject.Find("Hero").GetComponent<BonusScript>().points = 0;
f.Close();
}
}
else
{
Debug.Log("Save data not found. Starting from the beginning of the level");
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}