当卸载当前运行场景时,我想存储我的数据。为此,我编写了以下代码:
void OnDisable ()
{
BackUpPuzzleData();
}
public void BackUpPuzzleData ()
{
if (DataStorage.RetrievePuzzleStatus (difficultyLevel, puzzleId) == Constants.PUZZLE_NOT_OPENED
&& DataStorage.RetrievePuzzleStatus (difficultyLevel, puzzleId) != Constants.PUZZLE_COMPLETED)
DataStorage.StorePuzzleStatus (difficultyLevel, puzzleId, Constants.PUZZLE_RUNNING);
if (DataStorage.RetrievePuzzleStatus (difficultyLevel, puzzleId) == Constants.PUZZLE_RUNNING)
StorePuzzleData ();
}
private void StorePuzzleData ()
{
DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);
foreach (Transform cell in gridTransform) {
CellInformation cellInfo = cell.GetComponent<CellInformation> ();
if (cellInfo != null) {
CellStorage.StorePuzzleCellNumber (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.number);
CellStorage.StorePuzzleCellColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellColor);
CellStorage.StorePuzzleCellDisplayColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellDisplayColor);
}
}
}但是,当OnDisable方法在该时间控制台被调用时,给出了以下错误:

我已经在项目设置中设置了凭证的执行顺序,那么为什么我会遇到这样的错误呢?
目标:,基本上我想保存当前的游戏数据,所以当游戏玩家返回时,他可以重新开始,他离开了游戏。
发布于 2016-04-30 11:41:06
经过一段时间这个问题,现在我能解决这个问题了。我不知道有多少解决方案是正确的,但至少现在为我工作。
我在这里讨论这个问题,是因为有些议员从中得到了一些启示。
在游戏加载时,我用所有单元格的脚本填充了一个列表。网格的每个单元格都有一个附加CellInformation.的脚本所以我准备了所有这些细胞脚本的列表。
在游戏禁用时,我从这些脚本中获取值并存储到本地存储中。我可以访问脚本,但不能从列表中的游戏对象,因为他们已经被销毁。
void OnDisable ()
{
StorePuzzleData ();
}
private void StorePuzzleData ()
{
DataStorage.StorePuzzleTimePassed (difficultyLevel, puzzleId, GameController.gamePlayTime);
foreach (CellInformation cellInfo in cellInfoList) {
CellStorage.StorePuzzleCellNumber (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.number);
CellStorage.StorePuzzleCellColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellColor);
CellStorage.StorePuzzleCellDisplayColor (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.CellDisplayColor);
CellStorage.StorePuzzleCellGroupComplete (difficultyLevel, puzzleId, cellInfo.RowIndex, cellInfo.ColIndex, cellInfo.IsGroupComplete ? 1 : 0);
}
}我希望这将给其他程序员一些提示。
发布于 2016-04-27 19:53:26
我有一个建议,而不是保存当场景卸载,你可以保存每次玩家采取行动,这种方式,即使游戏崩溃,你可以恢复从最后一次玩家采取行动。
这个场景的卸载,会自动发生,或者用户会做任何动作(比如点击一个按钮)?如果这是一个player事件,您应该将保存代码放在该按钮上,因为据我所能研究的,在对象被销毁后,将调用OnDisable行为,这使得您无法存储它们的数据。
https://stackoverflow.com/questions/36895583
复制相似问题