Skip to content

oliverfcarson/unity

 
 

Repository files navigation

PubNub Unity SDK (V6)

Build Status Build status

This is the official PubNub Unity SDK repository.

PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms.

Get keys

You will need the publish and subscribe keys to authenticate your app. Get your keys from the Admin Portal.

Configure PubNub

  1. Download the PubNub Unity package from this repository.

  2. Import the package to your Unity project by going to Assets > Import Package > Custom Package.

  3. You will receive a number of compiler errors due to the PubNub Playmode tests. The errors can be resolved based on your Unity version.

  4. If you are using a recent Unity version (greater than 2018 LTS):

    1. In the Project window, navigate to Assets > PubNub > PlayModeTests.
    2. Right click the file PlayModeTests.cs and click Delete. Press the Delete button in the pop-up dialog to confirm.
    3. Let the Unity Editor compile to finalize these changes and remove all compiler errors.
  5. If you are using a Unity version equal to or older than 2018 LTS, you will need to enable the PubNub Unity package in the Test Runner:

    1. Navigate to Window > General > Test Runner.
    2. Click the mini drop down menu next to the window close button, and click Enable playmode tests for all assemblies.
    3. Restart your Unity editor to finalize these changes.
  6. Configure your keys:

    using PubNubAPI;
    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.PublishKey = "my_pubkey";
    pnConfiguration.SecretKey = "my_secretkey";
    pnConfiguration.LogVerbosity = PNLogVerbosity.BODY;
    pnConfiguration.UserId = "myUniqueUserId";
  7. If your Unity application contains existing Assembly Definitions, you might receive CS0246 compiler errors, as the PubNubAPI namespace may not be recognized. You will need to create an assembly definition to use the PubNubAPI in your application.

    1. Navigate to the Assets > PubNub folder in the Project window.
    2. Right-click the PubNub folder and create an Assembly Definition by selecting Create > Assembly Definition. Assign a name to the asset (such as PubNub) and leave the default settings.
    3. Navigate to the folder that you wish to use the PubNubAPI namespace. Click on the Assembly Definition file.
    4. In the Inspector window, add the PubNub Assembly Definition you created earlier to the list of Assembly Definition References section by clicking on the + icon.
    5. Scroll down in the Inspector window, and click Apply. The PubNubAPI namespace should now be visible in your application.
    6. You may receive more compiler errors when trying to use the PubNubAPI namespace. These come from the PubNub > Editor folder, and contain test files. Delete the entire PubNub > Editor folder and allow the Unity editor to recompile changes.

Add event listeners

pubnub.SubscribeCallback += SubscribeCallbackHandler;

//Handler
void SubscribeCallbackHandler(object sender, EventArgs e) {
    SubscribeEventEventArgs mea = e as SubscribeEventEventArgs;

    if (mea.Status != null) {
        switch (mea.Status.Category) {
            case PNStatusCategory.PNUnexpectedDisconnectCategory:
            case PNStatusCategory.PNTimeoutCategory:
                // handle publish
            break;
        }
    }
    if (mea.MessageResult != null) {
        Debug.Log("Channel" + mea.MessageResult.Channel);
        Debug.Log("Payload" + mea.MessageResult.Payload);
        Debug.Log("Publisher Id: " + mea.MessageResult.IssuingClientId);
    }
    if (mea.PresenceEventResult != null) {
        Debug.Log("SubscribeCallback in presence" + mea.PresenceEventResult.Channel + mea.PresenceEventResult.Occupancy + mea.PresenceEventResult.Event);
    }
    if (mea.SignalEventResult != null) {
        Debug.Log ("SubscribeCallback in SignalEventResult" + mea.SignalEventResult.Channel + mea.SignalEventResult.Payload);
    }
    if (mea.UUIDEventResult != null) {
        Debug.Log(mea.UUIDEventResult.Name);
        Debug.Log(mea.UUIDEventResult.Email);
        Debug.Log(mea.UUIDEventResult.ExternalID);
        Debug.Log(mea.UUIDEventResult.ProfileURL);
        Debug.Log(mea.UUIDEventResult.UUID);
        Debug.Log(mea.UUIDEventResult.ETag);
        Debug.Log(mea.UUIDEventResult.ObjectsEvent);
    }
    if (mea.ChannelEventResult != null) {
        Debug.Log(mea.ChannelEventResult.Name);
        Debug.Log(mea.ChannelEventResult.Description);
        Debug.Log(mea.ChannelEventResult.ChannelID);
        Debug.Log(mea.ChannelEventResult.ETag);
        Debug.Log(mea.ChannelEventResult.ObjectsEvent);
    }
    if (mea.MembershipEventResult != null) {
        Debug.Log(mea.MembershipEventResult.UUID);
        Debug.Log(mea.MembershipEventResult.Description);
        Debug.Log(mea.MembershipEventResult.ChannelID);
        Debug.Log(mea.MembershipEventResult.ObjectsEvent);
    }
    if (mea.MessageActionsEventResult != null) {
        Debug.Log(mea.MessageActionsEventResult.Channel);
        if(mea.MessageActionsEventResult.Data!=null){
            Debug.Log(mea.MessageActionsEventResult.Data.ActionTimetoken);
            Debug.Log(mea.MessageActionsEventResult.Data.ActionType);
            Debug.Log(mea.MessageActionsEventResult.Data.ActionValue);
            Debug.Log(mea.MessageActionsEventResult.Data.MessageTimetoken);
            Debug.Log(mea.MessageActionsEventResult.Data.UUID);
        }
        Debug.Log(mea.MessageActionsEventResult.MessageActionsEvent);
        Debug.Log(mea.MessageActionsEventResult.Subscription);
    }
}

Publish/subscribe

pubnub.Publish()
	.Channel("channel1")
	.Message("test message")
	.Async((result, status) => {    
		if (!status.Error) {
			Debug.Log(string.Format("Publish Timetoken: {0}", result.Timetoken));
		} else {
			Debug.Log(status.Error);
			Debug.Log(status.ErrorData.Info);
		}
	});

pubnub.Subscribe()
    .Channels(new List<string>() {
        "my_channel"
    })
    .Execute();

Documentation

Support

If you need help or have a general question, contact [email protected].

About

PubNub for Unity3D 5.x

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.6%
  • Shell 1.3%
  • PowerShell 0.1%