Rooms

You can use the CloudService.Rooms API to create new Rooms or delete and fetch existing ones.

After logging in to coherence Cloud and fetching an existing Room, you can join it via the CoherenceBridge.JoinRoom method:

using Coherence.Cloud;
using Coherence.Toolkit;
using UnityEngine;

class CreateRoomExample : MonoBehaviour
{
    async void Start()
    {
        // Wait until a player account has logged in to coherence Cloud.
        // The CoherenceCloudLogin component can be used for this.
        PlayerAccount playerAccount = await PlayerAccount.GetMainAsync();

        // Fetch information about regions available in our Project.
        var regionsService = playerAccount.Services.Regions;
        var regions = await regionsService.FetchRegionsAsync();

        // Create a new room in the first available region
        var rooms = playerAccount.Services.Rooms;
        var roomsService = rooms.GetRoomServiceForRegion(regions[0]);
        var newRoom = await roomsService.CreateRoomAsync(RoomCreationOptions.Default);

        // Join the room using the CoherenceBridge for this scene
        if (CoherenceBridgeStore.TryGetBridge(gameObject.scene, out var bridge))
        {
            bridge.JoinRoom(newRoom);
        }
    }
}

You can set FindOrCreate on RoomCreationOptions to true to first try finding an existing room with the specified tags, before creating a new one if none were found:

// Try finding an existing room with the "Capture The Flag"
// and "Ranked" tags, or create a new one if none exist.
var creationOptions = new RoomCreationOptions
{
    FindOrCreate = true,
    Tags = new[] { "Capture The Flag", "Ranked" }
};

var room = await roomsService.CreateRoomAsync(creationOptions);

Last updated

Was this helpful?