申请关闭时Unity赚钱

时间:2018-07-20 21:42:29

标签: c# unity3d save gameobject

下面的代码:当前是void awake()的一部分,其中if语句设置了所有第一次播放的playerPrefs值。下面的else语句试图实现当玩家重新打开应用程序时可以赚取多少枚硬币。

如何解决::如何制作它,以便仅在重新打开应用程序时才运行此代码?并非每次都重新加载场景。

可能的改进:如果您发现执行以下代码以防止时间作弊的简便方法,欢迎提出建议。我最多只需要24小时的红利硬币,或者如果开成少于24Hr的硬币,则会得到同等价值的硬币。

 else
 { 
    // Player Restart Game
    // Check Time for coin bonus

    ButtonCanvas.gameObject.SetActive(false);
    OfflineCanvas.gameObject.SetActive(true);

    // Same Year
    if (PlayerPrefs.GetInt("year").Equals(System.DateTime.Now.Year))
    {
        // Same Month
        if (PlayerPrefs.GetInt("month").Equals(System.DateTime.Now.Month))
        {
            // Same Day
            if(PlayerPrefs.GetInt("day").Equals(System.DateTime.Now.Day))
            { 
                // Add Coin Bouns for hours/min passed
                offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (((System.DateTime.Now.Hour - PlayerPrefs.GetInt("timeHour")) * 60) + (System.DateTime.Now.Minute - PlayerPrefs.GetInt("timeMin")));

            } 
            else // Different Day
            { 
                // Update if new day is higher than old
                if (PlayerPrefs.GetInt("day") < System.DateTime.Now.Month)
                {
                    PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
                    PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
                    PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);

                    // Add Coin Bonus For Time 24 hrs
                    offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
                }
            }
        } 
        else // Different Month
        { 
            // Update if new month is higher than old
            if (PlayerPrefs.GetInt("month") < System.DateTime.Now.Month)
            {
                PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
                PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
                PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
                PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);

                // Add Coin Bonus For Time 24 hrs
                offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
            }
        }
    } 
    else // Different Year
    { 
        // Update if new year is higher than old
        if (PlayerPrefs.GetInt("year") < System.DateTime.Now.Year){
            PlayerPrefs.SetInt("year", System.DateTime.Now.Year);
            PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
            PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
            PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
            PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);

            // Add Coin Bonus For Time 24 hrs
            offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试:

static bool IsCalled;
else{ //Player Restart Game
    //////
    /// Check Time for coin bonus
    //////
    if (!IsCalled)
    {
    ButtonCanvas.gameObject.SetActive(false);
    OfflineCanvas.gameObject.SetActive(true);

    if(PlayerPrefs.GetInt("year").Equals(System.DateTime.Now.Year)){ // Same Year
        if(PlayerPrefs.GetInt("month").Equals(System.DateTime.Now.Month)){ // Same Month
            if(PlayerPrefs.GetInt("day").Equals(System.DateTime.Now.Day)){ // Same Day
                // Add Coin Bouns for hours/min passed

                offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (((System.DateTime.Now.Hour - PlayerPrefs.GetInt("timeHour")) * 60) + (System.DateTime.Now.Minute - PlayerPrefs.GetInt("timeMin")));

            } else { // Different Day
                // Update if new day is higher than old
                if (PlayerPrefs.GetInt("day") < System.DateTime.Now.Month)
                {
                    PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
                    PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
                    PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
                    // Add Coin Bonus For Time 24 hrs
                    offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
                }
            }
        } else { // Different Month

            // Update if new month is higher than old
            if (PlayerPrefs.GetInt("month") < System.DateTime.Now.Month)
            {
                PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
                PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
                PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
                PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
                // Add Coin Bonus For Time 24 hrs
                offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
            }

        }
    } else { // Different Year
        // Update if new year is higher than old
        if(PlayerPrefs.GetInt("year") < System.DateTime.Now.Year){
            PlayerPrefs.SetInt("year", System.DateTime.Now.Year);
            PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
            PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
            PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
            PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
            // Add Coin Bonus For Time 24 hrs
            offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
        }
    }
    IsCalled = true;
}
}

布尔值将保持不变,直到重置游戏为止。

答案 1 :(得分:0)

方法是使用OnApplicationfoucs()OnApplicationPause()OnApplicationClose()