退出应用程序时保存游戏 [英] Save game when exiting app

查看:123
本文介绍了退出应用程序时保存游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用统一C#制作游戏.我已经为游戏编写了一个保存本地并保存云的代码.我按下按钮后会保存游戏,然后退出按钮.

I am making a game with unity 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 :)

谢谢

丹尼斯

推荐答案

因此,当在移动设备上按下主页"按钮时,将统一 做什么?

So in unity when home button pressed at device mobile what will unity do ?

Unity无法控制.操作系统可以.您没有提到哪个操作系统(Android或iOS),但是您可以研究每个按钮都按下主屏幕按钮会发生什么情况.

Unity does not control this. 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上暂停了一段时间.有时,Android会卸载资产.这取决于许多因素,例如应用程序已暂停多长时间,打开了多少应用程序以及设备上的可用内存.

What I can tell you is that the app is suspended for a while on Android. Sometimes, the assets are unloaded by 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.

您不能依靠操作系统来处理此问题.您必须自己处理.

如果它暂停了应用程序/游戏,那么当 玩家按下主屏幕按钮,我们可以保存游戏吗?

If it pause the application / game could we detect it first when the player press the home button we could do saving the game ?

是和否

您应该检测何时按下主页"按钮以执行某项操作.但是,您应该检测游戏暂停和恢复的时间.

You should not detect when the Home Button is pressed in order to do something. You should however detect when the Game is paused and resumed.

这是 OnApplicationPause 函数的作用.另一个类似的功能是 OnApplicationFocus ,但

This is what the OnApplicationPause function is used for. Another similar function is OnApplicationFocus but OnApplicationPause is more approprite for this.

当按下主页按钮时调用OnApplicationPause( bool pauseStatus )时,pauseStatus参数为true.重新打开应用程序后,pauseStatus参数为false.

When OnApplicationPause( bool pauseStatus ) is called when the Home Button is pressed, the pauseStatus parameter is true. When the app is re-opened, pauseStatus parameter is false.

这是为了让玩家不会丢失游戏数据

This is for the player to not lose the game data

保存加载数据,具体取决于pauseStatus值.

You save and load the data depending on pauseStatus value.

这是做什么:

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
    }
}

这篇关于退出应用程序时保存游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆