c# - 在 Unity C# 中延迟执行“If”语句
问题描述
我希望在第一个代码完成后直接执行一段特定的代码,而不是像它们当前正在运行的同时执行。
private void Update()
{
//This is the code to be executed first
if ((textActive == true) && (stopText == false))
{
Debug.Log("TextActive");
KeyText("On");
objectToEnable4.SetActive(true);
stopText = true;
}
//after which this code will execute to disable Object4
if (stopText == true)
{
objectToEnable4.SetActive(false);
}
}
两段代码都完美运行我只需要为第二个代码部分实现延迟我希望将代码延迟 2 秒以留出时间播放动画
我在这里先向您的帮助表示感谢。
解决方案
使用协程的好时机:
private void Update()
{
//This is the code to be executed first
if ((textActive == true) && (stopText == false))
{
Debug.Log("TextActive");
KeyText("On");
objectToEnable4.SetActive(true);
stopText = true;
StartCoroutine(myDelay());
}
}
IEnumerator myDelay()
{
// waits for two seconds before continuing
yield return new WaitForSeconds(2f);
if (stopText == true)
{
objectToEnable4.SetActive(false);
}
}
推荐阅读
- bash - 如何在bash中使用变量替换来回显字符串?
- github-actions - 带有分支过滤器的 GitHub Actions 标记过滤器
- android - api中需要的运行时权限小于23?
- amazon-elastic-beanstalk - 无法将 Route 53 上的子域分配给 Elastic Beanstalk 实例
- r - 我保存了一个 R 数据框,然后尝试立即加载它,但权限被拒绝
- reactjs - 在 React 中存储 JWT 令牌
- c# - 在 UWP 中,我将如何获取位于 Flyout 中、本身位于 ResourceDictionary 中的 Grid 实例?
- python - PyArrow / Dask to_parquet 分区所有空列
- python - 有什么建议可以改进我的代码(没有装饰器)?
- reactjs - 有没有办法在不使用 redux-persist 或类似中间件的情况下在 React/Redux 中持久化登录用户?