StartCoroutine方法不起作用

时间:2015-11-29 13:34:14

标签: c# unity3d

在此代码中使用了6个气泡。它们被缩放到特定级别,然后它们就消失了。现在的问题是所有气泡都在同一时间到来。但是,要求它们在延迟一段时间之后才会出现。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Display : MonoBehaviour {

public GameObject bubble1;
public GameObject bubble2;
public GameObject bubble3;
public GameObject bubble4;
public GameObject bubble5;
public GameObject bubble6;
private int c=0;
private int z = 0;
private int[] a=new int[6];
private GameObject[] enlarge=new GameObject[6];
void Start ()
{
    int ch,t,k=0;
    string str="";
    float tim;
    GameObject[] bubble = {bubble1,bubble2,bubble3,bubble4,bubble5,bubble6};
    for (int i=1; i<6; i++)
    a = 0;
    str = "0";
    if(c==0)
    foreach(GameObject b in bubble)
    {
        b.transform.localScale = new Vector3 (1F, 1F, 0F);
        str+=b.transform.tag;
    }
    c++;
    for(int i=0;i<6;i++)
    {

        ch=0;
        while(true)
        {
            ch = Random.Range (0, 6);
            if (a [ch] == 0) 
            {
                a [ch] = 1;
                str+="2";
                break;
            } 


        }
        str+="3";
        t=-1;
        foreach(GameObject b in bubble)
        {
            t++;
            if(t==ch)
            {

                b.transform.localScale = new Vector3 (1F, 1F, 0F);

                enlarge[k++]=b;

                transform.localScale = new Vector3 (1F, 1F, 0F);
                StartCoroutine("waitthreesec");

            }
            str+="4";
        }

    }

}
IEnumerator waitthreesec()
{
    //print ("Thread");
    yield return new WaitForSeconds(10);


    Debug.Log ("Just waited 10 second");
}


// Update is called once per frame
void Update ()
{
    float tim;
    string str = "";
    Time.timeScale = 0.5F;
    print (Time.fixedTime+"");

    if(a[0]==1)
    {
        bubble1.transform.localScale =new Vector3(1F,1F,0F )* Time.time;

    }
    if(a[1]==1)
    {

        bubble2.transform.localScale =new Vector3(1F,1F,0F )* Time.time;    
    }
    if(a[2]==1)
    {
        //Time.timeScale = 0.5F;
        bubble3.transform.localScale =new Vector3(1F,1F,0F )* Time.time;
    }
    if(a[3]==1)
    {

        bubble4.transform.localScale =new Vector3(1F,1F,0F )* Time.time;
    }
    if(a[4]==1)
    {

        bubble5.transform.localScale =new Vector3(1F,1F,0F )* Time.time;
    }
    if(a[5]==1)
    {

        bubble6.transform.localScale =new Vector3(1F,1F,0F )* Time.time;
    }

}

}

1 个答案:

答案 0 :(得分:2)

好的,所以让我先详细说明你在粘贴代码之前做错了什么。 首先,你不能很好地使用if / for C#语法。

如果你以这种方式使用for / foreach,它只会执行它下面的第一行代码:

foreach (var object in objects)
    DoStuff(object); //foreach will only execute this line per iterated object
    DoSomethingElse(); //foreach loop won't reach here, it will just execute after foreach loop is done

多线foreach / for的正确方法是:

foreach (var object in objects)
{
    DoStuff(object);
    DoSomethingElse();
}

if block:

也是如此
if (c==0)
    DoStuff(); //will be executed only if c equals 0
DoSomethingElse(); // will be executed regardless c equals 0 or not

在发布之前,请检查您的代码是否编译,因为在我开始清理之前没有。

重构代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Display : MonoBehaviour
{

    public GameObject bubble1;
    public GameObject bubble2;
    public GameObject bubble3;
    public GameObject bubble4;
    public GameObject bubble5;
    public GameObject bubble6;
    private int c = 0;
    private int z = 0;
    private int[] a = new int[6];
    private GameObject[] enlarge = new GameObject[6];

    void Start()
    {
        int ch, t, k = 0;
        string str = "";
        float tim;
        GameObject[] bubble = {bubble1, bubble2, bubble3, bubble4, bubble5, bubble6};

        for (int i = 1; i < 6; i++)
        {

            str = "0";
            if (c == 0)
            {
                foreach (GameObject b in bubble)
                {
                    b.transform.localScale = new Vector3(1F, 1F, 0F);
                    str += b.transform.tag;
                }
                c++;
                for (int j = 0; j < 6; j++)
                {

                    ch = 0;
                    while (true)
                    {
                        ch = Random.Range(0, 6);
                        if (a[ch] == 0)
                        {
                            a[ch] = 1;
                            str += "2";
                            break;
                        }


                    }
                    str += "3";
                    t = -1;
                    foreach (GameObject b in bubble)
                    {
                        t++;
                        if (t == ch)
                        {

                            b.transform.localScale = new Vector3(1F, 1F, 0F);

                            enlarge[k++] = b;

                            transform.localScale = new Vector3(1F, 1F, 0F);
                            StartCoroutine(waitthreesec()); //Instead of "waitthreesec", use waitthreesec() to avoid runtime overhead

                        }
                        str += "4";
                    }
                }
            }
        }

    }

    IEnumerator waitthreesec()
    {
        //print ("Thread");
        Debug.Log("Coroutine hit");
        yield return new WaitForSeconds(2);

        Debug.Log("Just waited 2 seconds");
    }


    // Update is called once per frame
    void Update()
    {
        float tim;
        string str = "";
        Time.timeScale = 0.5F;
        print(Time.fixedTime + "");

        if (a[0] == 1)
        {
            bubble1.transform.localScale = new Vector3(1F, 1F, 0F)*Time.time;

        }
        if (a[1] == 1)
        {

            bubble2.transform.localScale = new Vector3(1F, 1F, 0F)*Time.time;
        }
        if (a[2] == 1)
        {
            //Time.timeScale = 0.5F;
            bubble3.transform.localScale = new Vector3(1F, 1F, 0F)*Time.time;
        }
        if (a[3] == 1)
        {

            bubble4.transform.localScale = new Vector3(1F, 1F, 0F)*Time.time;
        }
        if (a[4] == 1)
        {

            bubble5.transform.localScale = new Vector3(1F, 1F, 0F)*Time.time;
        }
        if (a[5] == 1)
        {

            bubble6.transform.localScale = new Vector3(1F, 1F, 0F)*Time.time;
        }

    }
}

如果你想要实现你所尝试的目标,我会采用更短的方式。

为了让每个气泡一个接一个地弹出:

using UnityEngine;
using System.Collections;

public class Display : MonoBehaviour
{
    public GameObject[] Bubbles;

    void Start()
    {
        InitBubbles();
        StartCoroutine(PopBubbles()); //Instead of "PopBubbles", use PopBubbles() to avoid runtime overhead
    }

    public IEnumerator PopBubbles()
    {
        foreach (GameObject b in Bubbles)
        {
            b.transform.localScale = new Vector3(0f, 0f, 0f);
            while (Vector3.SqrMagnitude(Vector3.one - b.transform.localScale) > 0.05f)
            {
                b.transform.localScale += new Vector3(0.05f, 0.05f, 0.05f);
                yield return null;
            }
            yield return new WaitForSeconds(0.5f);
            b.transform.localScale = new Vector3(0f, 0f, 0f);
            yield return new WaitForSeconds(0.5f);
        }
        yield return null;
    }

    public void InitBubbles()
    {
        foreach (var bubble in Bubbles)
        {
            bubble.transform.localScale = Vector3.zero;
        }
    }
}

注意

关于如何使用Coroutines的一点说明:

  • Coroutine将返回每个收益的控制权
  • yield return new WaitForSeconds(x)只会使Coroutine返回控制并等待x秒,直到调用者可以再次从中获益

我已将单独的字段初始化程序更改为简单数组,因此您可以通过这种方式初始化它:

enter image description here