Skip to content

Instantly share code, notes, and snippets.

@perky
Created August 3, 2018 16:31
Show Gist options
  • Save perky/8706d8e174c2b1d5e04bda7dee7b24cd to your computer and use it in GitHub Desktop.
Save perky/8706d8e174c2b1d5e04bda7dee7b24cd to your computer and use it in GitHub Desktop.
using UnityEngine;
using Bolt;
public class LobbyMenu : GlobalEventListener
{
[SerializeField] string _gameplayScene;
[SerializeField] ushort _port = 27000;
[SerializeField] ushort _broadcastPort = 27001;
bool _bClientLANInit;
bool _bLANConnect;
void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, Screen.width - 20, Screen.height - 20));
if (GUILayout.Button("Start Server", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
{
BoltLauncher.StartServer(UdpKit.UdpEndPoint.Parse("127.0.0.1:" + _port));
}
if (GUILayout.Button("Connect Localhost", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
{
_bLANConnect = false;
BoltLauncher.StartClient();
}
if (GUILayout.Button("Connect LAN", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
{
_bLANConnect = true;
BoltLauncher.StartClient();
}
GUILayout.EndArea();
}
public override void BoltStartDone()
{
if(BoltNetwork.isServer)
{
BoltNetwork.LoadScene(_gameplayScene);
BoltNetwork.EnableLanBroadcast(_broadcastPort);
}
else
{
if (_bLANConnect)
{
BoltNetwork.EnableLanBroadcast(_broadcastPort);
_bClientLANInit = true;
BoltLog.Info("Searching for server...");
} else {
BoltNetwork.Connect(UdpKit.UdpEndPoint.Parse("127.0.0.1:" + _port));
}
}
}
void Update()
{
if (_bClientLANInit && !BoltNetwork.isConnected)
{
foreach (var session in BoltNetwork.SessionList)
{
BoltNetwork.Connect(session.Value.LanEndPoint);
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment