Unity 中玩家缺席时间
美好的一天,我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一些带有碰撞检测的方法,这些方法可以用于收集金币。创建一个金币模型,将上面的代码粘贴到其C#代码中即可收集金币,谢谢,希望对你有帮助
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