tilt-shift photography of HTML codes

Auto Discovery for Photon Server in Unity

Unity has become my first choice for most of the projects I do. Majority of them are branded games, some with custom hardware interaction, and some just plain apps.

The best thing about Unity is, it’s easy as Flash was for quick drag’n drop design and interaction components, plus it’s way more powerful and extendable.

Now back to the topic. In one of my current projects, I started using Photon Server. It’s pretty awesome and well supported server platform for multiplayer games, and realtime apps.

Photon Server in LAN enviroement provides no auto discovery APIs in PUN SDK. While, Unity’s own networking APIs have built-in auto discovery machanism. So, I made a simple solution for my project.

Use Unity’s auto discovery APIs to broadcast server’s address, so clients can connect to server easily.

Here’s pretty simple implementation of the idea:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;

public class LanDiscoveryScript : NetworkDiscovery
{
    // Use this for initialization
    void Start()
    {
        Initialize();

        if(Application.isMobilePlatform)
        {
            StartAsClient();
            //log("Starting client...");
        }
        else
        {
            // set broadcast data as IP address
            broadcastData = Network.player.ipAddress;

            StartAsServer();
            //log("Starting Server IP: " + broadcastData);
        }
    }

    // only running as client will receive broadcast event
    public override void OnReceivedBroadcast(string fromAddress, string data)
    {
        base.OnReceivedBroadcast(fromAddress, data);

        //log("OnReceivedBroadcast: " + fromAddress + "\nData: " + data);

        // set server's IP in PUN and connect to it
        PhotonNetwork.PhotonServerSettings.ServerAddress = data;
        PhotonNetwork.ConnectUsingSettings("1.0");
    }
}

 

This is just very basic example to give you the idea, you can use it as it is, or customize it with more data sent to clients as per app’s requirement.

Leave a Reply