Coroutine协程

Unity中的Coroutine协程机制,能够帮助我们实现延时等功能。

Coroutine例子

下面代码实现了延时3秒,将游戏对象设为active的功能。

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

public class CoroutineTest : MonoBehaviour
{
    public GameObject o;
    void Start()
    {
        // 延时3秒,将游戏对象设为active
        StartCoroutine(delayActive());
    }

    IEnumerator delayActive()
    {
        yield return new WaitForSeconds(3);
        o.SetActive(true);
    }
}

代码中,我们定义了一个生成器函数,并将其传入协程中。

作者:Gacfox
版权声明:本网站为非盈利性质,文章如非特殊说明均为原创,版权遵循知识共享协议CC BY-NC-ND 4.0进行授权,转载必须署名,禁止用于商业目的或演绎修改后转载。
Copyright © 2017-2024 Gacfox All Rights Reserved.
Build with NextJS | Sitemap