协程停止工作

Coroutine stops working

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

在这一个有效:

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),然后就停止了。 为什么会这样?

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

  • Unity - WaitForSeconds() does not work

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

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

在大多数情况下,这意味着您的对象或脚本变为非活动状态。检查此问题的最佳方法是将 OnDisable() 方法添加到您的脚本并从中调用日志记录