Unity Integrated Networking
Posted by SteamPunkProgrammerJul 26
I’ve been trying to learn the integrated networking (Raknet) for unity over the last few weeks. We are planning on making a peer-to-peer game using it in the near future. After a lot of reading of tutorials and looking at projects source code I finally understand it to a pretty good degree. In the process I developed a very clean master game server connection script that includes the ability to create a server, find servers with the same game type name, and verify the connection before moving the user onto a new scene. Below is the script I ended up creating.
using UnityEngine;
using System.Collections;
public class MasterGameConnector : MonoBehaviour
{
public int port = 31337;
public string gameTypeName = "change this name";
public string serverDefaultName = "some server";
public string readyScene = "sceneName";
enum ScreenState
{
Start,
Create,
Find,
Setup,
Ready
}
Vector2 serverScroll = Vector2.zero;
ScreenState currentScreenState = ScreenState.Start;
string serverNameField;
bool connected = false;
Vector2[] windowSizes = new Vector2[] {
new Vector2(150, 75),
new Vector2(200, 120),
new Vector2(200, 300),
new Vector2(200, 75)
};
Rect GetCenteredRect(Vector2 size)
{
return new Rect((Screen.width*0.5f)-(size.x*0.5f),
(Screen.height*0.5f)-(size.y*0.5f),
size.x, size.y);
}
// Use this for initialization
void Start ()
{
serverNameField = serverDefaultName;
}
void OnDisconnectedFromServer()
{
currentScreenState = ScreenState.Start;
connected = false;
}
void OnServerInitialized()
{
connected = true;
}
void OnConnectedToServer()
{
connected = true;
}
void OnGUI()
{
if (currentScreenState == ScreenState.Ready)
return;
GUI.Window(GetHashCode(), GetCenteredRect(windowSizes[(int)currentScreenState]), DrawWindow, currentScreenState.ToString());
}
void DrawWindow(int id)
{
SendMessage("Draw" + currentScreenState.ToString(), SendMessageOptions.DontRequireReceiver);
}
void DrawStart()
{
if (GUILayout.Button("Create Server"))
currentScreenState = ScreenState.Create;
if (GUILayout.Button("Find Server"))
{
currentScreenState = ScreenState.Find;
MasterServer.RequestHostList(gameTypeName);
}
}
void DrawCreate()
{
serverNameField = GUILayout.TextField(serverNameField);
if (GUILayout.Button("Create"))
{
Network.InitializeServer(32, port, true);
MasterServer.RegisterHost(gameTypeName, serverNameField);
serverNameField = serverDefaultName;
currentScreenState = ScreenState.Setup;
}
if (GUILayout.Button("Back"))
currentScreenState = ScreenState.Start;
}
void DrawFind()
{
serverScroll = GUILayout.BeginScrollView(serverScroll, GUI.skin.box);
{
HostData[] hosts = MasterServer.PollHostList();
foreach(HostData host in hosts)
{
if (GUILayout.Button(host.gameName))
{
Network.Connect(host);
currentScreenState = ScreenState.Setup;
}
}
}
GUILayout.EndScrollView();
if (GUILayout.Button("Back"))
currentScreenState = ScreenState.Start;
}
void DrawSetup()
{
GUILayout.FlexibleSpace();
if (connected)
{
if (GUILayout.Button("Ready"))
{
currentScreenState = ScreenState.Ready;
Application.LoadLevel(readyScene);
}
}
else
{
GUILayout.Label("Connecting...");
}
if (GUILayout.Button("Cancel"))
{
connected = false;
Network.Disconnect();
currentScreenState = ScreenState.Start;
}
}
}
2 comments
Comment by justifun on July 27, 2011 at 4:00 pm
Awesome!, I’ve been looking into multiplayer as well, but its a tad more confusing that playmaker.
How does one go ahead and use this in practicality? Can you put together a tutorial how to get this up and running? and perhaps in conjunction with playmaker?
Thanks
Comment by SteamPunkProgrammer on July 27, 2011 at 4:26 pm
I plan on, in the near future, once I fully understand unity networking, to do a whole series of videos on the subject because there is very little information on the subject, and almost no tutorials.
I do have some thoughts in the back of my mind about making some actions for playmaker to handle atleast the non-authoritative replication. If this does happen, I will definitively post about it on here.