简体   繁体   English

退出应用时保存游戏

[英]Save game when exiting app

I am making a game with unity C#. 我正在制作一个团结C#的游戏。 I have code a save local and save cloud for the game. 我有代码保存本地并为游戏保存云。 I make a button exit when pressed the game is saved and then exit. 当按下游戏保存然后退出时,我按下一个按钮退出。

But i have a problem in here saved game. 但我在这里保存的游戏有问题。 When someone or player press the home button which on device mobile the game will exit and run in background. 当某人或玩家按下设备移动设备上的主页按钮时,游戏将退出并在后台运行。

If the player restart the device the game that run in background will not saved to local or cloud. 如果玩家重启设备,则在后台运行的游戏将不会保存到本地或云端。 This is the problem. 这就是问题。

So in unity when home button pressed at device mobile what will unity do ? 所以在统一当主页按钮按下设备移动时,统一会做什么? It is pause the application / game or whatever ? 这是暂停应用程序/游戏或其他什么? What can we do in unity if the player press the home button ? 如果玩家按下主页按钮,我们可以做什么?

If it pause the application / game could we detect it first when the player press the home button we could do saving the game ? 如果暂停应用程序/游戏,我们可以在玩家按下主页按钮时首先检测到它,我们可以保存游戏吗? ( i mean run a code after the button home is pressed ) (我的意思是在按下按钮后运行代码)

Or any idea from your experience what will do if player press home button ? 或者根据你的经验,如果玩家按下主页按钮会怎么做? This is for the player to not lose the game data. 这是为了让玩家不丢失游戏数据。

Please give the detail for me to find the solution. 请详细说明我找到解决方案。

Any help would be amazing for continue the game :) 对于继续比赛,任何帮助都会很棒:)

Thanks 谢谢

Dennis 丹尼斯

So in unity when home button pressed at device mobile what will unity do ? 所以在统一当主页按钮按下设备移动时,统一会做什么?

Unity does not control this. Unity无法控制这一点。 The OS does. 操作系统。 You did not mention which OS (Android or iOS) but you can research what happens when the home button is pressed for each one. 您没有提到哪个操作系统(Android或iOS),但您可以研究每个操作系统按下主页按钮时会发生什么。

What I can tell you is that the app is suspended for a while on Android. 我可以告诉你的是,该应用程序在Android上暂停了一段时间。 Sometimes, the assets are unloaded by Android. 有时,Android会卸载资产。 This depends on many factors such as how long the app has been suspended, how many apps are open and the memory available on the device. 这取决于许多因素,例如应用程序已暂停多长时间,打开的应用程序数量以及设备上可用的内存。

You can't depend on the OS to handle this. 你不能依靠操作系统来处理这个问题。 You must handle it yourself. 你必须自己处理它。

If it pause the application / game could we detect it first when the player press the home button we could do saving the game ? 如果暂停应用程序/游戏,我们可以在玩家按下主页按钮时首先检测到它,我们可以保存游戏吗?

Yes and No. 是和否。

You should not detect when the Home Button is pressed in order to do something. 当为了做一些按下Home键,则不应检测。 You should however detect when the Game is paused and resumed. 但是,您应该检测游戏暂停和恢复的时间。

This is what the OnApplicationPause function is used for. 这就是OnApplicationPause函数的用途。 Another similar function is OnApplicationFocus but OnApplicationPause is more approprite for this. 另一个类似的功能是OnApplicationFocus但是OnApplicationPause更适合这个。

When OnApplicationPause( bool pauseStatus ) is called when the Home Button is pressed, the pauseStatus parameter is true . 按下Home按钮时调用OnApplicationPause( bool pauseStatus )时, pauseStatus参数为true When the app is re-opened, pauseStatus parameter is false . 重新打开应用程序时,pauseStatus参数为false

This is for the player to not lose the game data 这是为了让玩家不丢失游戏数据

You save and load the data depending on pauseStatus value. 您可以根据pauseStatus值保存加载数据。

Here is what to do: 这是做什么的:

void OnApplicationPause(bool pauseStatus)
{
    if (pauseStatus)
    {
        Debug.Log("Paused");
        //Save Player Settings
        //https://stackoverflow.com/q/40078490/3785314
    }
    else
    {
        Debug.Log("resumed");
        //Load Player Settings
        //https://stackoverflow.com/q/40078490/3785314
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM