Unity3D game development: multithreading and multithreading, unity3d Game Development

Source: Internet
Author: User

Unity3D game development: multithreading and multithreading, unity3d Game Development

Multi-thread and multi-thread for Unity3D Game Development

 

Multiple Threads in Unity3D. A thread is a very complex topic, but if you have mastered it, you can easily use multiple hardware processors or process data blocks that are difficult to divide and manage.

 

For example, in scenarios, A * algorithm is used for A large amount of data computing, A large number of vertices are operated in the deformation mesh, and image processing such as uploading data to the server and QR code identification must be continuously performed, if you want to deal with many things at the same time or interact with Unity objects, you can use thread. Otherwise, coroutine is used.

 

A thread runs in a program together with other threads. Real synchronization of multiple threads can be performed on a multi-processor computer. More threads depend on multiple processing cores.

 

During Unity3D programming, there is always a main thread to execute your code. You can also create additional threads and the main thread to run at the same time. In Unity, you can only access Unity3D components, objects, and Unity3D system calls from the main thread. Any attempt to access the second thread of these projects will fail and cause errors, which is a restriction to be taken seriously.

 

So when you write code, you think that a function starts and reaches its execution point and returns, and what you do is executed in another function, however, no corresponding changes have occurred. The operating system determines the execution of your code. At any time, your code can only be "Sleep" temporarily, and then let another code start to run.

 

 

 

Multithreading and multithreading in Unity3D

 

In this example, when the first thread loads the value of A into the CPU register to prepare for + 1, it is interrupted. The second thread reads the value of A and deducts 1000, at this time, A should be-950. Now the first thread starts again, and its result of 50 + 1 in the register is stored in A, A is changed to 51, and-950 is lost.

 

Fundamentally speaking, when multiple threads are used to simultaneously access variables or memory, a lot of preventive measures should be taken to ensure that this will not happen. So Unity decided to access these variables or memory from another thread is invalid, just to avoid problems with all systems and framework objects. So make sure that there is only one thread at a time to modify the variable. This does not mean that you cannot work with multiple threads. You can use "sort" to solve this problem.

 

C # has the lock keyword to ensure that only one thread can access a specific object within a specific period of time. Here, the object is called because the method locks a value type) or a prototype (primitive ).

 

1. int a = 50;

2.

3. object guard = new object ();

4.

5. void ThreadOneCode ()

6 .{

7. // some code here

8.

9. lock (guard)

10 .{

11. a = a + 1;

12 .}

13.

14. // some other code here

15.

16 .}

17.

18. void ThreadTwoCode ()

19 .{

20. // some code here

21.

22. lock (guard)

23 .{

24. a = a-1000;

25 .}

Copy code

 

All are locked in guard to ensure that only one thread accesses it through guard at the same time. You can use any suitable object. Nowadays, you may have various problems. For example, if you want to lock more than one thing, it may be nested with each other. What should we do?

 

This class is called Loom, which allows you to easily run code in another thread. Here there are two functions worth attention:

 

RunAsync (Action)-a group of code running on another thread.

 

QueueOnMainThread (Action, [Optional] float time)-statements running in the main thread (optional delay ).

 

Use Loom. Current to access Loom-create an invisible GameObject to process the interaction of the main game thread. The following example uses Loom to update the result of all vertices in a grid.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.