gpt4 book ai didi

c# - 协程停止工作

转载 作者:行者123 更新时间:2023-11-30 23:31:19 25 4
gpt4 key购买 nike

我有两个脚本,其中有一个协程。它在第一个中工作得很好,但在第二个中却没有,原因不明。

在这一个中有效:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.ImageEffects;

public class GameStartController : MonoBehaviour {
public Button startButton;
public GameObject cubeSpawner;

// Use this for initialization
private void Start() {
startButton = startButton.GetComponent<Button>();
}

public void StartGame() {
EnableCubeSpawner();
SpawnStartingCubes();
HideStartMenu();
StartCoroutine("FocusCamera");
PlayBackgroundMusic();
}

// Enables the cube spawner, so it can start spawning cubes
private void EnableCubeSpawner() {
cubeSpawner.SetActive(true);
}

private void SpawnStartingCubes() {
cubeSpawner.GetComponent<CubeSpawner>().GenerateStartingCubes();
}

private void PlayBackgroundMusic() {
var audio = GameObject.FindWithTag("Audio").GetComponent<AudioController>();
audio.PlayBackgroundMusic();
}

private void HideStartMenu() {
startButton.transform.parent.GetComponent<CanvasGroup>().interactable = false;
startButton.transform.parent.GetComponent<CanvasGroup>().alpha = 0f;
}

private IEnumerator FocusCamera() {
var camera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
var velocity = 0f;

while (Mathf.Abs(camera.GetComponent<DepthOfField>().aperture) > 0.001f) {
Debug.Log(Mathf.Abs(camera.GetComponent<DepthOfField>().aperture));

camera.GetComponent<DepthOfField>().aperture = Mathf.SmoothDamp(camera.GetComponent<DepthOfField>().aperture, 0f, ref velocity, 0.3f);
yield return null;
}

camera.GetComponent<DepthOfField>().aperture = 0f;
}
}

协程工作得很好,相机光圈从 0.6 平滑地变为 0。

然而在第二个脚本中这并没有发生:

using System.Collections;
using System.Linq;
using UnityEngine;
using UnityStandardAssets.ImageEffects;

public class GameOverController : MonoBehaviour {
public void EndGame() {
StartCoroutine("UnfocusCamera");
DisableCubeSpawner();
DestroyAllCubes();
StopBackgroundMusic();
ShowStartMenu();
}

// Disables the cube spawner, so it can stop spawning cubes
private void DisableCubeSpawner() {
var cubeSpawner = GameObject.FindWithTag("CubeSpawner");
cubeSpawner.SetActive(false);
}

private void DestroyAllCubes() {
var gameObjects = FindObjectsOfType(typeof(GameObject));

foreach (var gameObject in gameObjects.Where(gameObject => gameObject.name.Contains("Cube"))) {
Destroy(gameObject);
}
}

private void StopBackgroundMusic() {
var audio = GameObject.FindWithTag("Audio").GetComponent<AudioController>();
audio.StopBackgroundMusic();
}

private void ShowStartMenu() {
var startMenu = GameObject.FindWithTag("StartMenu");
startMenu.GetComponent<CanvasGroup>().interactable = true;
startMenu.GetComponent<CanvasGroup>().alpha = 1f;
}

private IEnumerator UnfocusCamera() {
var camera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
var velocity = 0f;

while (camera.GetComponent<DepthOfField>().aperture < 0.6f) {
Debug.Log(Mathf.Abs(camera.GetComponent<DepthOfField>().aperture));
camera.GetComponent<DepthOfField>().aperture = Mathf.SmoothDamp(camera.GetComponent<DepthOfField>().aperture, 0.6f, ref velocity, 0.3f);
yield return null;
}

// camera.GetComponent<DepthOfField>().aperture = 0f;
}
}

它只适用于一帧(光圈从 0 到 0.03),然后就停止了。为什么会这样?

最佳答案

如果您销毁(或禁用)一个游戏对象,则在附加到它的组件上运行的协程将停止。我找不到这方面的主要来源,但这里有另外两个堆栈溢出问题:

协程停止是因为您的 GameOverController 附加到的 GameObject 被销毁了。推测Unity在恢复协程之前会检查对象是否仍然存在,如果对象被销毁,Unity不会继续执行它。

要解决此问题,您可以延迟销毁 GameObject 直到动画完成(也许将销毁代码放在协程中的 while 循环之后)或将组件放在不会被销毁的 GameObject 上。

关于c# - 协程停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com