# Setting Up Photon

### 1. Install photon

Search for Photon Unity Networking in the Unity Asset Store, then download and import it into your project. (Note whether you are downloading PUN1 or PUN2. This tutorial uses PUN2, as there may be some compatibility issues with PUN1. It is recommended to use PUN2).

<figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FiTQDFgaJctD72OLEVRvv%2Fimage.png?alt=media&#x26;token=ea0e32e9-7388-40d1-9c1e-3a4b534020cc" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Pay attention to resolving issues related to **assemblies**.
{% endhint %}

*   Open the assembly of NRSDK.

    <figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FYxDcP3jj2UjravMg6QU5%2Foutput.png?alt=media&#x26;token=36f909f8-908a-4ba6-941c-dc6923d81f30" alt=""><figcaption></figcaption></figure>
*   Find the "Assembly Definition References" section and click the "+" button to add a new reference. In the new reference, select the Photon assembly PhotonUnityNetworking and PhotonRealtime. Click the "Apply" button to save your changes. &#x20;

    <figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FaVANjsAPK2gPBwtvsFLu%2F4b1b741e-4b11-4471-87be-117081d07c7b.png?alt=media&#x26;token=9ddf4cf8-dd01-47b1-a196-a1ae0deb2faa" alt=""><figcaption></figcaption></figure>

### 2. Set up Photon in Unity

{% tabs %}
{% tab title="Use Photon Cloud Server" %}
1. Register and create a new application on the [official website of Photon](https://dashboard.photonengine.com/publiccloud/overview), then obtain the App ID for your application.

<figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2F3uBFz1G8kEEWtzmeEjCB%2F6839ac72-e8fc-4d9f-98f8-55dde33b18e8.png?alt=media&#x26;token=78cca82a-eb70-4008-8fcb-8ee547c5ef50" alt=""><figcaption></figcaption></figure>

2. In Unity, open PhotonServerSettings (`Window > Photon Unity Networking`), and then enter your App ID.

<figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FEbcytcSEYWGlyDrWBB18%2Foutput%20(1).png?alt=media&#x26;token=e7327004-e5d4-401e-ac5a-cbf088ceee3a" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Use Self-hosted Server" %}
{% hint style="info" %}
Note that the self-hosted server is **only** compatible with **Windows** operating systems. For details, please refer to the [official document of photon](https://doc.photonengine.com/server/current/getting-started/photon-server-in-5min).
{% endhint %}

1. Download [Photon Server SDK](https://www.photonengine.com/sdks#server-sdkserverserver) and extract the server package to any place - preferably an empty folder which you prepared beforehand.
2.  Get a [license](https://dashboard.photonengine.com/selfhosted), claim it and save it to **"deploy\bin\_Win64".**

    <figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2F1dNPONUnaxwrF20uQElm%2Fimage.png?alt=media&#x26;token=9447383a-c181-42a7-991f-2839d03c3b1a" alt=""><figcaption></figcaption></figure>
3. Start "PhotonControl.exe" and confirm the admin rights for this application. They are needed for the option to set up Photon as a Service. Look out for the tray-bar icon (bottom right by default). Click the white/grey icon to open a menu that controls Photon.
4.  Pick `LoadBalancing (MyCloud)` from the Photon Instances and "Start as Application". Now you started Photon.

    <figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FCpERyrAAgZPyo5GhWljR%2F2075c194-1f32-42e2-8777-f7413ff409f1.png?alt=media&#x26;token=5e565031-24c6-4848-8a16-a1cd58c5ef60" alt=""><figcaption></figcaption></figure>
5. Now you can connect clients _**running on the same machine**._
6. To connect clients from a local network, simulated device or the public internet, change the IP Address Config. The next steps are to set up the method for local network. (Use one local host as a server, and then connect other client devices to the same Wi-Fi as that server.)
7. In Unity, open PhotonServerSettings (`Window > Photon Unity Networking`, then  click `Locate PhotonServerSettings)`

&#x20;![](https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FD06ZnrNmFcOc3LqKvGqP%2Fimage%20\(2\).png?alt=media\&token=af52ac0b-4e43-4ef1-9fe8-6bd85f02ecf4)![](https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FzrTEldhMg05tWdJASHtS%2Fimage%20\(3\).png?alt=media\&token=76fe20b2-7654-49ae-8445-b75c05ee1dc2)

9.  Modify PhotonServerSettings: unselect "**`Use Name Server`**", fill in the IP address and port number of the self-hosted server (if using UDP protocol, the default port number is **5055**).

    <figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FWsLlbq8okKVUqA0HHIzN%2Fimage%20(4).png?alt=media&#x26;token=d6116909-ee42-4d0e-b512-b387fcec9cbc" alt=""><figcaption></figcaption></figure>

    You can find your local server IP here:&#x20;

<figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FZDU2UAC9wJgNnHXsJ9QD%2F%E6%88%AA%E5%B1%8F2023-08-14%2019.15.07.png?alt=media&#x26;token=3da23db4-586c-4b43-a6f5-a94680a20b97" alt=""><figcaption></figcaption></figure>

10. Modify the **`PublicIpAddress`** field in **`GameServer.xml.config`** to the IP address of the self-hosted server (default is 127.0.0.1). You can find this file in: `deploy\LoadBalancing\GameServer\bin`

    <figure><img src="https://1774958057-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIM2VX8TwuOpFaP0UDa6i%2Fuploads%2FNGdejidiFzaCDRyHqznU%2Fimage%20(1).png?alt=media&#x26;token=0e0d3b6e-a7c0-4934-8700-5aee165199f6" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

### 3. Joining a room

A crucial prerequisite for sharing an anchor among different users is that they are in the same room. Therefore, we need to set up a script to allow them to join the same room.

The `joinedRoomIndicator` is used to indicate a successful room joining. The method used here is to change the color of the join button after successfully joining.

```csharp
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;

public class JoinRoomButton : MonoBehaviourPunCallbacks
{
    public Button joinedRoomIndicator;

    public void OnButtonClick()
    {
        // Connect to the Photon master server
        if (!PhotonNetwork.IsConnected)
        {
            PhotonNetwork.ConnectUsingSettings();
        }
        else
        {
            TryJoinRoom();
        }
    }

    public override void OnConnectedToMaster()
    {
        Debug.Log("Connected to Photon master server");
        TryJoinRoom();
    }

    private void TryJoinRoom()
    {
        // Try to join the room "MyRoom"
        PhotonNetwork.JoinRoom("MyRoom");
    }

    public override void OnJoinedRoom()
    {
        Debug.Log("Joined room: " + PhotonNetwork.CurrentRoom.Name);
        
        joinedRoomIndicator.image.color = Color.green;
    }

    public override void OnJoinRoomFailed(short returnCode, string message)
    {
        Debug.Log("Failed to join room");
        // If we failed to join the room, it might not exist, so let's try to create it
        PhotonNetwork.CreateRoom("MyRoom");
    }

    public override void OnCreatedRoom()
    {
        Debug.Log("Created room: " + PhotonNetwork.CurrentRoom.Name);
    }

    public override void OnCreateRoomFailed(short returnCode, string message)
    {
        Debug.Log("Failed to create room");
    }
}

```

