Skip to main content

Unity Complete Starter

This is the complete Unity starter used by the Unity tutorials. It creates or joins a room, attaches the package's native media device, and chooses the conference, webinar, or broadcast profile for the outcome you select.

Prerequisites

  • Unity 2022.3 or later.
  • A project with the native media plugin required by com.mediasfu.unity@0.1.0-preview.2.
  • A self-hosted endpoint for a local test, or a backend-approved room entry for a distributed app. Never save a reusable MediaSFU account credential in a scene, script, log, or player build.

Project files

Create a new Unity project. Replace Packages/manifest.json with the package entry below, then create the two scripts in Assets.

Packages/manifest.json:

{
"dependencies": {
"com.mediasfu.unity": "0.1.0-preview.2"
}
}

Assets/ProductScenarioRoom.cs:

using UnityEngine;
using MediaSFU.Unity;

namespace MediaSFU.Unity.Starter
{
public enum ProductScenario { CompactCall, HostDashboard, LiveGuestQueue, ClassroomWorkshop, RemotePodcast }

public sealed class ProductScenarioRoom : MonoBehaviour
{
[SerializeField] private ProductScenario scenario = ProductScenario.CompactCall;
public MediaSfuEventType EventType => scenario switch
{
ProductScenario.LiveGuestQueue => MediaSfuEventType.Webinar,
ProductScenario.RemotePodcast => MediaSfuEventType.Broadcast,
_ => MediaSfuEventType.Conference
};
}
}

Assets/MediaSFURoomStarter.cs:

using UnityEngine;
using MediaSFU.Unity;
using MediaSFU.Unity.Starter;

public sealed class MediaSFURoomStarter : MonoBehaviour
{
[SerializeField] private string participantName = "guest";
[SerializeField] private string meetingId = "";
[SerializeField] private bool createAsHost;
[SerializeField] private MediaSfuConnectionMode connectionMode = MediaSfuConnectionMode.SelfHosted;
[SerializeField] private string selfHostedEndpoint = "";
private MediaSfuClient client;
private IMediaSfuWebRtcDevice webRtcDevice;

private async void Start()
{
client = new MediaSfuClient(new MediaSfuClientOptions {
ConnectionMode = connectionMode, ConnectMediaSfu = true, LocalLink = selfHostedEndpoint,
Credentials = new MediaSfuCredentials()
});

var nativeEngine = MediaSfuNativePluginWebRtcEngine.TryCreateEngine();
if (!nativeEngine.Success || nativeEngine.Value == null) { Debug.LogError(nativeEngine.Detail); return; }
webRtcDevice = new MediaSfuWebRtcEngineDevice(nativeEngine.Value);
client.AttachWebRtcDevice(webRtcDevice);

var eventType = GetComponent<ProductScenarioRoom>()?.EventType ?? MediaSfuEventType.Conference;
MediaSfuOperationResult<MediaSfuRoom> room = createAsHost
? await client.CreateRoomAsync(new MediaSfuCreateRoomRequest { UserName = participantName, EventType = eventType })
: await client.JoinRoomAsync(new MediaSfuJoinRoomRequest { MeetingId = meetingId, UserName = participantName, IsLevel = "0" });
if (!room.Success) { Debug.LogError(room.Detail); return; }
var connected = await client.ConnectMediaAsync();
if (!connected.Success) Debug.LogError(connected.Detail);
}

private async void OnDestroy()
{
if (client == null) return;
await client.LeaveRoomAsync();
client.Dispose();
webRtcDevice?.Dispose();
}
}

In Unity, create an empty GameObject, add both scripts, then set the display name, outcome, and either a meeting ID or Create As Host in the Inspector. For the room's supplied inspector controls, import Basic Room Flow from the MediaSFU package's Samples tab in Unity Package Manager and add MediaSfuBasicRoomFlow to the same GameObject. That optional component exposes the package's currently available room actions such as chat, device controls, screen share, waiting-room decisions, recording controls, polls, whiteboard, and breakouts.

Run

Unity.exe -projectPath .

Enter Play mode after supplying a valid self-hosted endpoint or backend-approved room configuration.

What you should see

Unity reports a room connection or a useful error in the Console. With the native media plugin installed and an approved room, join with a second player to confirm local capture, remote media, and normal leave.

Recovery

If the native device cannot be created, read the Console message and install the required platform plugin before trying again. If access is refused, obtain a fresh room approval rather than placing an account key in the Unity scene.

Cleanup

Stop Play mode or leave the room. The component leaves, disposes the room client and native media device, and the scene should also remove its own listeners, timers, and cached room information.