首页 > 其他分享 >Unity中的协程

Unity中的协程

时间:2024-09-01 13:53:31浏览次数:5  
标签:协程 CountCo yield Start Unity using IEnumerator

        协程:让程序并行执行的功能。

        什么是协程?

 协程不是多线程,而是类似函数调用。

        一.协程的基本用法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    void Start()
    {
        e = CountCo();
        StartCoroutine(e);
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            StopCoroutine(e);
        }
    }

}

开启协程时用 e ,停止协程时也要用 e。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    void Start()
    {
        e = CountCo();
        StartCoroutine("CountCo");
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            StopCoroutine("CountCo");
        }
    }

}

开启协程时用"CountCo" ,停止协程时也要用 "CountCo"。 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    void Start()
    {
        e = CountCo();
        co = StartCoroutine("CountCo");
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            StopCoroutine(co);
        }
    }

}

 把 co 的值传递给停止协程的方法。

开始协程和停止协程的方法要对应,不能分别用两种方法。

yield:暂停执行协程,转移控制权给Unity。

二.注意事项:

1.禁用脚本不会停止协程的执行。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    void Start()
    {
        e = CountCo();
        co = StartCoroutine("CountCo");
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            StopCoroutine(co);
        }
        if (Input.GetKeyUp(KeyCode.F ))
        {
            this.enabled = false;//通过代码禁用脚本的方法
        }
    }

}

 

2.可以通过移除脚本的方法来停止协程。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    void Start()
    {
        e = CountCo();
        co = StartCoroutine("CountCo");
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            Destroy(this);//销毁当前对象,即脚本
        }
       
    }

}

3.通过禁用游戏对象的方法来停止协程。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    void Start()
    {
        e = CountCo();
        co = StartCoroutine("CountCo");
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            this.gameObject.SetActive(false);//禁用组件所在的游戏对象
        }
       
    }

}

4.通过销毁游戏对象来停止协程

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    void Start()
    {
        e = CountCo();
        co = StartCoroutine("CountCo");
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print(i);
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    private void Update()
    {
        if(Input.GetKeyUp (KeyCode.Space ))
        {
            Destroy(this.gameObject);//销毁游戏对象
        }
       
    }

}

 三:协程的特殊用法

1.Start()方法可以采用协程形式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    IEnumerator Start()
    {
        e = CountCo();
        co = StartCoroutine("CountCo");
        for (int i = 0; i < 9; i++)
        {
            print($"Start i ={i}");
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print($"CountCo i ={i}");
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    

}

运行结果:

2.可以等待任何一个返回值类型为IEnumerator的函数 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StudyCo : MonoBehaviour
{
    // Start is called before the first frame update
    IEnumerator e;
    Coroutine co;
    IEnumerator Start()
    {
        e = CountCo();
        yield return e;//等待协程 CountCo()执行完再执行协程Start()
        for (int i = 0; i < 9; i++)
        {
            print($"Start i ={i}");
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }
    }

    IEnumerator CountCo()
    {
        for (int i = 0; i < 9; i++)
        {
            print($"CountCo i ={i}");
            yield return new WaitForSeconds(1);//等待的时间
            //协程的原理:yield会构造一个返回值类型为IEnumerator类型的对象
        }

    }
    

}

等待协程 CountCo()执行完再执行协程Start() 

 

 

 

标签:协程,CountCo,yield,Start,Unity,using,IEnumerator
From: https://blog.csdn.net/m0_75031942/article/details/141680606

相关文章