Unity_与线程的关联

来源:互联网 发布:pdf阅读器有没有mac 编辑:程序博客网 时间:2024/06/05 03:48
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace TcpTest{    class Program    {        public static int counter = 0;        static void Main(string[] args)        {            //现成的创建;            Thread myThread = new Thread(Foo);            //线程的启用(执行方法);            myThread.Start();            Thread myThread1 = new Thread(Foo1);            //参数可以为空;            myThread1.Start();            //MyThread myThread = new MyThread(Foo);            //myThread.Start();            //MyThread myThread1 = new MyThread(Foo1);            //参数不可以为空;            //myThread1.Start(1);            //在黑窗口中While(true)打句话每秒执行700次左右            Thread MyThreadLoop =new  Thread(LoopFoo);            MyThreadLoop.Start();            ////线程的终止;            ////这种情况可以通过Bug产生,也可以手动操作;            //Thread.CurrentThread.Abort();        }        //public static void LoopFoo()        //{        //    while (true)        //    {        //        counter++;        //        Console.WriteLine(counter);        //    }        //}        //public static void LoopFoo()        //{        //    while (true)        //    {        //        counter++;        //        //线程的休眠;        //        //单位 毫秒;        //        //17等于70帧这就和Unity执行频率一样了;        //        //线程休眠有时不是必要的,只是为了限制节奏;        //        Thread.Sleep(17);        //        Console.WriteLine(counter);        //    }        //}        //public static void LoopFoo()        //{        //    while (true)        //    {        //        counter++;        //        //(CurrentThread)访问当前方法被那个线程所使用;        //        //和休眠 协程(等待一段时间去做某件事)差不多;        //        Thread.CurrentThread.Join(50);        //        Console.WriteLine(counter);        //    }        //}        //线程的终止;        public static void LoopFoo()        {            while (true)            {                counter++;                Console.WriteLine(counter);                if (counter>=20)                {                    Thread.CurrentThread.Abort();                }            }        }        public static void Foo()        {            Console.WriteLine("myThread");        }        public static void Foo1(object obj)        {            Console.WriteLine("myThread1");        }    }    //委托与类同级;    public delegate void MyThreadStart();    public delegate void MyParamersThreadStart(object obj);    public class MyThread    {        private MyThreadStart start;        private MyParamersThreadStart startP;        public MyThread(MyThreadStart start)        {            this.start = start;        }        public MyThread(MyParamersThreadStart start)        {            this.startP = start;        }        public void Start()        {            if (start!=null)            {                start();            }                   }        public void Start(object obj)        {            if (startP != null)            {                startP(obj);            }        }    }}

休眠最大的作用是节约性能

Unity在Update中打个Debug每秒执行60~70次

在黑窗口中While(true)打句话每秒执行700次左右

24帧就是视觉连续了0.0

Unity整个程序本身就是一个线程,需要注意不要轻易在Unity中主线程使用(Sleep(假死),Abort(强退))using UnityEngine;using System.Collections;using System.Threading;public class TestThread : MonoBehaviour {    // Use this for initialization    void Start () {        //在Unity中        Thread.Sleep(30000);        //这就直接关掉Unity了;        Thread.CurrentThread.Abort();    }    // Update is called once per frame    void Update () {        Debug.Log("Update");    }}在Unity中使用多线程就不能调用Unity相关组件了0.0
原创粉丝点击