Using the Unity Main Thread Dispatcher script
The VIVEPORT SDK APIs use callback functions to return their execution results. The various callback functions are called from threads other than the main thread. However, Unity requires function calls or actions (such as UI manipulation) to come from the main thread.
To solve this problem, use the Unity Main Thread Dispatcher script with your VR content to dispatch callback functions to the main thread. Here’s how:
-
Create an empty GameObject in your scene and name it as
MainThreadDispatcher
. -
You’ll find the script
MainThreadDispatcher.cs
under Assets/Viveport/Scripts/ in the VIVEPORT SDK. Attach this script to the GameObject you created. -
Add the following at the beginning of your script:
using Viveport
-
Use the following convention to execute an action from the main thread:
MainThreadDispatcher.Instance().Enqueue(YourAction)