Skip to content
pubnubPublic

About

PubNub for Unity3D 5.x

Resources

Stars

35 stars

Watchers

20 watching

Forks

Repository files navigation

unity-header

PubNub Unity SDK

PubNub Unity SDK Build status

PubNub provides global infrastructure for real-time, interactive applications.

Publish and receive messages in Unity. Use this SDK for Unity applications targeting mobile, desktop, WebGL, VR, and AR.

Documentation · API reference · Changelog · Support

Requirements

Requirement Supported version or setup
Unity Editor Unity 2018.4.26f1 or later
Build targets Mobile, Windows, macOS, Linux, WebGL, VR, and AR
Unity Package Manager After installing the package if you want to use the optional pre-made Unity-specific assets, run PubNub > Set up templates as documented by the SDK, restart Unity if prompted, and configure the generated PubNub assets/components.

Installation

In Unity Package Manager, choose Add package from git URL and enter:

https://github.com/pubnub/unity.git?path=/PubNubUnity/Assets/PubNub

For other installation methods, see environment setup.

Quickstart

This example runs in a Unity scene. It subscribes to hello_world, publishes one message, and prints the received text.

Get your keys

  1. Open the PubNub Admin Portal.
  2. Create an app and a keyset for development, or select an existing development keyset.
  3. Copy its publish key and subscribe key.

If Access Manager is enabled, obtain a token from your trusted backend and call pubnub.SetAuthToken(token). Keep the secret key out of the distributed Unity application.

Send and receive a message

Create PubNubHelloWorld.cs, attach it to a GameObject in the active scene, and add:

Replace the key placeholders. Use a User ID that identifies the user or device in your app.

using UnityEngine;
using PubnubApi;
using PubnubApi.Unity;

public class PubNubHelloWorld : MonoBehaviour
{
    private Pubnub pubnub;
    private SubscribeCallbackListener listener;
    private bool published;

    void Start()
    {
        PNConfiguration config = new PNConfiguration(new UserId("hello-world-user"))
        {
            SubscribeKey = "YOUR_SUBSCRIBE_KEY",
            PublishKey = "YOUR_PUBLISH_KEY"
        };

        pubnub = PubnubUnityUtils.NewUnityPubnub(config);

        listener = new SubscribeCallbackListener();
        listener.onMessage += (pn, message) =>
        {
            Debug.Log(message.Message);
            pubnub.UnsubscribeAll();
        };
        listener.onStatus += (pn, status) =>
        {
            if (
                status.Category == PNStatusCategory.PNConnectedCategory &&
                !published
            )
            {
                published = true;
                _ = pubnub.Publish()
                    .Channel("hello_world")
                    .Message("Hello world")
                    .ExecuteAsync();
            }
        };

        pubnub.AddListener(listener);
        pubnub.Subscribe<string>()
            .Channels(new[] { "hello_world" })
            .Execute();
    }

    void OnDestroy()
    {
        if (pubnub == null)
        {
            return;
        }

        pubnub.UnsubscribeAll();

        if (listener != null)
        {
            pubnub.RemoveListener(listener);
        }
    }
}

Run the example

Enter Play Mode in Unity Editor.

The Unity Console should show:

Hello world

The example calls pubnub.UnsubscribeAll() after receiving the message and during OnDestroy(), and removes the registered listener during teardown.

For a complete application, see the getting started guide.

Next steps

Task Documentation
Configure the client Configuration
Work with subscriptions and messages Publish & Subscribe
Check channel occupancy Presence
Read message history Message Persistence

Build with an AI coding assistant

The PubNub MCP server gives an AI coding assistant access to PubNub SDK documentation and PubNub APIs. Connect the assistant to the hosted server at https://mcp.pubnub.com, or run npx @pubnub/mcp@latest locally.

The server repository has setup steps for VS Code, Cursor, Claude Code, Claude Desktop, Codex, Gemini CLI, and OpenCode.

Before production

Use Access Manager to grant each client the permissions it needs. Keep the secret key on your backend. Never include it in a distributed client.

Keep one PubNub client for the intended game/session lifetime rather than constructing clients as GameObjects are recreated. Call pubnub.UnsubscribeAll() to stop subscriptions and pubnub.RemoveListener(listener) when the listener's owner is destroyed.

Live delivery through PubNub SDKs is at-most-once. A subscriber can miss messages while disconnected or if its buffer overflows. For longer-gap recovery, see Message Persistence.

Troubleshooting

Symptom Check
The project cannot resolve PubNub types after package installation Install the package using the documented Git URL, run the PubNub template setup, and allow Unity to finish package import and script compilation. See the Unity SDK guide.
A publish succeeds but no message appears Check the message handler, subscription readiness, keyset, and channel name.
A mobile build loses PubNub types or callbacks after managed-code stripping Apply the stripping/linker configuration documented in Unity troubleshooting.
Extra clients or duplicate messages during development Keep one client at game/session scope and remove subscriptions/listeners during teardown. Do not accumulate listeners every time a scene or GameObject is recreated.

For setup help, see troubleshooting. Check network status for service incidents.

Releases

Read the changelog before upgrading.

For a major version change, review the Unity migration guidance linked from the Unity SDK documentation and the changelog.

Support and contributions

For setup or account help, contact PubNub Support.

For a reproducible SDK bug, use GitHub Issues. Include the SDK version, runtime, and a small reproduction with credentials removed.

Test the change in a supported Unity Editor version and include a reproducible test or scene for behavioral changes before opening a pull request.

License

See the PubNub Software Development Kit License.

About

PubNub for Unity3D 5.x

Resources

Stars

35 stars

Watchers

20 watching

Forks

Releases

Packages

Used by

Contributors

Languages