简体   繁体   English

执行此代码时游戏停止运行 Unity C#

[英]The game stops working when performing this code Unity C#

I have a code that once every 24 hours (86400000 milliseconds) if the player's score is over 7000 points, then it divides all points above 7000 by 2 , but the problem is that when I get 7000+ points, the game freezes and no longer works never (everything worked well in unity).我有一个代码,如果玩家的分数超过7000分,则每 24 小时(86400000 milliseconds)一次,然后将所有高于7000 by 2 ,但问题是当我获得7000+分时,游戏会冻结并且不再从不工作(一切都很好地统一)。 What could be the reason?可能是什么原因?

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

public class LeagueClock : MonoBehaviour
{
    private ulong TimeLastReset;
    [SerializeField] private float msToWait = 86400000f;
    [SerializeField] private int scoreThreshold = 7000;
    private int scrMain;
    private int MainCoins;
    private int LegendaryTrophies;


    private void Start(){
        LegendaryTrophies = PlayerPrefs.GetInt("LegendaryTrophies");
        scrMain = PlayerPrefs.GetInt("scrMain");
        TimeLastReset = ulong.Parse(PlayerPrefs.GetString("LastReset", "0"));
        MainCoins = PlayerPrefs.GetInt("MainCoins");

        Debug.Log(DateTime.Now);
    }

    private void Update(){
        if (scrMain > 7000) {
            //apply the action for each interval that has passed.
            //For example, if the interval is 24 hours, and 49 hours
            //have passed, that's 2 intervals, so we reduce the score
            //twice.
            int intervalsPassed = GetIntervalsPassed();
            if (intervalsPassed > 0){
                for (int i = 0; i < intervalsPassed; i++) {
                    ReduceScore();
                }
                PlayerPrefs.SetInt("scrMain", scrMain);
                TimeLastReset = (ulong)DateTime.Now.Ticks;
                PlayerPrefs.SetString("LastReset", TimeLastReset.ToString());
            }
        }
    }

    private void ReduceScore() {
        MainCoins += ((scrMain-scoreThreshold)/2)*100;
        LegendaryTrophies += (scrMain-scoreThreshold)/2;
        scrMain -= (scrMain-scoreThreshold)/2;

        PlayerPrefs.SetInt("MainCoins", MainCoins);
        PlayerPrefs.SetInt("scrMain", scrMain);
        PlayerPrefs.SetInt("LegendaryTrophies", LegendaryTrophies);
        PlayerPrefs.Save();
    }

    //returns the number of full time intervals that have passed since 
    //we last reduced the score
    private int GetIntervalsPassed(){

            ulong diff = ((ulong)DateTime.Now.Ticks - TimeLastReset);
            ulong ms = diff / TimeSpan.TicksPerMillisecond;
    
            float secondsLeft = (float)(msToWait - ms) / 1000.0f;
            int intervalsPassed = Mathf.FloorToInt(ms / msToWait);
            
            Debug.Log($"SecondsLeft: {secondsLeft} | Intervals: {intervalsPassed} | Score: {scrMain}");        
            return intervalsPassed;
    }
}

It might memory problem that affects your game you can look at these documents, and I hope this helps Unity Memory memory 问题可能会影响你的游戏你可以看看这些文件,我希望这对Unity有帮助 Memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM