葡京网上娱乐场Unity主线程和子线程跳转调用(1)

Function
{
//异步在多线程下运行
Loom.RunAsync(() =>
    {
        //这个函数会进行大量文件读写操作
        LoadLocalFile();

        //回到unity线程继续运行
        Loom.QueueOnMainThread(()=>
        {    
            //这个函数是unity函数
            UnityFunction();
        }
    }
}

缘起

住户都说康奈尔笔记仿照,很好用啊,能抗遗忘曲线,让你的笔记事半功倍,有趣味的同学自行百度哈。

网上发出那么些成的沙盘,下载下来之后吧,看正在近乎在方写英文可能重新有益一点,行距很粗,而且还有网址在上面,心里无是异常情愿的游说吗。后来想在友好在word或者excel里面做一个模板下,后来愣是不见面管一个表的一条龙设置成占总表格的70%,最终放弃,后来纪念起来,css里面凡是可以就此cm做单位的呀,为什么非友好写一个啊,只所以div就好了呀

这个Loom类的盖思路就是把全副代码块放多线程中,并将要在主线程运行的代码按委托的点子封装起来保存之list里,Updata函数是unity函数,他会自行每帧执行同一坏。所以他得判list是否发任务并尽任务,从而实现了归来主线程

实现

  1. 先期把一个div设置成A4纸的大小,宽21cm,高29.7cm

        <div id="abody">
        </div>
    

    #abody { width: 21cm; height: 29.7cm; margin: 0 auto; overflow: hidden; padding: 1.5cm 1.2cm 1.2cm 2.5cm;}
    
  2. 被A4张这么好之div里面加少独转移的div,一个往左,占用29%的半空中,一个往右,占用68%底空中

    <div id="main" class="main le">
            <div class="aline">提示</div>
            <div class="aline"></div>
    </div>
    <div id="sider" class="main ri">
            <div class="aline">笔记</div>
            <div class="aline"></div>
    
    </div>
    <div id="footer" class="footer">
    
            <div class="aline doubleline">概要</div>
            <div class="aline"></div>
    </div>
    

    因此css的border分开区区栏

    .main {height: 75%; overflow: hidden;}
    
        .le { width: 28.99999%; border-right: double 3px #666; float: left; }
        .ri { width: 69.99999%; float: right; }
    
  3. 通向大框里面写一尽一行的横线,用一个div 的aline类实现,html见者

    这里要你的编辑器支持emmet的话语,写一个div.aline*42,就会见生42执相同之div出现了。然后用css的border属性画出一条条之线下。

    .aline { height: 0.9cm; border-bottom: 1px; border-bottom-style: dashed; border-bottom-color: #999; 
                margin-right: 8px; color: #eee; line-height: 0.9cm;}
    
  4. 重复于左右零星独大框的后面放一个div,清除浮动,放概要有些。

    <div id="main" class="main le">
            <div class="aline">提示</div>
            <div class="aline"></div>
    </div>
    <div id="sider" class="main ri">
            <div class="aline">笔记</div>
            <div class="aline"></div>
    
    </div>
    <div id="footer" class="footer">
    
            <div class="aline doubleline">概要</div>
            <div class="aline"></div>
    </div>
    

    ,里面的首行,用实线分出5R笔记的下局部结构来

    .footer {clear: both; overflow: hidden;}
    .doubleline { border-top: double 3px #666;}
    
  5. 纵使如此,很粗略的哈

    再见2017,你好,2018。
    顾念将在一直打印在用底言语,在此间https://files.cnblogs.com/files/nimeiz/0011.pdf

  网上的做法一般都是以史为鉴Loom,摘录一个剧本

Function
{
    //这个函数会进行大量文件读写操作
    LoadLocalFile();

    //这个函数是unity函数
    UnityFunction();
}
  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System;
  5 using System.Threading;
  6 using System.Linq;
  7 
  8     public class Loom :MonoBehaviour
  9     {
 10         public static int maxThreads = 8;
 11         static int numThreads;
 12 
 13         private static Loom _current;
 14         //private int _count;
 15         public static Loom Current
 16         {
 17             get
 18             {
 19                 Initialize();
 20                 return _current;
 21             }
 22         }
 23 
 24         void Awake()
 25         {
 26             _current = this;
 27             initialized = true;
 28         }
 29 
 30         static bool initialized;
 31 
 32         public static void Initialize()
 33         {
 34             if (!initialized)
 35             {
 36 
 37                 if (!Application.isPlaying)
 38                     return;
 39                 initialized = true;
 40                 var g = new GameObject("Loom");
 41                 _current = g.AddComponent<Loom>();
 42 #if !ARTIST_BUILD
 43                 UnityEngine.Object.DontDestroyOnLoad(g);
 44 #endif
 45             }
 46 
 47         }
 48         public struct NoDelayedQueueItem
 49         {
 50             public Action<object> action;
 51             public object param;
 52         }
 53 
 54         private List<NoDelayedQueueItem> _actions = new List<NoDelayedQueueItem>();
 55         public struct DelayedQueueItem
 56         {
 57             public float time;
 58             public Action<object> action;
 59             public object param;
 60         }
 61         private List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>();
 62 
 63         List<DelayedQueueItem> _currentDelayed = new List<DelayedQueueItem>();
 64 
 65         public static void QueueOnMainThread(Action<object> taction, object tparam)
 66         {
 67             QueueOnMainThread(taction, tparam, 0f);
 68         }
 69         public static void QueueOnMainThread(Action<object> taction, object tparam, float time)
 70         {
 71             if (time != 0)
 72             {
 73                 lock (Current._delayed)
 74                 {
 75                     Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = taction, param = tparam });
 76                 }
 77             }
 78             else
 79             {
 80                 lock (Current._actions)
 81                 {
 82                     Current._actions.Add(new NoDelayedQueueItem { action = taction, param = tparam });
 83                 }
 84             }
 85         }
 86 
 87         public static Thread RunAsync(Action a)
 88         {
 89             Initialize();
 90             while (numThreads >= maxThreads)
 91             {
 92                 Thread.Sleep(100);
 93             }
 94             Interlocked.Increment(ref numThreads);
 95             ThreadPool.QueueUserWorkItem(RunAction, a);
 96             return null;
 97         }
 98 
 99         private static void RunAction(object action)
100         {
101             try
102             {
103                 ((Action)action)();
104             }
105             catch
106             {
107             }
108             finally
109             {
110                 Interlocked.Decrement(ref numThreads);
111             }
112 
113         }
114 
115 
116         void OnDisable()
117         {
118             if (_current == this)
119             {
120 
121                 _current = null;
122             }
123         }
124 
125 
126 
127         // Use this for initialization
128         void Start()
129         {
130 
131         }
132 
133         List<NoDelayedQueueItem> _currentActions = new List<NoDelayedQueueItem>();
134 
135         // Update is called once per frame
136         void Update()
137         {
138             if (_actions.Count > 0)
139             {
140                 lock (_actions)
141                 {
142                     _currentActions.Clear();
143                     _currentActions.AddRange(_actions);
144                     _actions.Clear();
145                 }
146                 for (int i = 0; i < _currentActions.Count; i++)
147                 {
148                     _currentActions[i].action(_currentActions[i].param);
149                 }
150             }
151 
152             if (_delayed.Count > 0)
153             {
154                 lock (_delayed)
155                 {
156                     _currentDelayed.Clear();
157                     _currentDelayed.AddRange(_delayed.Where(d => d.time <= Time.time));
158                     for (int i = 0; i < _currentDelayed.Count; i++)
159                     {
160                         _delayed.Remove(_currentDelayed[i]);
161                     }
162                 }
163 
164                 for (int i = 0; i < _currentDelayed.Count; i++)
165                 {
166                     _currentDelayed[i].action(_currentDelayed[i].param);
167                 }
168             }
169         }
170     }

  必须管LoadLocalFile()
不“假死”。怎么开为?只要将Function放到多线程中,UnityFunction()回到主线程即可

动用办法

  Unity除了有些主导的数据类型,几乎所有的API都不能够在非unity线程中调用,如果项目中起一样段落老耗时操作,unity可能会见并发“假死”。如果这段操作是暨unity无关的,我们可拿这耗时的操作放到子线程中失去运转,防止unity假死提高性,如下面这伪代码