Unity多线程

转载自风宇冲Unity3D教程学院                                 

                             Unity多线程
 
有些不涉及U3D API的计算可以放在分线程里,能提高多核CPU的使用率。
 
总结:
0. 变量(都能指向相同的内存地址)都是共享的
1. 不是UnityEngine的API能在分线程运行
2. UnityEngine定义的基本结构(int,float,Struct定义的数据类型)可以在分线程计算 
如 Vector3(Struct)可以 , 但Texture2d(class,根父类为Object)不可以。
3
UnityEngine定义的基本类型的函数可以在分线程运行,如
  1. int i = 99;
  2. print (i.ToString());

 

  1. Vector3 x = new Vector3(0,0,9);
  2. x.Normalize();

类的函数不能在分线程运行

 
obj.name 
实际是get_name函数
分线程报错误:get_name  can only be called from the main thread.
 

Texture2D tt = new Texture2D(10,10);

实际会调用UnityEngine里的Internal_Create

分线程报错误:Internal_Create  can only be called from the main thread.

其他transform.position,Texture.Apply()等等都不能在分线程里运行。
 
结论: 分线程可以做 基本类型的计算, 以及非Unity(包括.Net及SDK)的API 
 
例1:在分线程里print信息

 

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5.  
  6. public class Manager : MonoBehaviour {
  7. void Start () {
  8. Thread t = new Thread(new ThreadStart(Cal));
  9. t.Start();
  10. }
  11. void Cal()
  12. {
  13. print ("Hello world!");
  14. }
  15. }
运行后在控制台里会输出’Hello world!‘
 
例2:基本的计算
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5.  
  6. public class Manager : MonoBehaviour {
  7. public int index;
  8. void Start () {
  9. index = 0;
  10. Thread t = new Thread(new ThreadStart(Cal));
  11. t.Start();
  12. }
  13. void Update () {
  14. print("index: "+index);
  15. }
  16. void Cal()
  17. {
  18. index = 10;
  19. }
  20. } 

例3:计算Vector3

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5.  
  6. public class Manager : MonoBehaviour {
  7. public Vector3 vec;
  8. void Start () {
  9. vec = new Vector3(0,0,0);
  10. Thread t = new Thread(new ThreadStart(Cal));
  11. t.Start();
  12. }
  13. void Update () {
  14. print(vec);
  15. }
  16. void Cal()
  17. {
  18. vec = new Vector3(10,20,0);
  19. }
  20. }
例4 在分线程里创建并计算Vector3
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5.  
  6. public class Manager : MonoBehaviour {
  7. public Texture2D t2d;
  8. void Start () {
  9. Thread t = new Thread(new ThreadStart(Cal));
  10. t.Start();
  11. }
  12.  
  13. void Cal()
  14. {
  15. Vector3 x = new Vector3(0,0,9);
  16. print(x);
  17. }
  18. }
例5 在分线程里创建并调用Vector3的函数
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Threading;
  5.  
  6. public class Manager : MonoBehaviour {
  7. public Texture2D t2d;
  8. void Start () {
  9. Thread t = new Thread(new ThreadStart(Cal));
  10. t.Start();
  11. }
  12.  
  13. void Cal()
  14. {
  15. Vector3 x = new Vector3(0,0,9);
  16. x.Normalize();
  17. print(x);
  18. }
  19. }
输出(0.0, 0.0, 1.0)
 
多线程还可以用来Socket传输数据
  • 0
    点赞
  • 0
    收藏 更改收藏夹
  • 0
    评论
<p><span style="font-size: 24px; color: #e53333;">“中级/进阶篇”讲解特点与内容:</span><br />      <span style="font-size: 18px;">   本“中级”与“进阶”篇, 是面向初中级游戏研发人员,以及Unity中高级学习者。为了更加深入的刨析各个语法的本质,我们采用反编译解读IL中间语言的方式,来解构语法重点与难点。 中级篇内容主要讲解: .Net 框架、里氏替换原则(LSP)、类的属性极其本质特性、IS ,AS 关键字、字符串的“驻留性” 原理、深入解析Equals() 原理、枚举类型、自定义集合、深入解析动态集合特性与内部原理、泛型集合、泛型约束、初级委托与事件讲解等。</span><br /><span style="font-size: 18px;"> "进阶篇"是在中级篇的基础之上,进一步研究与讲解关于IO操作、序列化、正则表达式、系统委托(Action、Function、Predicate等)、反射原理与特性、Linq查询表达式、多线程、线程池、任务、Socket套接字编程(Tcp与UDP协议),以及最后使用Unity开发具备实战价值的通讯聊天程序等。</span></p> <p><br /><span style="font-size: 24px; color: #e53333;">C#“进阶篇”教学详细说明如下:</span><br /><br /><span style="font-size: 18px;">1: IO操作与序列化</span><br /><span style="font-size: 18px;">      学习文件、目录、二进制文件、文本文件的读取与写入底层原理。学习文件序列化与反序列化技能。</span><br /><span style="font-size: 18px;">2: 正则表达式</span><br /><span style="font-size: 18px;">      学习正则表达式的强大作用与常用原字符的含义与应用场景。</span><br /><span style="font-size: 18px;">3: 深入委托与事件</span><br /><span style="font-size: 18px;">      学习Action、Func、Predicate 系统内置委托类型,已经适用场合。学习匿名方法、Lambda表达式。深入解析委托与事件的区别。</span><br /><span style="font-size: 18px;">4: 反射与特性</span><br /><span style="font-size: 18px;">      学习反射的概念与动态调用的重要应用价值,以及Type、Assembley核心类等,最后讲解“特性”技术。</span><br /><span style="font-size: 18px;">5: Linq 查询表达式</span><br /><span style="font-size: 18px;">     学习Linq 查询表达式对于“对象集合”(支持IEnumberable 或IEnumberable<T>) 以及SQL数据库、XML文档方面的强大查询功能。</span><span style="font-size: 18px;">    </span><br /><span style="font-size: 18px;">6: 多线程</span><br /><span style="font-size: 18px;">     学习多线程以及线程传参、线程取得返回数值技术,前台与后台线程、线程的同步、线程池、任务等技术。</span><span style="font-size: 18px;">   </span><br /><span style="font-size: 18px;">7: Socket套接字通讯</span><br /><span style="font-size: 18px;">     学习Socket套接字通讯中,Tcp与UPD通讯协议的不同应用场景,以及各自的演示示例,最后用Unity开发一款实用性的聊天通讯工具。</span></p> <p><span style="font-size: 24px; color: #e53333;"><strong>温习提示:</strong></span></p> <p><span style="font-size: 18px;">      </span><span style="font-size: 18px;">    </span><span style="font-size: 18px;"> 本C# for Unity 使用Virtual Studio2012,以及Unity5.2 进行开发与讲解。(学员使用更高版本,对学习没有任何影响)。      </span></p> <div>A:《C# For Unity系列之入门篇》<br />https://edu.csdn.net/course/detail/4560<br />B:《C# For Unity系列之基础篇》<br />https://edu.csdn.net/course/detail/4595<br />C: 《C# For Unity系列之中级篇》<br />https://edu.csdn.net/course/detail/24422<br /><br /></div>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值