Unity 中玩家缺席时间

发布于 2025-01-16 13:19:00 字数 2220 浏览 2 评论 0原文

美好的一天,我正在 Unity 2020.3.30 上制作一个 2d android 游戏。当玩家不在游戏中时我正在累积金钱。问题是,如果你点击107个金币并退出游戏5秒,那么钱将不是112(应该是(107个金币+5个金币))而是105,即100个金币,出于某种原因,添加了秒数,而不是 107。如何修复它?

脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class MainMenu : MonoBehaviour
{
    [SerializeField] int money;
    public int total_money;
    public Text moneyText;
 
    private void Start()
    {
        money = PlayerPrefs.GetInt("money");
        total_money = PlayerPrefs.GetInt("total_money");
        bool isFirst = PlayerPrefs.GetInt("isFirst") == 1 ? true : false;
        if (isFirst)
        {
            StartCoroutine(IdleFarm());
        }
        OfflineTime();
    }
 
    public void ButtonClick()
    {
        money++;
        total_money++;
        PlayerPrefs.SetInt("money", money);
        PlayerPrefs.SetInt("total_money", total_money);
    }
 
    IEnumerator IdleFarm()
    {
        yield return new WaitForSeconds(1);
        money++;
        Debug.Log(money);
        PlayerPrefs.SetInt("money", money);
        StartCoroutine(IdleFarm());
    }
 
    private void OfflineTime()
    {
        TimeSpan ts;
        if (PlayerPrefs.HasKey("LastSession"))
        {
            ts = DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("LastSession"));
            Debug.Log(string.Format("You were gone for {0} days, {1} hours, {2} minutes, {3} seconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds));
            money += (int)ts.TotalSeconds;
        }
    }
 
#if UNITY_ANDROID && !UNITY_EDITOR
 
    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            PlayerPrefs.SetString("LastSession", DateTime.Now.ToString());
        }
    }
#else
    private void OnApplicationQuit()
    {
        PlayerPrefs.SetString("LastSession", DateTime.Now.ToString());
    }
#endif
 
    public void ToAchievements()
    {
        PlayerPrefs.SetString("LastSession", DateTime.Now.ToString());
        SceneManager.LoadScene(1);
    }
 
    // Update is called once per frame
    void Update()
    {
        moneyText.text = money.ToString();
    }
}

Good day, I am making a 2d android game on Unity 2020.3.30. I am accruing money while the player is not in the game. The problem is, if you click 107 coins and exit the game for 5 seconds, then the money will not be 112 (as it should be (107 coins + 5 coins)) but 105, that is to 100 coins, for some reason, the number of seconds is added, and not to 107. How to fix it?

Script:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class MainMenu : MonoBehaviour
{
    [SerializeField] int money;
    public int total_money;
    public Text moneyText;
 
    private void Start()
    {
        money = PlayerPrefs.GetInt("money");
        total_money = PlayerPrefs.GetInt("total_money");
        bool isFirst = PlayerPrefs.GetInt("isFirst") == 1 ? true : false;
        if (isFirst)
        {
            StartCoroutine(IdleFarm());
        }
        OfflineTime();
    }
 
    public void ButtonClick()
    {
        money++;
        total_money++;
        PlayerPrefs.SetInt("money", money);
        PlayerPrefs.SetInt("total_money", total_money);
    }
 
    IEnumerator IdleFarm()
    {
        yield return new WaitForSeconds(1);
        money++;
        Debug.Log(money);
        PlayerPrefs.SetInt("money", money);
        StartCoroutine(IdleFarm());
    }
 
    private void OfflineTime()
    {
        TimeSpan ts;
        if (PlayerPrefs.HasKey("LastSession"))
        {
            ts = DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("LastSession"));
            Debug.Log(string.Format("You were gone for {0} days, {1} hours, {2} minutes, {3} seconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds));
            money += (int)ts.TotalSeconds;
        }
    }
 
#if UNITY_ANDROID && !UNITY_EDITOR
 
    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            PlayerPrefs.SetString("LastSession", DateTime.Now.ToString());
        }
    }
#else
    private void OnApplicationQuit()
    {
        PlayerPrefs.SetString("LastSession", DateTime.Now.ToString());
    }
#endif
 
    public void ToAchievements()
    {
        PlayerPrefs.SetString("LastSession", DateTime.Now.ToString());
        SceneManager.LoadScene(1);
    }
 
    // Update is called once per frame
    void Update()
    {
        moneyText.text = money.ToString();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2025-01-23 13:19:01

您可以尝试一些带有碰撞检测的方法,这些方法可以用于收集金币。创建一个金币模型,将上面的代码粘贴到其C#代码中即可收集金币,谢谢,希望对你有帮助

   void OnTriggerEnter(Collider col)
  {
     if (col.gameObject.tag == "Player")//If the colliding object is the player
  {
         money++;//money plus one
        Destroy(this.gameObject);//Destroy coins
    }
}

You can try some methods with collision detection, which can be applied to collect gold coins. Create a gold coin model and paste the above code in its C# code to collect gold coins,Thank you, hope it helps you

   void OnTriggerEnter(Collider col)
  {
     if (col.gameObject.tag == "Player")//If the colliding object is the player
  {
         money++;//money plus one
        Destroy(this.gameObject);//Destroy coins
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文