Several chat demos of Momento's Topics using Unity 2022.3.9f1.
- Install Unity 2022.3.9f1
- Clone this repository
- Download the latest MomentoSdkUnity-x.y.z.zip from https://github.com/momentohq/client-sdk-dotnet/releases and extract its contents into the folder
Assets/MomentoSdkUnity/
. This example demo requires client-dotnet-sdk release v1.30.1, or newer. - Rename
Assets/Scripts/secrets/Secrets.cs.template
toSecrets.cs
and paste your auth token inside it. This file is separate to discourage committing auth tokens. - Start Unity and open this folder
- Open the
Assets/Scenes/MomentoTopicsDemo-ModeratedChat.unity
scene file - Hit the play button to run inside the Unity Editor
If needed, add a firewall rule to allow your Unity or your built executable to access your network.
-
Tested with two Windows builds of the Unity project + subscribing/publishing to the Topic in the Momento Console website
-
There are five example scenes which run different example scripts. The
MomentoTopicsDemo.unity
,MomentoTopicsDemo-Coroutine.unity
, andMomentoTopicsDemo - HttpPubSub.unity
require either ensuring theMOMENTO_AUTH_TOKEN
environment variable is set or copyingAssets/Scripts/secrets/Secrets.cs.template
asAssets/Scripts/secrets/Secrets.cs
and pasting your auth token into the new file. Take care not to commit your token to source control. -
The first four scenes do not support building for WebGL currently. There is a fifth scene,
MomentoTopicsDemo - HttpPubSub.unity
, that uses an HTTP-based topics client suitable for WebGL builds.MomentoTopicsDemo.unity
(usingTopicsTest.cs
): utilizesTask.Run()
to run the subscription to the Momento Topic in a background threadMomentoTopicsDemo-Coroutine.unity
(usingTopicsTestCoroutine.cs
): utilizes Unity Coroutines to run the subscription asyncronously in the main thread.MomentoTopicsDemo-TokenVendingMachine.unity
(usingTopicsTestTokenVendingMachine.cs
): utilizes the example Momento Token Vending Machine to obtain a temporary, restricted scope Momento auth token. This is beneficial because (1) we no longer need to hard-code in a specific auth token into the app, and (2) we can utilize atokenId
embedded in the Topics message to more securely know which client/username sent a specific message. Note that you'll need to explicitly setup the Token Vending Machine separately and then specify its URL via thetokenVendingMachineURL
variable specified inTopicsTestTokenVendingMachine.cs
.MomentoTopicsDemo-ModeratedChat.unity
: corresponding Unity client for the Momento moderated chat demo (see https://github.com/momentohq/moderated-chat/tree/main and https://chat.gomomento.com/). As such, much of the code follows from the frontend web client that already exists.MomentoTopicsDemo - HttpPubSub.unity
(usingTopicsTestHttp.cs
): uses an HTTP API for publishing and subscribing to Momento topics suitable for building for the WebGL platform. The example demonstrates publishing and receiving both text and binary messages. Messages that contain an emoji picked from the emoji popup menu are published as binary, while messages that do not contain an emoji are published as plain text. The differences between the messages will not be apparent in the demo itself, but you can subscribe to the specified topic in the Momento Console to see the differences in message payload between messages that contain an emoji and those that do not.