unity跨线程操作的问题

来源:互联网 发布:mysql 5.6.25安装 编辑:程序博客网 时间:2024/06/08 03:34
/* Copyright (c) 2015 3shine.com
 * Anthor:penguin_ku(十月)
 * DateTime:2015/5/3 22:35:41
 * FileName:MainTaskProcessor
 * MachineName: win8.1-04020905
 * Version:V1.0.0.0
 * 
 * Function:
 * 1、
 * 2、
 * 3、
 * 4、
 *
 * Tip:
 * 1、
 * 2、
 * 3、
 * 4、
 *
 * Modify:
 * DateTime:
 * Remark:
*/
 
using Assets.Core.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
 
/// <summary>
/// 主线程任务处理器
/// </summary>
public class MainTaskProcessor : MonoBehaviour
{
    #region 私有变量
 
    private static System.Object m_oneLoopQueueLock = new object();
    private static List<TaskUnit> m_oneLoopQueues = new List<TaskUnit>();
 
    #endregion
 
    #region 公开方法
 
    /// <summary>
    /// 追加一次性任务
    /// </summary>
    /// <param name="p_action"></param>
    public static void AppendOneAction(TaskUnit p_action)
    {
        lock (m_oneLoopQueueLock)
        {
            m_oneLoopQueues.Add(p_action);
        }
    }
 
    #endregion
 
    #region 生命周期
 
    private void Start()
    {
        gameObject.name = "MainTaskProcessor";
    }
 
    private void Update()
    {
        for (int i = 0; i < m_oneLoopQueues.Count; i++)
        {
            var item = m_oneLoopQueues[i];
            item.CurrWait += Time.deltaTime;
            if (item.CurrWait >= item.Interval)
            {
                item.Action();
                item.CurrWait = 0;
                item.CurrLoopTimes++;
 
                if (item.LoopTimes != -1 && item.CurrLoopTimes >= item.LoopTimes)
                {
                    m_oneLoopQueues.RemoveAt(i);
                    i--;
                }
            }
        }
    }
 
    #endregion

}


/* Copyright (c) 2015 3shine.com
 * Anthor:penguin_ku(十月)
 * DateTime:2015/8/11 10:04:24
 * FileName:ActionUnit
 * Version:V1.0.0.0
 * 
 * Function:
 * 1、
 * 2、
 * 3、
 * 4、
 *
 * Tip:
 * 1、
 * 2、
 * 3、
 * 4、
 *
 * Modify:
 * DateTime:
 * Remark:
*/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
/// <summary>
/// 执行单元
/// </summary>
public class TaskUnit
{
    #region 公开属性
 
    /// <summary>
    /// 设置或获取需要执行的操作
    /// </summary>
    public Action Action { set; get; }
 
    /// <summary>
    /// 设置或获取等待间隔
    /// </summary>
    public float Interval { set; get; }
 
    /// <summary>
    /// 设置或获取需要循环执行次数
    /// </summary>
    public int LoopTimes { set; get; }
 
    /// <summary>
    /// 设置或获取当前等待时间
    /// </summary>
    public float CurrWait { set; get; }
 
    /// <summary>
    /// 设置或获取当前循环次数
    /// </summary>
    public int CurrLoopTimes { set; get; }
 
    #endregion
 
    #region 构造函数
 
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="p_action"></param>
    /// <param name="p_interval"></param>
    /// <param name="p_loopTimes"></param>
    public TaskUnit(Action p_action, float p_interval = 0, int p_loopTimes = 1)
    {
        Action = p_action;
        Interval = p_interval;
        LoopTimes = p_loopTimes;
        CurrWait = 0;
        CurrLoopTimes = 0;
    }
 
    #endregion
}



 使用:

 MainTaskProcessor.AppendOneAction(newTaskUnit(() =>
        {
/*你的代码*/
        }));


from: http://www.unitymanual.com/thread-41935-1-1.html


0 0