Unity中的协程

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

        什么是协程?

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

        一.协程的基本用法

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

 

 

 

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值