This document is about: PUN 2
SWITCH TO

PUN Classic (v1)、PUN 2 和 Bolt 處於維護模式。 PUN 2 將支援 Unity 2019 至 2022,但不會添加新功能。 當然,您所有的 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