【问题标题】:Unity Earn Coins While Application ClosedUnity 在应用程序关闭时赚取金币
【发布时间】:2018-07-20 21:42:29
【问题描述】:

下面的代码: 目前是 void awake() 的一部分,其中 if 语句为第一次播放设置所有 playerPrefs 值。下面的 else 语句试图实现玩家重新打开应用时将获得多少金币。

如何修复:我如何使它只在应用程序重新打开时才运行此代码?不是每次都重新加载场景。

可能的改进:如果您看到更简单的方法来执行以下代码以防止时间作弊,我愿意接受建议。我只想要最多 24 小时的奖励金币,或者如果打开的时间少于该奖励金币,则给予同等价值的金币。

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

【问题讨论】:

  • 您需要调用此代码,而不是从场景脚本的Awake()Start(),而是从场景之间持续存在的东西中调用。
  • 除非您在某处设置服务器并从中获取时间(afaik),否则您无法做很多事情来防止时间作弊。但是您可以存储未来最远的时间,并检查新时间是否晚于给他们硬币之前的那个时间......用户仍然可以时间作弊,但如果他们在手机上修复时间,它会总是少于那个时间,他们不会得到硬币。
  • 我注意到 Candy Crush 有一种有趣的威慑力,可以避免时间欺骗生命。我已经在手机上提前了时钟以立即获得生命,但是当我的手机时钟恢复正常时,距离下一个生命还有多长时间的计时器是未来几天。
  • 为了扩展这一点,我认为所做的是将当时设备上的时间存储在所给予的生命的时间里,这样下一个生命就不会被给予,直到那个时间再次+ 30分钟。所以你可以做的是当硬币被给予时,存储时间,然后对下一个硬币进行时间检查。如果有人通过提前时钟作弊,当时钟回到网络时间时,他们将不得不等待时间赶上。

标签: c# unity3d save gameobject


【解决方案1】:

试试:

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

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

【讨论】:

  • 我不知道这有什么帮助?
  • 该代码应该只在应用程序重新启动时调用一次,而不是在场景重新加载时调用。
【解决方案2】:

这样做的方法是使用 OnApplicationfoucs() OnApplicationPause() OnApplicationClose()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多