Unity多线程

本文介绍了Unity3D中如何使用多线程来提高CPU利用率,强调了哪些操作可以在分线程中执行,哪些不能,例如基本类型计算可以在分线程中进行,而涉及Unity API的操作必须在主线程执行。通过示例代码展示了打印信息、基本计算以及Vector3操作等在多线程中的应用,并提醒注意多线程与Unity图形更新的同步问题。
摘要由CSDN通过智能技术生成

转载自风宇冲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传输数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值