使用 'IEnumerator' 在对象移出相机视野后立即重新定位对象)

Using 'IEnumerator' to reposition object as soon as it moves outside of camera's field of view)

此代码应在 gameObject 离开相机视野后立即重新定位它。但是,gameObject 永远不会重新定位...

IEnumerator Reposition(NinjaStar ninjaStar) {
    bool onScreen = true;
    Renderer renderer = ninjaStar.gameObject.GetComponent<Renderer> ();

    yield return new WaitForSeconds (1);

    while (onScreen) {
        if (!renderer.isVisible) {
            ninjaStar.Initiate (0, 0, Vector3.zero); //this code never gets called
            ninjaStar.transform.position = poolPosition; //this code never gets called
            onScreen = false; //this code never gets called
        }
        yield return null;
    }
}

Renderer.isVisible 文档状态:

When running in the editor, the scene view cameras will also cause this value to be true.

因此请尝试将您的项目构建到您喜欢的平台并在那里进行测试(例如为 windows 构建并测试 .exe)。

或者,reposition/rotate 您的场景视图相机,这样它们就不会看到对象并在编辑器中对其进行测试。