Troubleshooting Unity SDK
Handling errors with exceptions
The PubNub Unity SDK normally returns error objects through callbacks during operation failures. While this approach is suitable for production environments, it can limit debugging capabilities during development.
Enabling exceptions provides more comprehensive stack traces and integrates with Unity's debugging tools, allowing errors to be caught at their source.
Enable exceptions
The callback can throw exceptions instead of errors. To enable exceptions:
-
In the Unity Editor, go to
File
->Build Settings
->Player Settings
->Other Settings
->Configuration
>Scripting define symbols
. Do this for each of the platforms you want to enable exceptions. -
After you type
ENABLE_PUBNUB_EXCEPTIONS
, remember to press Return with the focus in the text box, or Unity Editor will not save your setting.
Messages not received on mobile builds
The PubNub Unity SDK works correctly in the Unity Editor but fails to receive messages when deployed to Android or iOS devices. This issue occurs when Unity's code stripping optimization removes PubNub SDK assemblies during the build process.
When this happens, PubNub will initialize successfully, event listeners are added, and channels are subscribed to, but no messages are received on the device. You'll notice that messages appear correctly in the PubNub Debug Console, confirming that the service itself is working properly.
Unity's Managed Stripping Level removes code it determines as unused, but it cannot detect that PubNub assemblies are required at runtime, so it incorrectly strips them during the build process.
Fix with code stripping settings
-
In Unity Editor, go to Edit -> Project Settings -> Player.
-
Set Managed Stripping Level to
Minimal
.
Project-wide impact
This setting affects your entire Unity project and may increase binary size.
Fix with link.xml file
Create a link.xml
file in your project's Assets folder (root level) with the following content:
<linker>
<assembly fullname="PubNubAPI" preserve="all"/>
<assembly fullname="CBOR" preserve="all"/>
<assembly fullname="Numbers" preserve="all"/>
<assembly fullname="PubnubApiUnity" preserve="all"/>
<assembly fullname="URIUtility" preserve="all"/>
</linker>
This approach preserves only the necessary PubNub assemblies while maintaining optimization for the rest of your project.