Unity3d 协程、调用函数、委托

(一)协程

开启方法:StartCoroutine("函数名");

结束方法StopCoroutine("函数名"),StopAllCoroutines();

IEnumerator TestIEnumerator()
    {
        Debug.Log("协程");
        //等待游戏界面绘制完成
        yield return new WaitForEndOfFrame();
        Debug.Log("游戏界面绘制完成");
        //等待1秒后
        yield return new WaitForSeconds(1F);
        Debug.Log("1秒后");
        //等待0.5秒后
        yield return new WaitForSeconds(0.5F);
        Debug.Log("0.5秒后");
        while(true)
        {
            //等待固定更新
            yield return new WaitForFixedUpdate();
            Debug.Log("固定更新");
        }
    }
	// Use this for initialization
	void Start () {
        StartCoroutine("TestIEnumerator");
       }




(二)调用函数

开启方法 不重复调用 Invoke("函数名",“延迟时间”); 重复调用 InvokeRepeating("函数名",“延迟时间”,“重复间隔时间”);

结束方法 CancelInvoke("函数名"),CancelInvoke();

是否在有在调用的函数 IsInvoking(); 指定函数是否在调用 IsInvoking("函数名");

void TestInvokeRepeating()
    {
        Debug.Log("重复调用");
        m_round++;
        if (m_round > 15)
        {
            //结束所有调用
            //CancelInvoke();
            //结束指定调用
            CancelInvoke("TestInvokeRepeating");
        }

        if (IsInvoking("TestInvokeRepeating"))
        {
            Debug.Log("调用中");
        }
        else
        {
            Debug.Log("不在调用中");
        }
    }
    void TestInvokeRepeating2()
    {
        Debug.Log("重复调用TestInvokeRepeating2");
       
    }
	// Use this for initialization
	void Start () {
        m_round = 0;
        InvokeRepeating("TestInvokeRepeating",0f,1f);
        InvokeRepeating("TestInvokeRepeating2", 0f, 1f);
}


(二)委托

public class GameManager : MonoBehaviour
{
    //定义一个委托
    delegate int TestEntrust(int a);

    public int ReceiveLogic(int a)
    {
        Debug.Log("参数a="+a);
        return 0;
    }
	// Use this for initialization
	void Start () {
        //创建委托对象
        TestEntrust rl = new TestEntrust(ReceiveLogic);
        //调用
        rl(3);
       }
}





原文地址:https://www.cnblogs.com/lexiaoyao-jun/p/5208253.html