c# - 对象在一定时间后未出现
问题描述
因此,我将对象放入场景中,然后通过检查器(对象名称旁边的复选标记框)将其设置为“不可见”(如果您愿意,请停用),等待 8 秒后它不可见。我正在使用 Unity 2d 和 C#。
我让游戏开始暂停三秒钟,然后在有效之后播放。第一个脚本就是那个。该项目应该在 8 秒后重新出现,所以在游戏恢复后,这不起作用。
//delay before level starts script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class countDown : MonoBehaviour
{
public GameObject CountDown;
private void Start()
{
StartCoroutine("StartDelay");
}
void Update()
{
}
IEnumerator StartDelay()
{
Time.timeScale = 0;
float pauseTime = Time.realtimeSinceStartup + 3f;
while (Time.realtimeSinceStartup < pauseTime)
yield return 0;
CountDown.gameObject.SetActive(false);
Time.timeScale = 1;
}
{
//script for the flower to appear
IEnumerator Start()
{
print(Time.time);
yield return new WaitForSeconds(8);
print(Time.time);
flowerInScene.gameObject.SetActive(true);
}
[SerializeField] Transform flowerInScene;
}
解决方案
我仍然没有真正得到你的两种方法Start
你可以简单地StartCoroutine
在另一个协程的末尾调用 a ,这样你就可以将它们链接在一起(尽管通常有更好的方法来做你想做的事情):
using System.Collections;
using UnityEngine;
public class CountDown : MonoBehaviour
{
public GameObject CountDownObject;
public GameObject flowerObject;
private void Start()
{
StartCoroutine(Delay());
}
private IEnumerator Delay()
{
yield return new WaitForSeconds(3);
HideCountdown();
StartCoroutine(FlowerDelay());
}
private void HideCountdown()
{
CountDownObject.SetActive(false);
}
private IEnumerator FlowerDelay()
{
yield return new WaitForSeconds(8);
ShowFlower();
}
private void ShowFlower()
{
flowerObject.SetActive(true);
}
}
我个人不喜欢 Coroutines .. 它们有时并不那么容易调试。我更喜欢用简单的计时器做这样的事情(尽管在第一刻它看起来确实更糟)。优点是我现在可以直接在检查器中观看计时器倒计时:
using UnityEngine;
public class SimpleCountDown : MonoBehaviour
{
[Header("The Objects")]
public GameObject CountDownObject;
public GameObject FlowerObject;
[Header("Settings")]
// Here you can adjust the delay times
public float StartOffset = 3;
public float FlowerOffset = 8;
[Header("Debug")]
public float startTimer;
public float flowerTimer;
public bool isStartDelay;
public bool isFlowerDelay;
private void Start()
{
startTimer = StartOffset;
flowerTimer = FlowerOffset;
isStartDelay = true;
}
private void Update()
{
if (!isStartDelay && !isFlowerDelay) return;
if (isStartDelay)
{
startTimer -= Time.deltaTime;
if (startTimer <= 0)
{
HideCountdown();
isStartDelay = false;
isFlowerDelay = true;
}
}
if (isFlowerDelay)
{
flowerTimer -= Time.deltaTime;
if (flowerTimer <= 0)
{
ShowFlower();
isFlowerDelay = false;
this.enabled = false;
}
}
}
private void HideCountdown()
{
CountDownObject.SetActive(false);
}
private void ShowFlower()
{
FlowerObject.SetActive(true);
}
}
推荐阅读
- c# - 通过 sendinput 将鼠标点击发送到另一个应用程序,如何同步我的 c# 应用程序以等待处理这些鼠标点击?
- visual-c++ - RegGetValue 返回类型 REG_NONE
- spring - 有哪些具体案例可以证明 Spring 选择优于 Spring Boot 是合理的?
- python - 在 python 中创建具有分布作为插图的图形的最佳方法是什么?
- python - 向数据库发送信息时出错
- c# - StackExchange Redis 中的 KeysAsync 方法在哪里
- ios - 如何从其句柄(macOS / iOS)中获取库的路径?
- pandas - 在两列之间查找最早和最晚的日期
- python-3.x - 如何修复python sqlite数据库不存储数据
- javascript - Uncaught DOMException (in promise) - 在 serviceworker 中劫持特定的 GraphQL 请求