如果满足条件,如何添加24小时延迟



在我的游戏中,我试图制作一个函数,如果玩家获得超过10颗星,那么恒星生成者在接下来的24小时内不会生成恒星。当我不能在不影响产生恒星的随机函数的情况下使用单位系统函数时,我该如何处理?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Timers;
public class StarSpawner : MonoBehaviour   {
public Transform[] spawnPoints;
public GameObject[] blockPrefab;
public float timeBetweenWaves = 1f;
private float timeToSpawn = 2f;

// Start is called before the first frame update
void Spawn()
{
Instantiate(blockPrefab[Random.Range(0, blockPrefab.Length)], spawnPoints[Random.Range(0, 
spawnPoints.Length)]);
}
// Update is called once per frame
void Update()
{
if (Time.time >= timeToSpawn)
{
Spawn();
timeToSpawn = Time.time + timeBetweenWaves;
}
}

我曾经遇到过类似的问题,然后"保存";当前时间数据,即PlayerPrefs中的日期和时间。然后,我每隔一段时间就询问一下,看节省的时间是否比前一段时间长。这使得系统即使在游戏关闭时也能工作。

这也许不是最优雅的方式,但它很有效。

最新更新