Configuration API for Unity SDK
Complete API reference for building real-time applications on PubNub with the Unity Software Development Kit (SDK). This page covers configuration, initialization, and event handling with concise, working examples.
Even though you can configure and initialize the Unity SDK directly from Unity Editor via assets, you can also do it programmatically.
Refer to Getting Started for instructions on how to set up the Unity SDK using assets.
Build platform configuration
Different Unity build platforms may require specific configuration to ensure the PubNub SDK functions correctly.
Mobile build configuration
When building for Android or iOS platforms, you might encounter an issue where the PubNub Unity SDK works perfectly in the Unity Editor but fails to receive messages when deployed to mobile devices. This is typically caused by Unity's code stripping optimization removing PubNub SDK assemblies during the build process.
For detailed solutions to resolve this issue, refer to the Troubleshooting document.
WebGL configuration
The PubNub Unity SDK is compatible with Unity WebGL builds. To configure your project to build for WebGL:
Enable Web GL build mode only for builds
Using UnityWebGLHttpClientService outside of WebGL builds (including the editor) might cause unexpected behavior due to UnityWebRequest being thread-unsafe.
-
In your Unity Editor project tree, right-click any folder and navigate to Create -> PubNub -> PubNub Config Asset. This step creates a new scriptable object where you provide your PubNub account information.
Existing Config asset
If you have already created a config asset, you don't have to create a new one.
-
Open the scriptable
PNConfigAssetobject and mark the Enable Web GL Build Mode checkbox. This setsUnityWebGLHttpClientServiceas the transport layer inPnManagerBehaviourduring initialization.If you don't use
PnManagerBehaviour, initialize PubNub with the WebGL build mode using one of the following methods:11 -
Go to Edit -> Project Settings -> Player and set Managed Stripping Level to
Minimal.Additional HTTP setup
If for some reason you can't turn on the
Secureoption in eitherPNConfigurationor the Scriptable Object config file, then you need to also go to Project Settings -> Player -> WebGL Settings and set the Allow downloads over HTTP option to Always allowed.
-
Install the WebGL Threading Patcher. Navigate to Window -> Package Manager, click +, select Add package from git URL, paste the link to the Threading Patcher's GIT repository, and click Add.
These steps configure your project for WebGL builds only. Configure other targets as needed.