This document is about: PUN 2
SWITCH TO

PUN Classic (v1), PUN 2, Bolt는 휴업 모드입니다. Unity2022에 대해서는 PUN 2에서 서포트하지만, 신기능의 추가는 없습니다. 현재 이용중인 고객님의 PUN 및 Bolt 프로젝트는 중단되지 않고, 퍼포먼스나 성능이 떨어지는 일도 없습니다. 앞으로의 새로운 프로젝트에는 Photon Fusion 또는 Quantum을 사용해 주십시오.

Discord Activities

You can integrate your WebAssembly game directly into Discord as "Discord Activities". Multiplayer games have to meet extra requirements to work in this environment but the Realtime API is well prepared to support this.

To enable multiplayer in Discord Activities, you need to setup URL Mapping for Photon's addresses and rewrite the URLs on the client side to use the mapping.

This is supported with the Photon Realtime .Net and Unity SDKs v4.1.8.10 and up as well as v5.1.0 and up.

Requirements and Setup

Applications running as "Discord Activity" are hosted on a secure page (https), which means they can not communicate with just any server they like. All communication must go to discord.com.

Discord solves this via "URL Mapping", which allows you to sets up an address on discord.com for arbitrary addresses needed by an app.

The URL Mapping for a Discord Activity looks like this:

Discord URL Mapping
Discord URL Mapping

Please refer to Discord's documentation for details and terms of usage.

Setting up the URL Mapping for Photon makes it available via discord.com address but the clients will still not know and use the new addresses. This is done via Address Rewriting for Photon.

Address Rewriting

To modify the server addresses on a Photon client, a client.AddressRewriter function is used. Once registered, your Func<string, ServerConnection, string> gets called before any connection is made and can modify the address as needed.

For Discord Activities, the rewriting could look like this:

C#

private string AddressRewriterDiscord(string address, ServerConnection serverType)
{
    bool isUri = Uri.TryCreate(address, UriKind.Absolute, out Uri uri);
    if (isUri)
    {
        string host = uri.Host;
        string[] hostSplit = host.Split('.');
        if (hostSplit.Length != 3)
        {
            Debug.LogError($"Host address could not be split into 3 parts (subdomain, domain and tld).");
            return address;
        }

        string subdomain = hostSplit[0];
        string domain = hostSplit[1];

        string discordAddress = $"{uri.Scheme}://123.discord.com/{domain}Prefix/{subdomain}";
        //Debug.Log($"discordAddress: {discordAddress}");
        return discordAddress;
    }

    return address;
}

To apply this address rewriting, register the method before the client connects:
client.AddressRewriter = this.AddressReplacerDiscord;

In PUN, set: PhotonNetwork.NetworkingClient.AddressRewriter.

Make sure to use this only on builds for Discord Activities.

Back to top