Some calculations that do not involve u3d APIs can be placed in sub-threads, which can improve the utilization of multicore CPUs.
Summary: 0. variables (all pointing to the same memory address) are shared 1. An API that is not Unityengine can run 2 on a split thread. Unityengine defines the basic structure (int,float,struct defined data type) that can be computed in sub-threading, such as Vector3 (Struct), but texture2d (class, the root parent class is Object) is not available. 3 Unityengine defines a basic type of function that can be run on a sub-thread, such as
int i = 99; Print (i.ToString ());
Vector3 x = new Vector3 (0,0,9); X.normalize ();
The function of a class cannot be run on a sub-thread
Obj.name is actually get_name function sub-thread report error: Get_name can only is called from the main thread.
Texture2d tt = new texture2d (10,10);
Will actually call the internal_create in Unityengine.
Sub-Threading Report error: Internal_create can only is called from the main thread. Other transform.position,texture.apply () and so on are not allowed to run in sub-threads.
Conclusion: Sub-threading can do basic types of calculations, as well as non-unity (including. NET and SDK) API
Example 1: Thread print information in a split line
Using Unityengine; Using System.Collections; Using System; Using System.Threading;
public class Manager:monobehaviour {void Start () {Thread t = new Thread (new ThreadStart (Cal)); T.start ();} void Cal ( {print ("Hello world!");}} After running, the ' Hello world! ' will be output in the console.
Example 2: The basic calculation
Using Unityengine; Using System.Collections; Using System; Using System.Threading;
public class Manager:monobehaviour {public int index; void Start () {index = 0; Thread t = new Thread (new ThreadStart (Cal)); T.start (); } void Update () {print ("index:" +index);} void Cal () {index = 10;}}
Example 3: Calculating Vector3
Using Unityengine; Using System.Collections; Using System; Using System.Threading;
public class Manager:monobehaviour {public Vector3 vec; void Start () {VEC = new Vector3 (0,0,0); Thread t = new Thread (new ThreadStart (Cal)); T.start (); } void Update () {print (VEC);} void Cal () {VEC = new Vector3 (10,20,0);}} Example 4 creation and calculation of Vector3 in thread
Using Unityengine; Using System.Collections; Using System; Using System.Threading;
public class Manager:monobehaviour {public texture2d t2d; void Start () {Thread t = new Thread (new ThreadStart (Cal)); T.start (); }
void Cal () {Vector3 x = new Vector3 (0,0,9); print (x);}} Example 5 a function that creates and calls Vector3 thread in a split line
using unityengine; using system.collections; using system; using system.threading;
Public Class manager: monobehaviour {public texture2d t2d; Void start () {Thread T =&nbs P;new thread (New threadstart (Cal)); T.start ();
Void cal () {vector3 x = new Vector3 (0,0,9); x.normalize (); print (x);}} output (0.0, 0.0, 1.0)
Multithreading You can also use the socket to transmit data to the socket post