您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 2浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity多线程Loom类

程序员正茂 发布时间:2018-12-06 18:05:29 ,浏览量:2

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using System.Linq;

public class Loom : MonoBehaviour
{
    public static int maxThreads = 8;
    static int numThreads;

    private static Loom _current;
    public static Loom Current
    {
        get
        {
            Initialize();
            return _current;
        }
    }
    //####去除Awake
    //  void Awake()  
    //  {  
    //      _current = this;  
    //      initialized = true;  
    //  }  

    static bool initialized;

    //####作为初始化方法自己调用,可在初始化场景调用一次即可
    public static void Initialize()
    {
        if (!initialized)
        {

            if (!Application.isPlaying)
                return;
            initialized = true;
            GameObject g = new GameObject("Loom");
            //####永不销毁
            DontDestroyOnLoad(g);
            _current = g.AddComponent();
        }
    }

    private List _actions = new List();
    public struct DelayedQueueItem
    {
        public float time;
        public Action action;
    }
    private List _delayed = new List();

    List _currentDelayed = new List();

    public static void QueueOnMainThread(Action action)
    {
        QueueOnMainThread(action, 0f);
    }
    public static void QueueOnMainThread(Action action, float time)
    {
        if (time != 0)
        {
            if (Current != null)
            {
                lock (Current._delayed)
                {
                    Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = action });
                }
            }
        }
        else
        {
            if (Current != null)
            {
                lock (Current._actions)
                {
                    Current._actions.Add(action);
                }
            }
        }
    }

    public static Thread RunAsync(Action a)
    {
        Initialize();
        while (numThreads >= maxThreads)
        {
            Thread.Sleep(1);
        }
        Interlocked.Increment(ref numThreads);
        ThreadPool.QueueUserWorkItem(RunAction, a);
        return null;
    }

    private static void RunAction(object action)
    {
        try
        {
            ((Action)action)();
        }
        catch
        {
        }
        finally
        {
            Interlocked.Decrement(ref numThreads);
        }
    }

    void OnDisable()
    {
        if (_current == this)
        {
            _current = null;
        }
    }

    // Use this for initialization  
    void Start()
    {
    }

    List _currentActions = new List();

    // Update is called once per frame  
    void Update()
    {
        lock (_actions)
        {
            _currentActions.Clear();
            _currentActions.AddRange(_actions);
            _actions.Clear();
        }
        foreach (var a in _currentActions)
        {
            a();
        }
        lock (_delayed)
        {
            _currentDelayed.Clear();
            _currentDelayed.AddRange(_delayed.Where(d => d.time  {

            //子线程需要执行的代码写在这里
            //msg.format...

            //主线程执行代码
            Loom.QueueOnMainThread(() =>
            {
                textReceiveMsg.text += msg + "\r\n";
            });

        });

        Debug.Log("receive:" + msg);
    }

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0405s