Aki Jirou
Aki Jirou

Reputation: 29

Spawn object with Multithread in Unity

i'm a beginner programmer. And i want to create an Objectspawner sort of like a middleware that Process Pathfinding script in a multi threads then pass it back to Gameobject to execute. Basically each object that call for pathfinding, the calculation process will be handle by different threads. what should i learn to create this ?

Upvotes: 0

Views: 4105

Answers (2)

Programmer
Programmer

Reputation: 125305

This is possible. You can use Unity's structures and math API's like Vector3, Vector2 and Mathf in another Thread.

Get the position of the Object, store it in a Vector3 or Vector2 then start a new thread and pass that Vector to it. You can do all your pathfinding work in your new Thread.

Whenever you need to update the Object's position or use that modified Vector from the new Thread, use the UnityThread.executeInUpdate function from my other post to do this. This function allows you to call Unity API from another Thread. You can make yours if you don't want to use mine or you can read how it is made from that post then make your own from it.

Upvotes: 1

zambari
zambari

Reputation: 5035

While its possible to start seperate threads in Unity, its own API does not support calls from any other thread that its main thread, in which it executes your scripts. Instancing an object is the last thing you want to do while not in the main thread. Why do you think you need threads?

Upvotes: 0

Related Questions