- Unity 2019.x.x or newer
- The following Unity Player settings must be applied:
- Scripting Runtime Version should be '.NET 4.x Equivalent'
- API Compatibility Level should be '.NET Standard 2.0'
- Please download the latest Unity package from the GitHub releases page. All releases from 1.2.4 have
.unitypackage
included.
- You can import the package by going to Assets -> Import Package -> Custom Package in the Unity UI. For more detailed information on importing packages, visit https://docs.unity3d.com/Manual/AssetPackagesImport.html.
- Make sure to disable assembly validation if your project fails to build due to conflicts with Unity's internal newtonsoft json library.
- Please set
ClientOptions.AutomaticNetworkStateMonitoring
tofalse
when instantiating the Ably Client Library, since this feature is not supported and will throw a runtime exception. - Custom NewtonSoft JSON DLLs under
Plugins
can be removed, in the case of conflict with other NewtonSoft DLLs in the project, or if the use of inbuilt Newtonsoft is preferred. - Configure SynchronizationContext to execute callbacks on Main/UI thread.
- Sample code :
using System;
using System.Threading;
using IO.Ably;
using IO.Ably.Realtime;
using UnityEngine;
using UnityEngine.UI;
namespace Example.ChatApp
{
public class AblyConsole : MonoBehaviour
{
private AblyRealtime _ably;
private ClientOptions _clientOptions;
// It's recommended to use other forms of authentication. E.g. JWT, Token Auth
// This is to avoid exposing root api key to a client
private static string _apiKey = "ROOT_API_KEY_COPIED_FROM_ABLY_WEB_DASHBOARD";
void Start()
{
InitializeAbly();
}
private void InitializeAbly()
{
_clientOptions = new ClientOptions
{
Key = _apiKey,
// this will disable the library trying to subscribe to network state notifications
AutomaticNetworkStateMonitoring = false,
AutoConnect = false,
// this will make sure to post callbacks on UnitySynchronization Context Main Thread
CustomContext = SynchronizationContext.Current
};
_ably = new AblyRealtime(_clientOptions);
_ably.Connection.On(args =>
{
Debug.Log($"Connection State is <b>{args.Current}</b>");
});
}
}
- Please take a look at Unity demo chat app to see a functioning Ably SDK setup.
- Also check blog on Multiplayer game in unity with ably.
- It doesn't support WebGL due to incompatibility with WebSockets. Read the Direct Socket Access section under WebGL Networking. We have active issue to add support for the same #1211.
- To support WebGL, you should refer to interation with browser javascript from WebGL. You can import ably-js as a browser javascript and call it from WebGL. For more information, refer to the project Ably Tower Defence.
- Please take a look at the contributing doc for information relating to local development setup, writing and running tests.
- Detailed information related to updating Ably DLLs using Unity Plug-ins is available in updating-ably-unitypackage.