Unity延迟函数

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

public class SupportTask
{
    public Action m_cb;
    public float m_duration;
    public SupportTask(Action cb,float f)
    {
        m_cb = cb;
        m_duration = f;
    }
}

public class SupportBehavior : MonoBehaviour {
    
    public static SupportBehavior inst;

    public List m_onUpdate = new List();
    public List m_onFixedUpdate = new List();
    public List m_onLateUpdate = new List();

    public List m_cbs = new List();

    void Awake()
    {
        inst = this;
    }

	void Update () 
	{
        for (int i = 0; i < m_onUpdate.Count; i++)
        {
            try
            {
                m_onUpdate[i]();
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
	}

	void FixedUpdate()
	{
        for (int i = 0; i < m_onFixedUpdate.Count; i++)
        {
            try
            {
                m_onFixedUpdate[i]();
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
        if (m_cbs.Count > 0)
        {
            for(int i = m_cbs.Count - 1; i > -1; i--)
            {
                m_cbs[i].m_duration = m_cbs[i].m_duration - Time.deltaTime;
                if (m_cbs[i].m_duration <= 0)
                {
                    try
                    {
                        m_cbs[i].m_cb();
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e.ToString());
                    }
                    m_cbs.Remove(m_cbs[i]);
                }
            }
        }
	}

    void LateUpdate()
    {
        for (int i = 0; i < m_onLateUpdate.Count; i++)
        {
            try
            {
                m_onLateUpdate[i]();
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
    }

    public void AddCbAfter(float s,Action cb)
    {
        m_cbs.Add(new SupportTask(cb, s));
    }
}

 

你可能感兴趣的:(Unity延迟函数)