Before you start, you’ll need to create a developer team on the Discord Developer Portal. This team will be used to manage your Discord applications and SDK integrations.If you already have a team configured, you can skip this step.
Enable the Public Client toggle in the OAuth2 tab.
This guide requires enabling Public Client to allow you to get started with the SDK quickly. Most games will not want to ship as a public client. This setting should be reviewed by your team prior to releasing your game. Learn more about public clients.
Click on the Downloads link under the Discord Social SDK section of the sidebar.
Select the latest version from the version dropdown and download the SDK for Unity.
A Unity sample project is available for download on this page, but we are not going to cover it in this guide. Explore it on your own after you finish this guide!
Let’s set up your Unity project to include the Social SDK package and add the necessary objects and scripts to use it.
Create a new 2D project in Unity Hub using Unity version 2021.3 or later
Either:
Unzip the zip file in the Unity Packages folder, or
Unzip the zip file and Install Package from Disk. Make sure the folder is in a directory that won’t get moved or deleted as your Unity project will load it from that location.
In your project add a Scripts folder and create a DiscordManager.cs script
Add the following code to DiscordManager.cs:
Copy
Ask AI
using UnityEngine;using UnityEngine.UI;using Discord.Sdk;using System.Linq;public class DiscordManager : MonoBehaviour{ [SerializeField] private ulong clientId; // Set this in the Unity Inspector from the dev portal [SerializeField] private Button loginButton; [SerializeField] private Text statusText; private Client client; private string codeVerifier;}
Add an empty object to the scene (GameObject > Create Empty) called DiscordManager and attach the DiscordManager.cs script to it
Add a button to the scene GameObject > UI > Legacy > Button
Add text to the scene GameObject > UI > Legacy > Text
Position the button and text somewhere visible on the screen
Attach the button and text to the DiscordManager in the inspector
Run it!
This is all we’ll need to get started! You shouldn’t see anything happen, but if you run into any issues, check out the troubleshooting section before moving to the next step.
Make sure the Social SDK package was successfully added to Unity
Mac libdiscord_partner_sdk.dylib Not Opened
On Mac you may get the error “libdiscord_partner_sdk.dylib” Not Opened because Apple couldn’t verify it. If this happens press Done on the popup.You’ll need to open your System Settings > Privacy & Security and scroll down to the Security section. It will tell you “libdiscord_partner_sdk.dylib” was blocked to protect your Mac. Press Open Anyway and try running again.Now when you get the pop up you’ll have the option to select Open Anyway and it will be able to use it successfully.
Let’s add some event handlers to monitor what’s happening with our Discord connection. We’ll set up two important callbacks:
A logging callback to see what the SDK is doing
A status callback to know when we can start using Discord features
We’ll start by adding the following code to your DiscordManager.cs:
Copy
Ask AI
void Start(){ client = new Client(); // Modifying LoggingSeverity will show you more or less logging information client.AddLogCallback(OnLog, LoggingSeverity.Error); client.SetStatusChangedCallback(OnStatusChanged); // Make sure the button has a listener if (loginButton != null) { //loginButton.onClick.AddListener(StartOAuthFlow); } else { Debug.LogError("Login button reference is missing, connect it in the inspector!"); } // Set initial status text if (statusText != null) { statusText.text = "Ready to login"; } else { Debug.LogError("Status text reference is missing, connect it in the inspector!"); }}private void OnLog(string message, LoggingSeverity severity){ Debug.Log($"Log: {severity} - {message}");}private void OnStatusChanged(Client.Status status, Client.Error error, int errorCode){ Debug.Log($"Status changed: {status}"); statusText.text = status.ToString(); if(error != Client.Error.None) { Debug.LogError($"Error: {error}, code: {errorCode}"); }}
This will hook up the API status changes to your text in the game. Then we’ll need to get your Client ID from the OAuth2 tab in the developer portal and paste it into the ClientID on the DiscordManager in the inspector.
Now, if you press play and click the button, it should start the OAuth flow! You’ll be redirected to your browser to log in and authorize the game. There will be some logging to the console, but the status won’t change yet.
Press play and click the button again to log in and authorize the game. This time you should see the status text change as it goes through the OAuth flow, and it should end up Ready
Let’s access the user’s Discord relationships (friends list) and display the count. This will help you understand how to access and use Discord data in your game.Lets add some new code to your DiscordManager.cs:
When the client status is Ready, it’ll call our ClientReady function, which will call Client::GetRelationships, returning us a list of all the player’s friends. We then log the number of friends directly to the console.
Let’s show your game’s activity on Discord using Rich Presence. This feature lets players see what others are doing in your game directly in their Discord friends list.Update your ClientReady method with this code:
Hit play and then click the button. Once the OAuth flow is complete, you will see the status hit Ready. The console will tell you if setting rich presence was successful, and you can check your Discord profile to see it!
Check out our in depth sample for using the Social SDK in Unity following the best practices laid out in these guides. It contains easy to drop in prefabs with both code and UI to quickly integrate Discord’s social features into your game.
Need help? Join the Discord Developers Server and share questions in the #social-sdk-dev-help channel for support from the community.If you encounter a bug while working with the Social SDK, please report it here: https://dis.gd/social-sdk-bug-report