gpt4 book ai didi

c# - 函数 "OnApplicationPause"在应用程序启动时起作用。安卓,unity3d

转载 作者:太空狗 更新时间:2023-10-29 13:44:35 27 4
gpt4 key购买 nike

当我在游戏编辑器或手机上启动我的游戏时,它会出现 2-3 秒的故障并调用函数 OnApplicationPause。但是当我在编辑器中关闭应用程序/暂停时它不会调用。这对我的项目不利,那么如何解决呢?有功能代码:

void OnApplicationPause()
{
DateTime time = DateTime.Now;
SaveVariablesInClass();
PlayerPrefs.SetString("SV", JsonUtility.ToJson(save)); //saves special class
}

void SaveVariablesInClass()
{
save.Bananas = bananas;
save.GoldBananas = goldBananas;

save.TwoBananasSpawnChance = twoBananasSpawnChance;
save.HigherBananaLevelChance = higherBananaLevelChance;
save.SimpleBananaStormChance = simpleBananaStormChance;

save.GoldBananaChance = goldBananaChance;
save.TwoGoldBananaChance = twoGoldBananaChance;

save.offlineEfficiency = offlineEfficiency;
save.offlineProductionTime = offlineProductionTime;

save.goldCoefficient = goldCoefficient;

save.BonusLevels = bonusLevels;
save.GoldBonusLevels = goldBonusLevels;
save.SimpleBonusLevelPrices = simpleBonusLevelPrices;
save.GoldBonusLevelPrices = goldBonusLevelPrices;
save.simpleMaxLevels = simpleMaxLevels;
save.goldMaxLevels = goldMaxLevels;

save.time = time.ToString();

save.CurrentSimpleBonus1 = currentSimpleBonus1;
save.LaunchedGame = launchedGame;
}

最佳答案

您在方法中缺少 bool 参数。它应该是 OnApplicationPause(bool pauseStatus)。在 Awake 之后,每个 GameObject 都会调用 OnApplicationPause。所以你的代码应该是:

void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
DateTime time = DateTime.Now;
SaveVariablesInClass();
PlayerPrefs.SetString("SV", JsonUtility.ToJson(save)); //saves special class
}
}

您在评论中显示的延迟是由渲染而非脚本引起的。

关于c# - 函数 "OnApplicationPause"在应用程序启动时起作用。安卓,unity3d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56775185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com