Unity SDK Guide
Unity package install, join, render and events.
The Unity SDK has the C# RtcEngine + IRtcEngineEventHandler shape with capitalized method names (Initialize, JoinChannel, OnUserJoined) — the same as the Agora Unity reference version.
Prerequisites
- Unity 2022.3 LTS+ (IL2CPP or Mono).
- Platform permissions:
CAMERA/RECORD_AUDIOon Android, a usage description on iOS, the system camera permission on Windows/macOS. - An
appIdand a token service (see Enable Services and Get Credentials).
Installation
Planned distribution:
1) UPM: add "com.nimeet.rtc": "file:../ni-meet/sdk/unity" to Packages/manifest.json
2) or import the .unitypackage (Assets/Nimeet/Rtc)
Unity SDK installation and version information follow the published package; the examples on this page use the public interface.Get a token
The game backend issues the token; the Unity client only receives a string. Do not write the App Certificate into the client. Production advice: hand out a short-lived token at login and renew it against the same backend inside OnTokenPrivilegeWillExpire.
Join a channel
using Nimeet.Rtc;
public class MeetingController : MonoBehaviour, IRtcEngineEventHandler
{
private IRtcEngine engine;
void Start()
{
engine = RtcEngine.CreateAgoraRtcEngine();
var context = new RtcEngineContext { appId = AppConfig.AppId, eventHandler = this };
engine.Initialize(context);
engine.SetChannelProfile(ChannelProfileType.ChannelProfileLiveBroadcasting);
engine.EnableVideo();
engine.StartPreview();
var options = new ChannelMediaOptions
{
clientRoleType = ClientRoleType.ClientRoleBroadcaster,
autoSubscribeAudio = true,
autoSubscribeVideo = true,
};
engine.JoinChannel(AppConfig.RtcToken, "weekly-sync", 10001, options);
}
public void OnJoinChannelSuccess(RtcConnection connection, int elapsed) => Debug.Log("joined");
public void OnUserJoined(RtcConnection connection, int remoteUid, int elapsed) { }
public void OnUserOffline(RtcConnection connection, int remoteUid, UserOfflineReasonType reason) { }
public void OnTokenPrivilegeWillExpire(string token) { engine.RenewToken(FetchFreshToken()); }
public void OnError(int err, string msg) => Debug.LogError($"rtc {err}: {msg}");
void OnDestroy() => RtcEngine.Destroy();
}Publishing and subscribing
engine.MuteLocalAudioStream(true);
engine.MuteLocalVideoStream(true);
engine.EnableLocalVideo(false);
engine.UpdateChannelMediaOptions(options);Screen sharing: GetScreenCaptureSources enumerates sources → StartScreenCaptureByDisplayId / StartScreenCaptureByWindowId; in Unity the shared picture is usually rendered into a RawImage (texture callback) rather than a VideoSurface.
Events
IRtcEngineEventHandler is implemented explicitly in C#: OnJoinChannelSuccess, OnUserJoined, OnUserOffline, OnFirstRemoteVideoDecoded, OnTokenPrivilegeWillExpire, OnError, OnConnectionStateChanged.
Error handling
- Synchronous methods return
int;0means success and a negative value is an Agora error number; - Asynchronous errors arrive through
OnError; - Unimplemented capabilities return
-4(ERR_NOT_SUPPORTED).
Troubleshooting
| Symptom | Cause | Handling |
|---|---|---|
| Crash after an IL2CPP build | The native library did not make it into the package | Check that Plugins/<platform> contains the matching ABI |
| Fine in the editor, black screen on device | Permissions were not requested | Request the camera/microphone on the first launch on device |
| Touching the UI inside a callback errors | Not on the main thread | Queue the work and handle it in Update() |
| The picture is upside down on Android | Front-camera mirroring | Adjust it with VideoCanvas.mirrorMode |
| Low frame rate | Encoding parameters are too high | Lower the resolution/frame rate in SetVideoEncoderConfiguration |
Next steps
- Full interfaces and overloads: API reference for this platform
- Error codes and troubleshooting: Error code table, FAQ
- Issue tokens on your server: Token authentication
Platform information
| Item | value |
|---|---|
| Platform | Unity(C#) |
| Language | C# |
| SDK package | Nimeet.Rtc |
| Interface version | 4.6.2 |
| Compatible Agora API | Agora unity API |
The public interfaces are aligned with the Agora unity API; installation and runtime requirements are listed under Prerequisites above.
Availability
This platform's SDK: Generally available. The examples use the public API; runtime capability follows the published SDK package.
join-channel flow
The method names, parameter types and return values match the API reference for the same platform. Where a method has several overloads, the most common one is shown here; see the API reference for all overloads.
| Step | Interface | Method | Overload count |
|---|---|---|---|
| subscribe to remote media | IBaseSpatialAudioEngine | MuteRemoteAudioStream | 1 |
| Create instance / initialize | ILocalSpatialAudioEngine | Initialize | 1 |
| Renew the Token | IMusicContentCenter | RenewToken | 1 |
| enable video and local preview | IRtcEngine | EnableVideo | 1 |
| Join a channel | IRtcEngine | JoinChannel | 2 |
| Leave a channel | IRtcEngine | LeaveChannel | 2 |
| Channel scenarios | IRtcEngine | SetChannelProfile | 1 |
| switch roles | IRtcEngine | SetClientRole | 2 |
| bind the local video | IRtcEngine | SetupLocalVideo | 1 |
MuteRemoteAudioStream(uid: uint, mute: bool): int
Initialize(): int
RenewToken(token: string): int
EnableVideo(): int
JoinChannel(token: string, channelId: string, info?: string, uid?: uint): int
LeaveChannel(): int
SetChannelProfile(profile: CHANNEL_PROFILE_TYPE): int
SetClientRole(role: CLIENT_ROLE_TYPE): int
SetupLocalVideo(canvas: VideoCanvas): intEvents and callbacks
This SDK has no event callbacks.
Error codes
| Constant | value |
|---|---|
ERR_ABORTED | 20 |
ERR_ADM_GENERAL_ERROR | 1005 |
ERR_ADM_INIT_PLAYOUT | 1008 |
ERR_ADM_INIT_RECORDING | 1011 |
ERR_ADM_START_PLAYOUT | 1009 |
ERR_ADM_START_RECORDING | 1012 |
ERR_ADM_STOP_PLAYOUT | 1010 |
ERR_ADM_STOP_RECORDING | 1013 |
ERR_ALREADY_IN_USE | 19 |
ERR_BITRATE_LIMIT | 115 |
ERR_BUFFER_TOO_SMALL | 6 |
ERR_CLIENT_IS_BANNED_BY_SERVER | 123 |
ERR_CONNECTION_INTERRUPTED | 111 |
ERR_CONNECTION_LOST | 112 |
ERR_DATASTREAM_DECRYPTION_FAILED | 122 |
ERR_DECRYPTION_FAILED | 120 |
ERR_ENCRYPTED_STREAM_NOT_ALLOWED_PUBLISH | 130 |
ERR_FAILED | 1 |
ERR_INIT_NET_ENGINE | 21 |
ERR_INVALID_APP_ID | 101 |
ERR_INVALID_ARGUMENT | 2 |
ERR_INVALID_CHANNEL_NAME | 102 |
ERR_INVALID_STATE | 8 |
ERR_INVALID_TOKEN | null |
ERR_INVALID_USER_ACCOUNT | 134 |
ERR_INVALID_USER_ID | 121 |
ERR_JOIN_CHANNEL_REJECTED | 17 |
ERR_LEAVE_CHANNEL_REJECTED | 18 |
ERR_LOAD_MEDIA_ENGINE | 1001 |
ERR_NOT_INITIALIZED | 7 |
ERR_NOT_IN_CHANNEL | 113 |
ERR_NOT_READY | 3 |
ERR_NOT_SUPPORTED | 4 |
ERR_NO_PERMISSION | 9 |
ERR_NO_SERVER_RESOURCES | 103 |
ERR_OK | 0 |
ERR_PCMSEND_BUFFEROVERFLOW | 201 |
ERR_PCMSEND_FORMAT | 200 |
ERR_REFUSED | 5 |
ERR_RESOURCE_LIMITED | 22 |
ERR_SET_CLIENT_ROLE_NOT_AUTHORIZED | 119 |
ERR_SIZE_TOO_LARGE | 114 |
ERR_STREAM_MESSAGE_TIMEOUT | 117 |
ERR_TIMEDOUT | 10 |
ERR_TOKEN_EXPIRED | 109 |
ERR_TOO_MANY_DATA_STREAMS | 116 |
ERR_VDM_CAMERA_NOT_AUTHORIZED | 1501 |
IRtcEngine | null |
the platform REST layer uses REST error codes;the SDK layer maps connection/authentication failures to Agora error numbers, for the mapping table see SDK error identifiers per platform。