Low Level API
The ODIN Unity SDK provides a low-level API that allows you to create your own voice-chat experience from scratch.
This manual handles the low-level API and the classes that are provided with the SDK. If you are looking for easy to use prefabs and components that are provided with the SDK, please refer to the High Level API.
Getting Started
The low-level API is designed to give you full control over the voice-chat experience in your game. It is a set of
classes that you can use to create your own voice-chat experience from scratch. All starts with
the Room
Please see the Sample InstanceSampleBasic
that is provided with the SDK to see how to use the low-level API.
Please note: The sample code below only shows the most important parts of the sample. It skips error handling and
validation for the sake of simplicity. Please refer to the sample code for a complete example.
Creating a Room
The Room
Handling Events
Next step is to handle events that are fired by the room. The room will fire events for all important actions that are happening in the room. You can subscribe to these events and handle them in your game.
The events are being called in a different thread than the main thread! You need to make sure that you handle the events in a thread-safe way and you need to make sure that any call into the Unity API is dispatched to the main thread.
If you need to call the Unity API, you need to dispatch the call to the main thread. You can create a ConcurrentQueue
instance in your class and dispatch the call to the main thread in the Update
method of your MonoBehaviour. There are
many examples on how to handle these situations in the Unity community. But here is a quick example:
Joining a Room
Once you have created the room and subscribed to the events, you can join the room. You just need to create a token and join the room with it.
To join a room, you just need to set the Room.Token Room
Tokens are typically generated by your backend and are used to authenticate the user and to provide the user with the necessary information to join the room. You should never generate tokens in your frontend or client application as this would expose your secret keys to the public. See Access Keys in ODIN for more information on that topic. We have an example on how to generate tokens using a simple NodeJS script in a cloud function: Generating Tokens.
If you want to test ODIN without setting up your own backend, you can use this code to create a token for testing
purposes.
Please note: The function ( OdinRoom.GenerateTestToken
You can create an access key for testing purposes for up to 25 users directly in this web component:
Click on the button to create an access key that you can use for free for up to 25 concurrent users.
Adding Media
Once you have joined the room, you can add media to the room, i.e. add the user's microphone stream to the room. If you want users to be connected to each other immediately, you can add the media right after joining the room.
You'll need to have a OdinMicrophoneReader
It is important to understand what happens here: Imagine the ODIN as an audio pipeline. The microphone has some output and we need to connect that output to the input of the room. With this code, we do exactly that. We link the output of the microphone to the input of the room. The room will then send the audio data to the server and the server will send the audio data to all other peers in the room.
Creating a Playback Stream
To hear other users in the room, you need to create a playback stream for each media created by every user/peer in the
room. You can do this by subscribing to the Room.OnMediaStarted
Of course, you also need to handle the case if users disconnect or remove their medias from the room (i.e. muting
themselves). You can do this by subscribing to the Room.OnMediaStopped
Testing the setup
Just press Play in the Unity Editor to test your basic ODIN integration. The operating system will ask you for permission to use the microphone. You need to build the client and deploy it to a colleague or another machine to test the voice chat integration.
You can also use our free Web Client to test your setup. You'll just open our Web Client in your browser, set the same access key and room name and you can talk to your Unity client. You can find a guide on how to use the Web Client here.
You should now have simple one-room non-spatial voice chat working. Anyone in the same room can hear and talk to each
other. During play and when others are connecting to the same room, you'll see new GameObjects being created in the
hierarchy for peers (they have an OdinPeer OdinMedia
Implementing 3D spatial voice-chat
Implementing a 3D spatial voice-chat requires a bit more work. It's basically the same as the 2D non-spatial voice-chat, but, these two things need to be done:
- First, the
spatialBlend
of the AudioSource needs to be set to1.0
which is 3D sound. This enables Unity to calculate the volume and direction of the sound based on the position of the GameObject in the scene. - Second, the
OdinPeer
component (and thus subsequentOdinMedia
objects) needs to be attached to a GameObject that is moving in the scene. This is typically the GameObject that represents the player or the avatar of the player.
The actual implementation is different for each game or application and depends on how you have set up your multiplayer
framework. Typical multiplayer frameworks like Mirror Networking, Unity Netcode for GameObjects or Photon PUN assign
connectionIds to players. You can use these connectionIds to identify the player and attach
the OdinPeer
Odin has their own ids for peers and media, so to some extent you need to map ODIN’s user ids to (connection) ids in your game. When connecting to a room in ODIN, you can set a user data object that will be sent to all other peers in the room. This user data object can contain the connection id of the player in your game. You can then use this connection id to map the Odin user id to the player in your game.
That's all there is to do to change 2D non-spatial voice-chat to 3D spatial voice-chat.
We have a guide on how to implement 3D spatial voice-chat using the Mirror Networking framework with the 1.x version of the Unity SDK, but the principles are the same and can be applied to any multiplayer framework. You can find the guide here: Unity Mirror Integration.
Mixing 2D and 3D voice-chat
In some games or applications, you might want to mix 2D non-spatial voice-chat with 3D spatial voice-chat. As use-case for this is that you have 3D spatial voice-chat for players in a "world" room where every player can hear and talk to each other based on their position in the world, but you could also have a radio room for separate teams that have 2D non-spatial voice-chat.
Imagine CounterStrike where you have 3D spatial voice-chat for the players in the world, but you also have 2D non-spatial voice-chat for the players in the same team. If a player is talking via radio, only the players in the same team can still be heard by the enemy in 3D spatial voice-chat. So, it might be a good strategy to quickly sneak up to the enemy and listen to what they try to privately discuss on their radio.
To do that, you create two Room
Next steps
You should now have a basic understanding of how to use the high-level API of the ODIN Unity SDK. You can now start implementing your own voice chat solution in your game or application. Check out the scripting API for more information on the actual functionality.
Switch to scripting