C# 当游戏不使用Unity c时,需要帮助保持计时器运行吗#

C# 当游戏不使用Unity c时,需要帮助保持计时器运行吗#,c#,unity3d,C#,Unity3d,我正在用c#unity创建一个游戏,在应用程序关闭时,我很难保持计时器运行。我可以使用TimeSpan fine,在游戏结束时找到滴答声,然后立即滴答声,并将其转换为秒 我在游戏中有一种高级货币,我想每x秒增加一次(在这个例子中,我用10来测试)。我在游戏中有一个叫做showTimeLeft的倒计时,它每秒减少1。我还有一个save/load.cs,它保存所有变量并加载它们。我所做的是将游戏结束后的时间以秒为单位,再除以10秒,然后得到溢价货币,但我有一个大问题 当我结束游戏时,我说计时器是5秒

我正在用c#unity创建一个游戏,在应用程序关闭时,我很难保持计时器运行。我可以使用TimeSpan fine,在游戏结束时找到滴答声,然后立即滴答声,并将其转换为秒

我在游戏中有一种高级货币,我想每x秒增加一次(在这个例子中,我用10来测试)。我在游戏中有一个叫做showTimeLeft的倒计时,它每秒减少1。我还有一个save/load.cs,它保存所有变量并加载它们。我所做的是将游戏结束后的时间以秒为单位,再除以10秒,然后得到溢价货币,但我有一个大问题

当我结束游戏时,我说计时器是5秒,即使过了20秒,计时器也会在10秒重新启动。所以我想做的是从计时器中提取剩余时间,然后从游戏停止的总秒数中减去它,对总秒数(15)和滴答时间(10)进行调制,得到剩余的5秒,然后在我加载游戏时在5点重新启动计时器,取总秒数(15)/滴答时间(10)的整数,得到1。然后,我想将溢价货币增加1,因为当5秒变为0时,然后是1,因为总秒数和滴答时间的剩余整数是1,然后将时钟设置回5秒,这是剩余的

这听起来很让人困惑,写这个脚本让我很困惑,但我希望当我写下代码的时候,你们能理解我的意图。我在代码中添加了关于我试图做什么的注释,不确定是否100%正确

PremiumCurrencyTimerManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class PremiumCurrencyTimerManager : MonoBehaviour {

    public GameManager GM; //Game Manager, holds most of my variables
    public Text text; //Text that holds the timer, and premium currency and max premium currency I can hold.
    public Button collect; // this button collects the currency
    public int currencyToCollect; //This is the variable that holds my currency
    public int maxCurrencyToCollect; //Max of currency I can hold before I collect
    public int timeToTick = 10; //Time to tick +1 currency
    public TimeSpan showTimeLeft; //Time left on countdown
    public TimeSpan endTimeLeft; //0 seconds, if the time left = this variable then add a currency

    private void Awake()
    {
        showTimeLeft = TimeSpan.FromSeconds(timeToTick); //When this game first starts the time left = start time
        endTimeLeft = TimeSpan.FromSeconds(0); //variable that shows how many seconds to add a currency, when timer reaches 0 add a currency


        collect.onClick.AddListener(onCollect); //When the button is clicked call onCollect()        
    }

    private void Start()
    {
        StartCoroutine(getCurrency(showTimeLeft)); //Start the countdown, in the background.

        StartCoroutine(showTimer()); //Shows the actual countdown.
    }

    private void Update()
    {
        text.text = showTimeLeft + "\n\n" + currencyToCollect.ToString() + "/" + maxCurrencyToCollect.ToString();

        if (showTimeLeft <= endTimeLeft)
            showTimeLeft = TimeSpan.FromSeconds(timeToTick); //If the timer reaches 0, restart at timeToTick, I think my main problem with the save/load is that if I stop the game at 2 seconds, it starts the timer back up at 2 seconds because thats what was saved, and it adds a currency anyway.

    }

    public void onCollect() //Called when button is clicked, moves the currency I need to collect to my variable page so I can use it
    {
        GM.premiumCurrency += currencyToCollect;
        currencyToCollect = 0;
    }

    public void addCurrency() //Call this when the timer reaches 0 to add a currency
    {
        if (currencyToCollect < maxCurrencyToCollect)
            currencyToCollect++;
        else
            currencyToCollect = maxCurrencyToCollect;
    }

    public IEnumerator getCurrency(TimeSpan timeInSecs) //Background timer
    {
        int x = timeInSecs.Seconds;
        while (true)
        {
            yield return new WaitForSeconds(x);
            addCurrency();
        }
    }

    public IEnumerator showTimer() //Timer we see, subtracts 1 per second
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            showTimeLeft = showTimeLeft.Subtract(TimeSpan.FromSeconds(1));            
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用制度;
公共类PremiumCurrencyTimerManager:单行为{
publicgamemanager GM;//游戏管理器,保存我的大部分变量
public Text Text;//保存计时器的文本,以及我可以保存的高级货币和最高高级货币。
public按钮collect;//此按钮收集货币
public int currencyToCollect;//这是保存我的货币的变量
public int maxCurrencyToCollect;//我在收款前可以持有的最大货币
public int timeToTick=10;//勾选时间+1货币
public TimeSpan showTimeLeft;//倒计时剩余时间
public TimeSpan endTimeLeft;//0秒,如果剩余时间=此变量,则添加货币
私人空间
{
showTimeLeft=TimeSpan.FromSeconds(timeToTick);//此游戏首次开始时,剩余时间=开始时间
endTimeLeft=TimeSpan.FromSeconds(0);//变量,显示计时器达到0时添加货币所需的秒数添加货币
collect.onClick.AddListener(onCollect);//单击按钮时调用onCollect()
}
私有void Start()
{
Start例程(getCurrency(showTimeLeft));//在后台开始倒计时。
start例程(showTimer());//显示实际的倒计时。
}
私有void更新()
{
text.text=showtimelft+“\n\n”+currencyToCollect.ToString()+“/”+maxCurrencyToCollect.ToString();
if(showTimeLeft数据。showTimeLeft。秒)
{
totalSeconds-=data.showTimeLeft.Seconds;//减去这个,因为它是时钟上剩下的,然后添加一个高级货币
PCM.premiumCurrency++;
}
test=totalSeconds%PCM.timeToTick;//temp变量,该变量使用剩余的秒数和时间来计时,以获取加载游戏时需要在时钟上显示的秒数
PCM.maxCurrencyToCollect=data.maxCurrencyToCollect;
GM.premiumCurrency=data.premiumCurrency;
PCM.showTimeLeft=从秒开始的时间跨度(测试);
PCM.premiumCurrency=data.premiumCurrency;
mailboxTicks=(int)(totalSeconds/PCM.timeToTick);//这只是余数的int,因此我们可以将其添加为高级货币,我们在之前进行调制,并将其设置为剩余时间。我不想要高级货币的分数
if(邮箱标记+PCM.PremiumCurrent

我希望我现在有更多的道理,应该有一个更简单的方法来做到这一点!现在当我关闭游戏并加载它时得到的结果是,即使我只关闭并重新打开游戏2秒,它也会直接进入最大货币。

所以…
System.Time.now
?所以…
System.Time.now
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;

public class SaveLoad : MonoBehaviour{

    public GameManager GM;
    public PremiumCurrencyManager PCM;
    public long ticksSinceClose;
    public long totalSeconds;
    public int mailboxTicks;
    public long test;
    public long test2;
    public long b;


    public void Save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");

        PlayerData data = new PlayerData();

        data.currencyToCollect= PCM.currencyToCollect;
        data.maxCurrencyToCollect= PCM.maxCurrencyToCollect;
        data.premiumCurrency= GM.premiumCurrency;
        data.showTimeLeft = PCM.showTimeLeft;


        bf.Serialize(file, data);
        file.Close();
}

    public void Load()
    {
        if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            PlayerData data = (PlayerData)bf.Deserialize(file);
            file.Close();

            ticksSinceClose = System.DateTime.Now.Ticks - data.gameCloseTicks;
            totalSeconds = ticksSinceClose / TimeSpan.TicksPerSecond;

            if (totalSeconds > data.showTimeLeft.Seconds)
            {
                totalSeconds -= data.showTimeLeft.Seconds; //Subtract this because it was what was left on the clock, and add a premium currency
                PCM.premiumCurrency++;
            }

            test = totalSeconds % PCM.timeToTick; //temp variable that takes the remainder of the seconds and time to tick, to get the seconds that needs to be put on the clock when we load the game

            PCM.maxCurrencyToCollect= data.maxCurrencyToCollect;
            GM.premiumCurrency = data.premiumCurrency ;

            PCM.showTimeLeft = TimeSpan.FromSeconds(test);

            PCM.premiumCurrency = data.premiumCurrency;

            mailboxTicks = (int)(totalSeconds/ PCM.timeToTick); //this is just the int of the remainder, so we can add it as premium currency, we take the modulation before and make it the time left. I dont want fractions of a premium currency 

            if (mailboxTicks + PCM.premiumCurrency < PCM.maxCurrencyToCollect)
                PCM.premiumCurrency += mailboxTicks;
            else
                PCM.premiumCurrency = MaM.maxCurrencyToCollect;


        }
    }

    [System.Serializable]
    public class PlayerData
    {
        public long gameCloseTicks;

        public int currencyToCollect;
        public int maxCurrencyToCollect;
        public int PremiumCurrency;
        public TimeSpan showTimeLeft;
    }
}