当我在游戏编辑器或手机上启动游戏时,它会出现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)。唤醒后对每个游戏对象调用onapplicationpause。所以你的代码应该是:

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

您在注释中显示的延迟是由呈现而不是脚本造成的。

10-08 03:08