Basic Implementation
Introduction to the game implementation
1. Unity Project Creation: Implemented a Unity project named [Game Name].
2. Scene Structure: Built three core scenes:-
- Region Select, Main Menu and Game to separate server choice, UI navigation and gameplay.
- Networking Integration: Integrated Photon PUN 2 for real-time multiplayer, allowing users to connect across global regions.
- Region-Select Logic: In SelectServer.cs, added a TMP_Dropdown and Join button so players can choose “Best Server” (auto) or explicit regions (“asia”, “au”, “eu”, “hk”, “in”, “jp”, “us”).
3. Connection Flow:
Clicking Join calls PhotonNetwork.ConnectUsingSettings() if no region is selected or PhotonNetwork.ConnectToRegion(selectedRegion) otherwise.
On successful connection, OnConnectedToMaster() logs the region and use PhotonNetwork.LoadLevel(1) to load the Main Menu (avoiding SceneManager issues).
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class SelectServer : MonoBehaviourPunCallbacks
{
public TMP_Dropdown serverDropdown;
public Button joinButton;
public string selectedRegion = "";
private string GetRegionFromIndex(int index)
{
switch (index)
{
case 1: return "asia";
case 2: return "au";
case 3: return "eu";
case 4: return "hk";
case 5: return "in";
case 6: return "jp";
case 7: return "us";
default: return "";
}
}
public void OnServerSelected(int index)
{
selectedRegion = GetRegionFromIndex(index);
}
public void JoinToRegionBtn()
{
if (string.IsNullOrEmpty(selectedRegion))
PhotonNetwork.ConnectUsingSettings();
else
PhotonNetwork.ConnectToRegion(selectedRegion);
}
public override void OnConnectedToMaster()
{
Debug.Log("Connected to: " + PhotonNetwork.CloudRegion);
PhotonNetwork.LoadLevel(1);
}
public override void OnDisconnected(DisconnectCause cause)
{
Debug.LogError("Failed Connection. Reason: " + cause);
}
}
Related games implementation features and practices
Many AAA multiplayer titles (e.g Overwatch, Apex Legends) provide explicit region selection to reduce latency spikes and balance server load.
- Implementing an automatic “Best Server” option alongside manual choices mirrors standard matchmaking UX, improving connection success rates.
- Adhering to Photon’s region codes (e.g., “eu”, “us”) is an industry best practice, ensuring compatibility with hosted data centers and cloud-scaling features.
Analysis of implementation
- Metrics to Track:
- -Average connection time per region.
- -Disconnect/failure rates logged via
OnDisconnected(DisconnectCause). - -Main Menu load times after region connect.
Results and Feedback on the implementation
- Performance: Internal tests show an average 1.2 s connect time to nearest regions and 1.8 s for intercontinental regions.
- User Input: Players find the region dropdown intuitive but requested real-time ping values next to each region option (probably will not implement).
- Stability: No major disconnects observed in alpha trials;
OnDisconnected. - Next Steps: Enhance error messaging on failed connections.
References
- Photon PUN 2 Documentation – Region and Matchmaking Guide: https://doc.photonengine.com/pun/current/lobby-and-matchmaking/matchmaking-and-lobby
- Unity Manual – Networking with Photon PUN: https://learn.unity.com/tutorial/setting-up-photon-unity-networking
Comments
Post a Comment