首页 > 解决方案 > 即使游戏暂停且时间刻度设置为 0,协程仍会运行

问题描述

正如标题所指出的,由于某种原因,当我的游戏暂停时,我的协程仍然运行。我什至将时间尺度条件置于一段时间条件下,以便在暂停时不会运行,但无济于事。我已经完整地添加了我的代码,希望有人能够提供帮助。

using UnityEngine;
using System.Collections;
using Chronos;

public class ObjectSpawn : BaseBehaviour //MonoBehaviour
{

    public float minTime = 3f;
    public float maxTime = 9f;
    public float minX = -65.5f;
    public float maxX = -5.5f;
    public float topY = -5.5f;
    public float z = 0.0f;
    public int count = 50;
    public GameObject prefab;
    public bool doSpawn = true;

    public float fallGrav =1.0f;
    int first = 1;

    void Start()
    {
        Clock clock = Timekeeper.instance.Clock("MovingOneWayPlatforms");

        StartCoroutine(Spawner());

    }



    IEnumerator Spawner()
    {
        while (first == 1) {
                yield return time.WaitForSeconds(8.0f);
                first = 0;
            } 
        while (doSpawn   && count > 0 /*&& time.timeScale != 0 */)
        {

            Renderer renderer = GetComponent<Renderer>();
            float min = renderer.bounds.min.x;
            float max = renderer.bounds.max.x;
            Vector3 v12 = new Vector3(Random.Range(minX, maxX), this.gameObject.transform.position.y, 0f);

            prefab.GetComponent<Rigidbody2D>().gravityScale = fallGrav;
            prefab =  Instantiate(prefab, v12, Random.rotation);

            count--; 
           //  yield return new WaitForSeconds(Random.Range(minTime, maxTime)); 
              yield return time.WaitForSeconds(Random.Range(minTime, maxTime));

             Destroy(prefab, 6);

        }
    }
}

标签: unity3d

解决方案


尝试取消注释您的第二个 while 语句,我认为这是您的问题。


推荐阅读