即使游戏暂停并且时间刻度设置为0,协程仍在运行

时间:2018-10-08 22:45:31

标签: unity3d

正如标题所指出的,由于某些原因,当我的游戏暂停时,我的例程仍然运行。我什至甚至将时间刻度条件置于一会儿条件下,以使那一会儿如果暂停但不起作用时不会运行。我已经完整添加了我的代码,并希望有人能够提供帮助。

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);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我是新手,但仍然不习惯Chronos。 也许我错了,但我的猜测是这一行。

Destroy(prefab, 6);

据我了解,销毁的延迟不会受到计时的影响。 您最好使用新的协程销毁它。

像这样

StartCoroutine(DestroyRoutine(prefab))

IEnumurator DestroyRoutine(GameObject gameobject)
{
   yield return time.WaitForSeconds(6);
   Destroy(gameObject)    
}